Skip to content

Commit 91358ba

Browse files
committed
Simplify
1 parent a2959b3 commit 91358ba

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

lib/ruby_llm/schema.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,9 @@ def valid?
6262
end
6363
end
6464

65-
def self.new(*args, **kwargs, &block)
66-
# Only return the class itself when called within a schema block context for embedding
67-
# This is determined by checking if we're being called with no arguments in a specific context
68-
if args.empty? && kwargs.empty? && block.nil? && caller.any? { |line| line.include?("class_eval") }
69-
self
70-
else
71-
instance = allocate
72-
instance.send(:initialize, *args, **kwargs)
73-
instance
74-
end
75-
end
76-
77-
def initialize(name = nil, description: nil, **kwargs)
65+
def initialize(name = nil, description: nil)
7866
@name = name || self.class.name || "Schema"
79-
@description = description || kwargs[:description]
67+
@description = description
8068
end
8169

8270
def validate!

lib/ruby_llm/schema/dsl/schema_builders.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def object_schema(description: nil, of: nil, &block)
5454
# If the block returned a reference and no properties were added, use the reference
5555
if result.is_a?(Hash) && result["$ref"] && sub_schema.properties.empty?
5656
result.merge(description ? {description: description} : {})
57-
# If the block returned a Schema class instance, convert it to reference
57+
# If the block returned a Schema class or instance, convert it to inline schema
5858
elsif schema_class?(result) && sub_schema.properties.empty?
5959
schema_class_to_inline_schema(result).merge(description ? {description: description} : {})
6060
else

lib/ruby_llm/schema/dsl/utilities.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def determine_array_items(of, &)
3838
return reference(of) if of.is_a?(Symbol)
3939
return schema_class_to_inline_schema(of) if schema_class?(of)
4040

41-
raise InvalidArrayTypeError, "Invalid array type: #{of.inspect}. Must be a primitive type (:string, :number, etc.), a symbol reference, or a Schema class."
41+
raise InvalidArrayTypeError, "Invalid array type: #{of.inspect}. Must be a primitive type (:string, :number, etc.), a symbol reference, a Schema class, or a Schema instance."
4242
end
4343

4444
def determine_object_reference(of, description = nil)
@@ -52,7 +52,11 @@ def determine_object_reference(of, description = nil)
5252
raise InvalidObjectTypeError, "Invalid object type: #{of.inspect}. Class must inherit from RubyLLM::Schema."
5353
end
5454
else
55-
raise InvalidObjectTypeError, "Invalid object type: #{of.inspect}. Must be a symbol reference or a Schema class."
55+
if schema_class?(of)
56+
schema_class_to_inline_schema(of)
57+
else
58+
raise InvalidObjectTypeError, "Invalid object type: #{of.inspect}. Must be a symbol reference, a Schema class, or a Schema instance."
59+
end
5660
end
5761

5862
description ? result.merge(description: description) : result
@@ -87,18 +91,31 @@ def primitive_type?(type)
8791
end
8892

8993
def schema_class?(type)
90-
type.is_a?(Class) && type < Schema
94+
(type.is_a?(Class) && type < Schema) || type.is_a?(Schema)
9195
end
9296

93-
def schema_class_to_inline_schema(schema_class)
97+
def schema_class_to_inline_schema(schema_class_or_instance)
98+
# Handle both Schema classes and Schema instances
99+
schema_class = if schema_class_or_instance.is_a?(Class)
100+
schema_class_or_instance
101+
else
102+
schema_class_or_instance.class
103+
end
104+
94105
# Directly convert schema class to inline object schema
95106
{
96107
type: "object",
97108
properties: schema_class.properties,
98109
required: schema_class.required_properties,
99110
additionalProperties: schema_class.additional_properties
100111
}.tap do |schema|
101-
schema[:description] = schema_class.description if schema_class.description
112+
# For instances, prefer instance description over class description
113+
description = if schema_class_or_instance.is_a?(Class)
114+
schema_class.description
115+
else
116+
schema_class_or_instance.instance_variable_get(:@description) || schema_class.description
117+
end
118+
schema[:description] = description if description
102119
end
103120
end
104121
end

spec/ruby_llm/schema_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,11 @@
409409
# Invalid object types
410410
expect {
411411
schema_class.object :item, of: 123
412-
}.to raise_error(RubyLLM::Schema::InvalidObjectTypeError, /Invalid object type: 123.*Must be a symbol reference or a Schema class/)
412+
}.to raise_error(RubyLLM::Schema::InvalidObjectTypeError, /Invalid object type: 123.*Must be a symbol reference, a Schema class, or a Schema instance/)
413413

414414
expect {
415415
schema_class.object :item, of: "invalid"
416-
}.to raise_error(RubyLLM::Schema::InvalidObjectTypeError, /Invalid object type: "invalid".*Must be a symbol reference or a Schema class/)
416+
}.to raise_error(RubyLLM::Schema::InvalidObjectTypeError, /Invalid object type: "invalid".*Must be a symbol reference, a Schema class, or a Schema instance/)
417417

418418
# Non-Schema class
419419
expect {

0 commit comments

Comments
 (0)