Module: Anthropic::Internal::Type::Union Private

Includes:
Converter, Util::SorbetRuntimeSupport
Included in:
Helpers::InputSchema::UnionOf, Models::AnthropicBeta, Models::Beta::BetaBashCodeExecutionToolResultBlock::Content, Models::Beta::BetaBashCodeExecutionToolResultBlockParam::Content, Models::Beta::BetaCitationsDelta::Citation, Models::Beta::BetaCodeExecutionToolResultBlockContent, Models::Beta::BetaCodeExecutionToolResultBlockParamContent, Models::Beta::BetaContentBlock, Models::Beta::BetaContentBlockParam, Models::Beta::BetaContentBlockSource::Content, Models::Beta::BetaContentBlockSourceContent, Models::Beta::BetaImageBlockParam::Source, Models::Beta::BetaMCPToolResultBlock::Content, Models::Beta::BetaMessageParam::Content, Models::Beta::BetaRawContentBlockDelta, Models::Beta::BetaRawContentBlockStartEvent::ContentBlock, Models::Beta::BetaRawMessageStreamEvent, Models::Beta::BetaRequestDocumentBlock::Source, Models::Beta::BetaRequestMCPToolResultBlockParam::Content, Models::Beta::BetaTextCitation, Models::Beta::BetaTextCitationParam, Models::Beta::BetaTextEditorCodeExecutionToolResultBlock::Content, Models::Beta::BetaTextEditorCodeExecutionToolResultBlockParam::Content, Models::Beta::BetaThinkingConfigParam, Models::Beta::BetaToolChoice, Models::Beta::BetaToolResultBlockParam::Content, Models::Beta::BetaToolResultBlockParam::Content::Content, Models::Beta::BetaToolUnion, Models::Beta::BetaWebSearchToolResultBlockContent, Models::Beta::BetaWebSearchToolResultBlockParamContent, Models::Beta::MessageCountTokensParams::System, Models::Beta::MessageCountTokensParams::Tool, Models::Beta::MessageCreateParams::System, Models::Beta::Messages::BatchCreateParams::Request::Params::System, Models::Beta::Messages::BetaMessageBatchResult, Models::BetaError, Models::CitationsDelta::Citation, Models::ContentBlock, Models::ContentBlockParam, Models::ContentBlockSource::Content, Models::ContentBlockSourceContent, Models::DocumentBlockParam::Source, Models::ErrorObject, Models::ImageBlockParam::Source, Models::MessageCountTokensParams::System, Models::MessageCountTokensTool, Models::MessageCreateParams::System, Models::MessageParam::Content, Models::Messages::BatchCreateParams::Request::Params::System, Models::Messages::MessageBatchResult, Models::Model, Models::RawContentBlockDelta, Models::RawContentBlockStartEvent::ContentBlock, Models::RawMessageStreamEvent, Models::TextCitation, Models::TextCitationParam, Models::ThinkingConfigParam, Models::ToolChoice, Models::ToolResultBlockParam::Content, Models::ToolResultBlockParam::Content::Content, Models::ToolUnion, Models::WebSearchToolResultBlockContent, Models::WebSearchToolResultBlockParamContent
Defined in:
lib/anthropic/internal/type/union.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Examples:

# `raw_message_stream_event` is a `Anthropic::RawMessageStreamEvent`
case raw_message_stream_event
when Anthropic::RawMessageStartEvent
  puts(raw_message_stream_event.message)
when Anthropic::RawMessageDeltaEvent
  puts(raw_message_stream_event.delta)
when Anthropic::RawMessageStopEvent
  puts(raw_message_stream_event.type)
else
  puts(raw_message_stream_event)
end
case raw_message_stream_event
in {type: :message_start, message: message}
  puts(message)
in {type: :message_delta, delta: delta, usage: usage}
  puts(delta)
in {type: :message_stop}
  # ...
else
  puts(raw_message_stream_event)
end

Instance Method Summary collapse

Methods included from Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, to_sorbet_type

Methods included from Converter

coerce, dump, inspect, meta_info, new_coerce_state, type_info

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



134
135
136
# File 'lib/anthropic/internal/type/union.rb', line 134

def ==(other)
  Anthropic::Internal::Type::Union === other && other.derefed_variants == derefed_variants
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



123
124
125
126
127
# File 'lib/anthropic/internal/type/union.rb', line 123

def ===(other)
  known_variants.any? do |_, variant_fn|
    variant_fn.call === other
  end
end

#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.

Tries to efficiently coerce the given value to one of the known variants.

If the value cannot match any of the known variants, the coercion is considered non-viable and returns the original value.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class<StandardError>] :error

    @option state [Integer] :branched

Returns:

  • (Object)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/anthropic/internal/type/union.rb', line 165

def coerce(value, state:)
  if (target = resolve_variant(value))
    return Anthropic::Internal::Type::Converter.coerce(target, value, state: state)
  end

  strictness = state.fetch(:strictness)
  exactness = state.fetch(:exactness)

  alternatives = []
  known_variants.each do |_, variant_fn|
    target = variant_fn.call
    exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
    state[:branched] += 1

    coerced = Anthropic::Internal::Type::Converter.coerce(target, value, state: state)
    yes, no, maybe = exact.values
    if (no + maybe).zero? || (!strictness && yes.positive?)
      exact.each { exactness[_1] += _2 }
      state[:exactness] = exactness
      return coerced
    elsif maybe.positive?
      alternatives << [[-yes, -maybe, no], exact, coerced]
    end
  end

  case alternatives.sort_by!(&:first)
  in []
    exactness[:no] += 1
    state[:error] = ArgumentError.new("no matching variant for #{value.inspect}")
    value
  in [[_, exact, coerced], *]
    exact.each { exactness[_1] += _2 }
    coerced
  end
    .tap { state[:exactness] = exactness }
ensure
  state[:strictness] = strictness
end

#dump(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=>Object})

    .

    @option state [Boolean] :can_retry

Returns:

  • (Object)


213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/anthropic/internal/type/union.rb', line 213

def dump(value, state:)
  if (target = resolve_variant(value))
    return Anthropic::Internal::Type::Converter.dump(target, value, state: state)
  end

  known_variants.each do
    target = _2.call
    return Anthropic::Internal::Type::Converter.dump(target, value, state: state) if target === value
  end

  super
end

#hashInteger

Returns:

  • (Integer)


141
# File 'lib/anthropic/internal/type/union.rb', line 141

def hash = variants.hash

#inspect(depth: 0) ⇒ String

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:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


249
250
251
252
253
254
255
256
257
258
# File 'lib/anthropic/internal/type/union.rb', line 249

def inspect(depth: 0)
  if depth.positive?
    return is_a?(Module) ? super() : self.class.name
  end

  members = variants.map { Anthropic::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
  prefix = is_a?(Module) ? name : self.class.name

  "#{prefix}[#{members.join(' | ')}]"
end

#to_sorbet_typeObject

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.

Returns:

  • (Object)


229
230
231
232
233
234
235
236
237
238
239
# File 'lib/anthropic/internal/type/union.rb', line 229

def to_sorbet_type
  types = variants.map { Anthropic::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
  case types
  in []
    T.noreturn
  in [type]
    type
  else
    T.any(*types)
  end
end

#variantsArray<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.

All of the specified variants for this union.

Returns:

  • (Array<Object>)


53
# File 'lib/anthropic/internal/type/union.rb', line 53

def variants = derefed_variants.map { _2 }