Class: Riffer::Agent::StructuredOutput
- Inherits:
-
Object
- Object
- Riffer::Agent::StructuredOutput
- Defined in:
- lib/riffer/agent/structured_output.rb
Overview
Parses and validates structured JSON responses against a Riffer::Params schema.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
The schema parameters.
Instance Method Summary collapse
-
#initialize(params) ⇒ StructuredOutput
constructor
– : (Riffer::Params) -> void.
-
#json_schema(strict: false) ⇒ Object
Returns the JSON Schema for this structured output.
-
#parse_and_validate(json_string) ⇒ Object
Parses a JSON string and validates it against the schema, returning a Result carrying either the validated object or an error message.
Constructor Details
#initialize(params) ⇒ StructuredOutput
– : (Riffer::Params) -> void
14 15 16 |
# File 'lib/riffer/agent/structured_output.rb', line 14 def initialize(params) @params = params end |
Instance Attribute Details
#params ⇒ Object (readonly)
The schema parameters.
10 11 12 |
# File 'lib/riffer/agent/structured_output.rb', line 10 def params @params end |
Instance Method Details
#json_schema(strict: false) ⇒ Object
Returns the JSON Schema for this structured output.
– : (?strict: bool) -> Hash[Symbol, untyped]
22 23 24 |
# File 'lib/riffer/agent/structured_output.rb', line 22 def json_schema(strict: false) @params.to_json_schema(strict: strict) end |
#parse_and_validate(json_string) ⇒ Object
Parses a JSON string and validates it against the schema, returning a Result carrying either the validated object or an error message. – : (String) -> Riffer::Agent::StructuredOutput::Result
30 31 32 33 34 35 36 37 38 |
# File 'lib/riffer/agent/structured_output.rb', line 30 def parse_and_validate(json_string) parsed = JSON.parse(json_string, symbolize_names: true) validated = @params.validate(parsed) Result.new(object: validated) rescue JSON::ParserError => e Result.new(error: "JSON parse error: #{e.}") rescue Riffer::ValidationError => e Result.new(error: "Validation error: #{e.}") end |