Skip to content

Commit 905c511

Browse files
committed
Try again
1 parent aa2780a commit 905c511

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

lib/ruby_llm/template/loader.rb

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,43 @@ def load_schema_class(context = {})
7979
# Create a context for evaluating the schema
8080
schema_context = create_schema_context(context)
8181

82-
# Evaluate the schema file and return the result
82+
# Evaluate the schema file (this might define classes/modules)
8383
result = schema_context.instance_eval(schema_content, schema_file.to_s)
8484

85-
# Debug: Log what we got back from evaluation
85+
# Try different ways to find the schema:
86+
87+
# 1. If the result is already a valid schema class or instance
88+
if result.is_a?(Class) && result < RubyLLM::Schema
89+
return result.new
90+
elsif result.is_a?(RubyLLM::Schema) || result.respond_to?(:to_json_schema)
91+
return result
92+
end
93+
94+
# 2. Look for a schema class using naming conventions
95+
# Convert template name to expected class name (e.g., "identify_brand_from_transaction" -> "IdentifyBrandFromTransaction::Schema")
96+
template_class_name = @template_name.to_s.split('_').map(&:capitalize).join
97+
possible_class_names = [
98+
"#{template_class_name}::Schema",
99+
"#{template_class_name}Schema",
100+
template_class_name
101+
]
102+
103+
schema_class = nil
104+
possible_class_names.each do |class_name|
105+
begin
106+
schema_class = class_name.constantize
107+
break if schema_class.is_a?(Class) && schema_class < RubyLLM::Schema
108+
schema_class = nil
109+
rescue NameError
110+
# Class doesn't exist, try next one
111+
end
112+
end
113+
114+
if schema_class
115+
return schema_class.new
116+
end
117+
118+
# 3. If nothing worked, provide detailed error
86119
result_info = if result.nil?
87120
"nil"
88121
elsif result.is_a?(Class)
@@ -91,14 +124,7 @@ def load_schema_class(context = {})
91124
"Instance: #{result.class} (is_a?(RubyLLM::Schema): #{result.is_a?(RubyLLM::Schema)}, responds_to?(:to_json_schema): #{result.respond_to?(:to_json_schema)})"
92125
end
93126

94-
# If the result is a class that inherits from RubyLLM::Schema, instantiate it
95-
if result.is_a?(Class) && result < RubyLLM::Schema
96-
result.new
97-
elsif result.is_a?(RubyLLM::Schema) || result.respond_to?(:to_json_schema)
98-
result
99-
else
100-
raise Error, "Schema file must return a RubyLLM::Schema class or instance. Got: #{result_info}"
101-
end
127+
raise Error, "Schema file must return a RubyLLM::Schema class or instance, or define one of: #{possible_class_names.join(', ')}. Got: #{result_info}"
102128
rescue Error => e
103129
# Re-raise our own errors as-is to preserve the detailed message
104130
raise e

0 commit comments

Comments
 (0)