Class: OpenAI::Helpers::StructuredOutput::SorbetAdapter

Inherits:
Object
  • Object
show all
Includes:
JsonSchemaConverter, Internal::Type::Converter
Defined in:
lib/openai/helpers/sorbet.rb,
sig/openai/helpers/sorbet.rbs

Overview

Adapts application-owned Sorbet models to structured output requests.

Load explicitly with require "openai/helpers/sorbet".

Defined Under Namespace

Classes: HydrationError

Constant Summary

Constants included from JsonSchemaConverter

JsonSchemaConverter::NO_REF, JsonSchemaConverter::POINTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JsonSchemaConverter

assoc_meta!, cache_def!, to_json_schema, to_json_schema_inner, to_nilable

Methods included from Internal::Type::Converter

coerce, coerce_with_error, #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

#initialize(model) ⇒ SorbetAdapter

Returns a new instance of SorbetAdapter.

Parameters:

  • model (Class<T::Struct>)


35
36
37
38
39
40
41
42
43
# File 'lib/openai/helpers/sorbet.rb', line 35

def initialize(model)
  unless model.is_a?(Class) && model < T::Struct && !model.name.to_s.empty?
    raise ArgumentError, "#{model} must be a named T::Struct subclass"
  end

  @name = model.name
  @compiled_structs = {}
  @node = compile_struct(model, path: @name, ancestors: [])
end

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


32
33
34
# File 'lib/openai/helpers/sorbet.rb', line 32

def name
  @name
end

Instance Method Details

#coerce(value, state:) ⇒ 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:

  • value (Object)
  • state: (::Hash[Symbol, untyped])

Returns:

  • (Object)


52
53
54
55
56
57
58
# File 'lib/openai/helpers/sorbet.rb', line 52

def coerce(value, state:)
  return value if value.nil? || value.is_a?(JSON::ParserError)

  hydrated = hydrate(@node, value, path: @name.split("::").last)
  state.fetch(:exactness)[:yes] += 1
  hydrated
end

#to_json_schemaHash{Symbol=>Object}

Returns:

  • (Hash{Symbol=>Object})


46
# File 'lib/openai/helpers/sorbet.rb', line 46

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

#to_json_schema_inner(state:) ⇒ 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.



49
# File 'lib/openai/helpers/sorbet.rb', line 49

def to_json_schema_inner(state:) = schema_for(@node, state: state)