Module: OpenAI::Helpers::StructuredOutput::ResponseParser Private
- Defined in:
- lib/openai/helpers/structured_output/response_parser.rb,
sig/openai/helpers/structured_output/response_parser.rbs
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Request preparation and response coercion for the Responses API.
Class Method Summary collapse
-
.get_models(parsed) ⇒ Array<(JsonSchemaConverter|nil, Hash)>
private
Extracts structured output models and replaces request values with JSON Schema.
-
.parse!(raw, model, tool_models) ⇒ Hash
private
Post-processes raw API responses to parse and coerce structured outputs into typed Ruby objects.
Class Method Details
.get_models(parsed) ⇒ Array<(JsonSchemaConverter|nil, Hash)>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts structured output models and replaces request values with JSON Schema.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/openai/helpers/structured_output/response_parser.rb', line 60 def self.get_models(parsed) model = nil tool_models = {} case parsed in {text: OpenAI::StructuredOutput::JsonSchemaConverter => model} parsed.update( text: { format: { type: :json_schema, strict: true, name: model.name.split("::").last, schema: model.to_json_schema } } ) in {text: {format: OpenAI::StructuredOutput::JsonSchemaConverter => model}} parsed.fetch(:text).update( format: { type: :json_schema, strict: true, name: model.name.split("::").last, schema: model.to_json_schema } ) in { text: { format: { type: :json_schema, schema: OpenAI::StructuredOutput::JsonSchemaConverter => model } } } parsed.dig(:text, :format).store(:schema, model.to_json_schema) else end case parsed in {tools: Array => tools} # rubocop:disable Metrics/BlockLength mapped = tools.map do |tool| case tool in OpenAI::StructuredOutput::JsonSchemaConverter name = tool.name.split("::").last tool_models.store(name, tool) { type: :function, strict: true, name: name, parameters: tool.to_json_schema } in {type: :function, parameters: OpenAI::StructuredOutput::JsonSchemaConverter => params} func = tool.fetch(:function) name = func[:name] ||= params.name.split("::").last tool_models.store(name, params) func.update(parameters: params.to_json_schema) tool in {type: _, function: {parameters: OpenAI::StructuredOutput::JsonSchemaConverter => params, **}} name = tool[:function][:name] || params.name.split("::").last tool_models.store(name, params) tool[:function][:parameters] = params.to_json_schema tool in {type: _, function: Hash => func} params = func[:parameters] # rubyfmt 0.14.1 corrupts a long guarded `in` clause into `in if`. # Keep the same guard in the existing handwritten branch instead. next tool unless params.is_a?(Class) && params < OpenAI::Internal::Type::BaseModel name = func[:name] || params.name.split("::").last tool_models.store(name, params) func[:parameters] = params.to_json_schema tool else tool end end # rubocop:enable Metrics/BlockLength tools.replace(mapped) else end [model, tool_models] end |
.parse!(raw, model, tool_models) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Post-processes raw API responses to parse and coerce structured outputs into typed Ruby objects.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/openai/helpers/structured_output/response_parser.rb', line 18 def self.parse!(raw, model, tool_models) if model.is_a?(OpenAI::StructuredOutput::JsonSchemaConverter) raw[:output] &.flat_map do |output| next [] unless output[:type] == "message" output[:content].to_a end &.each do |content| next unless content[:type] == "output_text" begin parsed = JSON.parse(content.fetch(:text), symbolize_names: true) rescue JSON::ParserError => e parsed = e end coerced = OpenAI::Internal::Type::Converter.coerce(model, parsed) content.store(:parsed, coerced) end end raw[:output]&.each do |output| next unless output[:type] == "function_call" next if (model = tool_models[output.fetch(:name)]).nil? begin parsed = JSON.parse(output.fetch(:arguments), symbolize_names: true) rescue JSON::ParserError => e parsed = e end coerced = OpenAI::Internal::Type::Converter.coerce(model, parsed) output.store(:parsed, coerced) end raw end |