class Riffer::StructuredOutput
Riffer::StructuredOutput provides parse/validate for structured JSON responses from LLM providers.
params = Riffer::Params.new params.required(:sentiment, String) so = Riffer::StructuredOutput.new(params) result = so.parse_and_validate(‘{“sentiment”:“positive”,“score”:0.9}’) result.object #=> {sentiment: “positive”, score: 0.9}
Attributes
Public Class Methods
Source
# File lib/riffer/structured_output.rb, line 19 def initialize(params) @params = params end
: (Riffer::Params) -> void
Public Instance Methods
Source
# File lib/riffer/structured_output.rb, line 26 def json_schema(strict: false) @params.to_json_schema(strict: strict) end
Returns the JSON Schema for this structured output.
: (?strict: bool) -> Hash[Symbol, untyped]
Source
# File lib/riffer/structured_output.rb, line 35 def parse_and_validate(json_string) parsed = JSON.parse(json_string) validated = @params.validate(parsed.transform_keys(&:to_sym)) 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
Parses a JSON string and validates it against the schema.
Returns a Result with the validated object on success, or an error message on failure.
: (String) -> Riffer::StructuredOutput::Result