Class: Anthropic::Helpers::InputSchema::UnionOf

Inherits:
Object
  • Object
show all
Includes:
JsonSchemaConverter, Internal::Type::Union
Defined in:
lib/anthropic/helpers/input_schema/union_of.rb

Overview

Examples:

example = Anthropic::UnionOf[Float, Anthropic::ArrayOf[Integer]]

Generic:

  • Member

Constant Summary

Constants included from JsonSchemaConverter

JsonSchemaConverter::NO_REF, JsonSchemaConverter::POINTERS

Class Method 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::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, meta_info, new_coerce_state, type_info

Constructor Details

#initialize(*variants) ⇒ UnionOf

Returns a new instance of UnionOf.

Parameters:

  • variants (Array<generic<Member>>)


48
49
50
51
52
# File 'lib/anthropic/helpers/input_schema/union_of.rb', line 48

def initialize(*variants)
  variants.each do |v|
    v.is_a?(Proc) ? variant(v) : variant(-> { v })
  end
end

Class Method Details

.[]void



45
# File 'lib/anthropic/helpers/input_schema/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.

Parameters:

  • state (Hash{Symbol=>Object})

    @option state [HashObject=>String] :defs

    @option state [Array]

Returns:

  • (Hash{Symbol=>Object})


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/anthropic/helpers/input_schema/union_of.rb', line 23

def to_json_schema_inner(state:)
  Anthropic::Helpers::InputSchema::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}"]}
      Anthropic::Helpers::InputSchema::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 ? Anthropic::Internal::Util.deep_merge(*schemas, concat: true) : {anyOf: schemas}
  end
end