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::NO_REF, JsonSchemaConverter::POINTERS
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, new_coerce_state, type_info
Constructor Details
#initialize(*variants) ⇒ UnionOf
Returns a new instance of UnionOf.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/openai/helpers/structured_output/union_of.rb', line 58 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
.[] ⇒ void
55 |
# File 'lib/openai/helpers/structured_output/union_of.rb', line 55 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 42 43 44 45 46 47 48 49 50 51 |
# 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 } if mergeable OpenAI::Internal::Util.deep_merge(*schemas, concat: true) else { anyOf: schemas.each do if _1.key?(:$ref) _1.update(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::NO_REF => true) end end } end end end |