Class: OpenAI::Helpers::StructuredOutput::BaseModel

Inherits:
Internal::Type::BaseModel show all
Extended by:
JsonSchemaConverter
Defined in:
lib/openai/helpers/structured_output/base_model.rb

Overview

Represents a response from OpenAI's API where the model's output has been structured according to a schema predefined by the user.

This class is specifically used when making requests with the response_format parameter set to use structured output (e.g., JSON).

See examples/structured_outputs_chat_completionsexamples/structured_outputs_chat_completions.rb for a complete example of use

Constant Summary

Constants included from JsonSchemaConverter

JsonSchemaConverter::NO_REF, JsonSchemaConverter::POINTERS

Instance Attribute Summary

Attributes inherited from Internal::Type::BaseModel

#last_response

Class Method Summary collapse

Methods included from JsonSchemaConverter

assoc_meta!, cache_def!, to_json_schema_inner, to_nilable

Methods inherited from Internal::Type::BaseModel

==, #==, #[], #_request_id, #_set_last_response, coerce, #deconstruct_keys, #deep_to_h, dump, #encode_with, fields, hash, #hash, inherited, #initialize, inspect, #inspect, known_fields, recursively_to_h, required, #to_h, #to_json, #to_s, to_sorbet_type, #to_yaml

Methods included from Internal::Type::Converter

#coerce, coerce, #dump, dump, #inspect, inspect, meta_info, new_coerce_state, type_info

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

This class inherits a constructor from OpenAI::Internal::Type::BaseModel

Class Method Details

.optionalObject



62
63
64
65
66
67
# File 'lib/openai/helpers/structured_output/base_model.rb', line 62

def optional(...)
  message =
    "`optional` is not supported for structured output APIs, " \
    "use `#required` with `nil?: true` instead"
  raise message
end

.to_json_schemaHash{Symbol=>Object}

Returns:

  • (Hash{Symbol=>Object})


16
# File 'lib/openai/helpers/structured_output/base_model.rb', line 16

def to_json_schema = OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_json_schema(self)

.to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object}

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.

Parameters:

  • state (Hash{Symbol=>Object})

    @option state [HashObject=>String] :defs

    @option state [Array] :path

Returns:

  • (Hash{Symbol=>Object})

Raises:

  • (ArgumentError)


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
53
54
55
56
57
58
# File 'lib/openai/helpers/structured_output/base_model.rb', line 27

def to_json_schema_inner(state:)
  field_values = fields.values
  duplicate = field_values.map { _1.fetch(:api_name) }.tally.find { _2 > 1 }
  raise ArgumentError.new("Duplicate API field name: #{duplicate.first}") if duplicate

  OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.cache_def!(state, type: self) do
    path = state.fetch(:path)
    properties = field_values.to_h do |field|
      api_name, type, nilable, meta = field.fetch_values(:api_name, :type, :nilable, :meta)
      new_state = {**state, path: [*path, ".#{api_name}"]}

      schema = OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_json_schema_inner(
        type,
        state: new_state
      )
      schema = OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_nilable(schema) if nilable
      OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.assoc_meta!(
        schema,
        meta: meta.except(:api_name)
      )

      [api_name, schema]
    end

    {
      type: "object",
      properties: properties,
      required: properties.keys.map(&:to_s),
      additionalProperties: false
    }
  end
end