Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ruby_llm/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def method_missing(method_name, ...)
end

def respond_to_missing?(method_name, include_private = false)
%i[string number integer boolean array object any_of null].include?(method_name) || super
%i[string number integer boolean array object any_of one_of null].include?(method_name) || super
end
end
end
4 changes: 4 additions & 0 deletions lib/ruby_llm/schema/dsl/complex_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def any_of(name, description: nil, required: true, **options, &block)
add_property(name, any_of_schema(description: description, **options, &block), required: required)
end

def one_of(name, description: nil, required: true, **options, &block)
add_property(name, one_of_schema(description: description, **options, &block), required: required)
end

def optional(name, description: nil, &block)
any_of(name, description: description) do
instance_eval(&block)
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_llm/schema/dsl/schema_builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ def any_of_schema(description: nil, &block)
}.compact
end

def one_of_schema(description: nil, &block)
schemas = collect_schemas_from_block(&block)

{
description: description,
oneOf: schemas
}.compact
end

private

def determine_array_items(of, &)
Expand Down
51 changes: 50 additions & 1 deletion spec/ruby_llm/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@
end
end
end

it "supports arrays of oneOf types" do
schema_class.array :items do
one_of :value do
string :alphanumeric
number :numeric
end
end
end

it "supports basic oneOf with primitive types" do
schema_class.one_of :status do
string enum: %w[active inactive]
integer
boolean
end

properties = schema_class.properties
one_of_schemas = properties[:status][:oneOf]

expect(one_of_schemas).to include(
{type: "string", enum: %w[active inactive]},
{type: "integer"},
{type: "boolean"}
)
end
end

# ===========================================
Expand Down Expand Up @@ -221,6 +247,29 @@
expect(object_schema[:properties][:nested_field]).to eq({type: "string"})
end

it "supports one_of with mixed types including objects" do
schema_class.one_of :exclusive_field do
string enum: %w[option1 option2]
integer
object do
string :nested_field
end
null
end

properties = schema_class.properties
one_of_schemas = properties[:exclusive_field][:oneOf]

expect(one_of_schemas).to include(
{type: "string", enum: %w[option1 option2]},
{type: "integer"},
{type: "null"}
)

object_schema = one_of_schemas.find { |s| s[:type] == "object" }
expect(object_schema[:properties][:nested_field]).to eq({type: "string"})
end

it "supports reference to a defined schema by block" do
schema_class.define :address do
string :street
Expand Down Expand Up @@ -368,7 +417,7 @@
it "supports method delegation for schema methods" do
instance = schema_class.new

expect(instance).to respond_to(:string, :number, :integer, :boolean, :array, :object, :any_of, :null)
expect(instance).to respond_to(:string, :number, :integer, :boolean, :array, :object, :any_of, :one_of, :null)
expect(instance).not_to respond_to(:unknown_method)
end

Expand Down