Class: OpenAI::Helpers::StructuredOutput::UnionOf
- Inherits:
-
Object
- Object
- OpenAI::Helpers::StructuredOutput::UnionOf
- Includes:
- JsonSchemaConverter, Internal::Type::Union
- Defined in:
- lib/openai/helpers/structured_output/union_of.rb
Overview
Constant Summary
Constants included from JsonSchemaConverter
JsonSchemaConverter::COUNTER, JsonSchemaConverter::POINTER
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*variants) ⇒ UnionOf
constructor
A new instance of UnionOf.
- #to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object} private
Methods included from JsonSchemaConverter
cache_def!, to_json_schema, to_json_schema_inner, to_nilable
Methods included from Internal::Type::Union
#==, #===, #coerce, #dump, #hash, #inspect, #to_sorbet_type, #variants
Methods included from Internal::Util::SorbetRuntimeSupport
#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type
Methods included from Internal::Type::Converter
#coerce, coerce, #dump, dump, #inspect, inspect, type_info
Constructor Details
#initialize(*variants) ⇒ UnionOf
Returns a new instance of UnionOf.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/openai/helpers/structured_output/union_of.rb', line 48 def initialize(*variants) case variants in [Symbol => d, Hash => vs] discriminator(d) vs.each do |k, v| v.is_a?(Proc) ? variant(k, v) : variant(k, -> { v }) end else variants.each do |v| v.is_a?(Proc) ? variant(v) : variant(-> { v }) end end end |
Class Method Details
.[] ⇒ Object
45 |
# File 'lib/openai/helpers/structured_output/union_of.rb', line 45 def self.[](...) = new(...) |
Instance Method Details
#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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/openai/helpers/structured_output/union_of.rb', line 23 def to_json_schema_inner(state:) OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.cache_def!(state, type: self) do path = state.fetch(:path) mergeable_keys = {[:anyOf] => 0, [:type] => 0} schemas = variants.to_enum.with_index.map do new_state = {**state, path: [*path, "?.#{_2}"]} OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_json_schema_inner( _1, state: new_state ) end schemas.each do |schema| mergeable_keys.each_key { mergeable_keys[_1] += 1 if schema.keys == _1 } end mergeable = mergeable_keys.any? { _1.last == schemas.length } mergeable ? OpenAI::Internal::Util.deep_merge(*schemas, concat: true) : {anyOf: schemas} end end |