Class: Riffer::Agent::StructuredOutput

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#paramsObject (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.message}")
rescue Riffer::ValidationError => e
  Result.new(error: "Validation error: #{e.message}")
end