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

Class Method Summary collapse

Methods included from JsonSchemaConverter

cache_def!, to_json_schema_inner, to_nilable

Methods inherited from Internal::Type::BaseModel

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

Methods included from Internal::Type::Converter

#coerce, coerce, #dump, dump, inspect, #inspect, 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

.optionalvoid

Raises:



68
69
70
71
72
73
# File 'lib/openai/helpers/structured_output/base_model.rb', line 68

def optional(...)
  # rubocop:disable Layout/LineLength
  message = "`optional` is not supported for structured output APIs, use `#required` with `nil?: true` instead"
  # rubocop:enable Layout/LineLength
  raise RuntimeError.new(message)
end

.required(name_sym, type_info, spec = {}) ⇒ void



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

def required(name_sym, type_info, spec = {})
  super

  doc = [type_info, spec].grep(Hash).filter_map { _1[:doc] }.first
  known_fields.fetch(name_sym).update(description: doc) unless doc.nil?
end

.to_json_schemaHash{Symbol=>Object}

Returns:



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:

  • @option state [HashObject=>String] :defs

    @option state [Array] :path

Returns:

API:

  • private



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
# File 'lib/openai/helpers/structured_output/base_model.rb', line 27

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

      schema =
        case type
        in {"$ref": String}
          type
        in OpenAI::Helpers::StructuredOutput::JsonSchemaConverter
          type.to_json_schema_inner(state: new_state).update(field.slice(:description))
        else
          OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_json_schema_inner(
            type,
            state: new_state
          )
        end
      schema = OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_nilable(schema) if nilable
      [name, schema]
    end

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