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



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

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

Class Method Details

.[]void



47
# File 'lib/anthropic/helpers/input_schema/union_of.rb', line 47

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
# 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 do
        mergeable_keys[_1] += 1 if schema.keys == _1 && schema[_1].is_a?(Array)
      end
    end
    mergeable = mergeable_keys.any? { _1.last == schemas.length }
    mergeable ? Anthropic::Internal::Util.deep_merge(*schemas, concat: true) : {anyOf: schemas}
  end
end