Class: Anthropic::Internal::Type::BaseModel Abstract
- Inherits:
-
Object
- Object
- Anthropic::Internal::Type::BaseModel
- Extended by:
- Converter, Util::SorbetRuntimeSupport
- Defined in:
- lib/anthropic/internal/type/base_model.rb
Overview
Direct Known Subclasses
Helpers::InputSchema::BaseModel, Helpers::Streaming::CitationEvent, Helpers::Streaming::InputJsonEvent, Helpers::Streaming::SignatureEvent, Helpers::Streaming::TextEvent, Helpers::Streaming::ThinkingEvent, Models::APIErrorObject, Models::AuthenticationError, Models::Base64ImageSource, Models::Base64PDFSource, Models::Beta::BetaBase64ImageSource, Models::Beta::BetaBase64PDFSource, Models::Beta::BetaBashCodeExecutionOutputBlock, Models::Beta::BetaBashCodeExecutionOutputBlockParam, Models::Beta::BetaBashCodeExecutionResultBlock, Models::Beta::BetaBashCodeExecutionResultBlockParam, Models::Beta::BetaBashCodeExecutionToolResultBlock, Models::Beta::BetaBashCodeExecutionToolResultBlockParam, Models::Beta::BetaBashCodeExecutionToolResultError, Models::Beta::BetaBashCodeExecutionToolResultErrorParam, Models::Beta::BetaCacheControlEphemeral, Models::Beta::BetaCacheCreation, Models::Beta::BetaCitationCharLocation, Models::Beta::BetaCitationCharLocationParam, Models::Beta::BetaCitationConfig, Models::Beta::BetaCitationContentBlockLocation, Models::Beta::BetaCitationContentBlockLocationParam, Models::Beta::BetaCitationPageLocation, Models::Beta::BetaCitationPageLocationParam, Models::Beta::BetaCitationSearchResultLocation, Models::Beta::BetaCitationSearchResultLocationParam, Models::Beta::BetaCitationWebSearchResultLocationParam, Models::Beta::BetaCitationsConfigParam, Models::Beta::BetaCitationsDelta, Models::Beta::BetaCitationsWebSearchResultLocation, Models::Beta::BetaCodeExecutionOutputBlock, Models::Beta::BetaCodeExecutionOutputBlockParam, Models::Beta::BetaCodeExecutionResultBlock, Models::Beta::BetaCodeExecutionResultBlockParam, Models::Beta::BetaCodeExecutionTool20250522, Models::Beta::BetaCodeExecutionTool20250825, Models::Beta::BetaCodeExecutionToolResultBlock, Models::Beta::BetaCodeExecutionToolResultBlockParam, Models::Beta::BetaCodeExecutionToolResultError, Models::Beta::BetaCodeExecutionToolResultErrorParam, Models::Beta::BetaContainer, Models::Beta::BetaContainerUploadBlock, Models::Beta::BetaContainerUploadBlockParam, Models::Beta::BetaContentBlockSource, Models::Beta::BetaDocumentBlock, Models::Beta::BetaFileDocumentSource, Models::Beta::BetaFileImageSource, Models::Beta::BetaImageBlockParam, Models::Beta::BetaInputJSONDelta, Models::Beta::BetaMCPToolResultBlock, Models::Beta::BetaMCPToolUseBlock, Models::Beta::BetaMCPToolUseBlockParam, Models::Beta::BetaMessage, Models::Beta::BetaMessageDeltaUsage, Models::Beta::BetaMessageParam, Models::Beta::BetaMessageTokensCount, Models::Beta::BetaMetadata, Models::Beta::BetaModelInfo, Models::Beta::BetaPlainTextSource, Models::Beta::BetaRawContentBlockDeltaEvent, Models::Beta::BetaRawContentBlockStartEvent, Models::Beta::BetaRawContentBlockStopEvent, Models::Beta::BetaRawMessageDeltaEvent, Models::Beta::BetaRawMessageDeltaEvent::Delta, Models::Beta::BetaRawMessageStartEvent, Models::Beta::BetaRawMessageStopEvent, Models::Beta::BetaRedactedThinkingBlock, Models::Beta::BetaRedactedThinkingBlockParam, Models::Beta::BetaRequestDocumentBlock, Models::Beta::BetaRequestMCPServerToolConfiguration, Models::Beta::BetaRequestMCPServerURLDefinition, Models::Beta::BetaRequestMCPToolResultBlockParam, Models::Beta::BetaSearchResultBlockParam, Models::Beta::BetaServerToolUsage, Models::Beta::BetaServerToolUseBlock, Models::Beta::BetaServerToolUseBlockParam, Models::Beta::BetaSignatureDelta, Models::Beta::BetaTextBlock, Models::Beta::BetaTextBlockParam, Models::Beta::BetaTextDelta, Models::Beta::BetaTextEditorCodeExecutionCreateResultBlock, Models::Beta::BetaTextEditorCodeExecutionCreateResultBlockParam, Models::Beta::BetaTextEditorCodeExecutionStrReplaceResultBlock, Models::Beta::BetaTextEditorCodeExecutionStrReplaceResultBlockParam, Models::Beta::BetaTextEditorCodeExecutionToolResultBlock, Models::Beta::BetaTextEditorCodeExecutionToolResultBlockParam, Models::Beta::BetaTextEditorCodeExecutionToolResultError, Models::Beta::BetaTextEditorCodeExecutionToolResultErrorParam, Models::Beta::BetaTextEditorCodeExecutionViewResultBlock, Models::Beta::BetaTextEditorCodeExecutionViewResultBlockParam, Models::Beta::BetaThinkingBlock, Models::Beta::BetaThinkingBlockParam, Models::Beta::BetaThinkingConfigDisabled, Models::Beta::BetaThinkingConfigEnabled, Models::Beta::BetaThinkingDelta, Models::Beta::BetaTool, Models::Beta::BetaTool::InputSchema, Models::Beta::BetaToolBash20241022, Models::Beta::BetaToolBash20250124, Models::Beta::BetaToolChoiceAny, Models::Beta::BetaToolChoiceAuto, Models::Beta::BetaToolChoiceNone, Models::Beta::BetaToolChoiceTool, Models::Beta::BetaToolComputerUse20241022, Models::Beta::BetaToolComputerUse20250124, Models::Beta::BetaToolResultBlockParam, Models::Beta::BetaToolTextEditor20241022, Models::Beta::BetaToolTextEditor20250124, Models::Beta::BetaToolTextEditor20250429, Models::Beta::BetaToolTextEditor20250728, Models::Beta::BetaToolUseBlock, Models::Beta::BetaToolUseBlockParam, Models::Beta::BetaURLImageSource, Models::Beta::BetaURLPDFSource, Models::Beta::BetaUsage, Models::Beta::BetaWebFetchBlock, Models::Beta::BetaWebFetchBlockParam, Models::Beta::BetaWebFetchTool20250910, Models::Beta::BetaWebFetchToolResultBlock, Models::Beta::BetaWebFetchToolResultBlockParam, Models::Beta::BetaWebFetchToolResultErrorBlock, Models::Beta::BetaWebFetchToolResultErrorBlockParam, Models::Beta::BetaWebSearchResultBlock, Models::Beta::BetaWebSearchResultBlockParam, Models::Beta::BetaWebSearchTool20250305, Models::Beta::BetaWebSearchTool20250305::UserLocation, Models::Beta::BetaWebSearchToolRequestError, Models::Beta::BetaWebSearchToolResultBlock, Models::Beta::BetaWebSearchToolResultBlockParam, Models::Beta::BetaWebSearchToolResultError, Models::Beta::DeletedFile, Models::Beta::FileDeleteParams, Models::Beta::FileDownloadParams, Models::Beta::FileListParams, Models::Beta::FileMetadata, Models::Beta::FileRetrieveMetadataParams, Models::Beta::FileUploadParams, Models::Beta::MessageCountTokensParams, Models::Beta::MessageCreateParams, Models::Beta::Messages::BatchCancelParams, Models::Beta::Messages::BatchCreateParams, Models::Beta::Messages::BatchCreateParams::Request, Models::Beta::Messages::BatchCreateParams::Request::Params, Models::Beta::Messages::BatchDeleteParams, Models::Beta::Messages::BatchListParams, Models::Beta::Messages::BatchResultsParams, Models::Beta::Messages::BatchRetrieveParams, Models::Beta::Messages::BetaDeletedMessageBatch, Models::Beta::Messages::BetaMessageBatch, Models::Beta::Messages::BetaMessageBatchCanceledResult, Models::Beta::Messages::BetaMessageBatchErroredResult, Models::Beta::Messages::BetaMessageBatchExpiredResult, Models::Beta::Messages::BetaMessageBatchIndividualResponse, Models::Beta::Messages::BetaMessageBatchRequestCounts, Models::Beta::Messages::BetaMessageBatchSucceededResult, Models::Beta::ModelListParams, Models::Beta::ModelRetrieveParams, Models::BetaAPIError, Models::BetaAuthenticationError, Models::BetaBillingError, Models::BetaErrorResponse, Models::BetaGatewayTimeoutError, Models::BetaInvalidRequestError, Models::BetaNotFoundError, Models::BetaOverloadedError, Models::BetaPermissionError, Models::BetaRateLimitError, Models::BillingError, Models::CacheControlEphemeral, Models::CacheCreation, Models::CitationCharLocation, Models::CitationCharLocationParam, Models::CitationContentBlockLocation, Models::CitationContentBlockLocationParam, Models::CitationPageLocation, Models::CitationPageLocationParam, Models::CitationSearchResultLocationParam, Models::CitationWebSearchResultLocationParam, Models::CitationsConfigParam, Models::CitationsDelta, Models::CitationsSearchResultLocation, Models::CitationsWebSearchResultLocation, Models::Completion, Models::CompletionCreateParams, Models::ContentBlockSource, Models::DocumentBlockParam, Models::ErrorResponse, Models::GatewayTimeoutError, Models::ImageBlockParam, Models::InputJSONDelta, Models::InvalidRequestError, Models::Message, Models::MessageCountTokensParams, Models::MessageCreateParams, Models::MessageDeltaUsage, Models::MessageParam, Models::MessageTokensCount, Models::Messages::BatchCancelParams, Models::Messages::BatchCreateParams, Models::Messages::BatchCreateParams::Request, Models::Messages::BatchCreateParams::Request::Params, Models::Messages::BatchDeleteParams, Models::Messages::BatchListParams, Models::Messages::BatchResultsParams, Models::Messages::BatchRetrieveParams, Models::Messages::DeletedMessageBatch, Models::Messages::MessageBatch, Models::Messages::MessageBatchCanceledResult, Models::Messages::MessageBatchErroredResult, Models::Messages::MessageBatchExpiredResult, Models::Messages::MessageBatchIndividualResponse, Models::Messages::MessageBatchRequestCounts, Models::Messages::MessageBatchSucceededResult, Models::Metadata, Models::ModelInfo, Models::ModelListParams, Models::ModelRetrieveParams, Models::NotFoundError, Models::OverloadedError, Models::PermissionError, Models::PlainTextSource, Models::RateLimitError, Models::RawContentBlockDeltaEvent, Models::RawContentBlockStartEvent, Models::RawContentBlockStopEvent, Models::RawMessageDeltaEvent, Models::RawMessageDeltaEvent::Delta, Models::RawMessageStartEvent, Models::RawMessageStopEvent, Models::RedactedThinkingBlock, Models::RedactedThinkingBlockParam, Models::SearchResultBlockParam, Models::ServerToolUsage, Models::ServerToolUseBlock, Models::ServerToolUseBlockParam, Models::SignatureDelta, Models::TextBlock, Models::TextBlockParam, Models::TextDelta, Models::ThinkingBlock, Models::ThinkingBlockParam, Models::ThinkingConfigDisabled, Models::ThinkingConfigEnabled, Models::ThinkingDelta, Models::Tool, Models::Tool::InputSchema, Models::ToolBash20250124, Models::ToolChoiceAny, Models::ToolChoiceAuto, Models::ToolChoiceNone, Models::ToolChoiceTool, Models::ToolResultBlockParam, Models::ToolTextEditor20250124, Models::ToolTextEditor20250429, Models::ToolTextEditor20250728, Models::ToolUseBlock, Models::ToolUseBlockParam, Models::URLImageSource, Models::URLPDFSource, Models::Usage, Models::WebSearchResultBlock, Models::WebSearchResultBlockParam, Models::WebSearchTool20250305, Models::WebSearchTool20250305::UserLocation, Models::WebSearchToolRequestError, Models::WebSearchToolResultBlock, Models::WebSearchToolResultBlockParam, Models::WebSearchToolResultError, RequestOptions
Class Method Summary collapse
- .==(other) ⇒ Boolean
- .coerce(value, state:) ⇒ self, Object private
- .dump(value, state:) ⇒ Hash{Object=>Object}, Object private
- .fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}} private
- .hash ⇒ Integer
-
.inherited(child) ⇒ Object
private
Assumes superclass fields are totally defined before fields are accessed / defined on subclasses.
- .inspect(depth: 0) ⇒ String private
- .known_fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}} private
- .optional(name_sym, type_info, spec = {}) ⇒ Object private
- .recursively_to_h(model, convert:) ⇒ Hash{Symbol=>Object} private
- .required(name_sym, type_info, spec = {}) ⇒ Object private
- .to_sorbet_type ⇒ Object private
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#[](key) ⇒ Object?
Returns the raw value associated with the given key, if found.
- #deconstruct_keys(keys) ⇒ Hash{Symbol=>Object}
-
#deep_to_h ⇒ Hash{Symbol=>Object}
In addition to the behaviour of ‘#to_h`, this method will recursively call `#to_h` on nested models.
- #hash ⇒ Integer
-
#initialize(data = {}) ⇒ BaseModel
constructor
Create a new instance of a model.
- #inspect ⇒ String private
-
#to_h ⇒ Hash{Symbol=>Object}
(also: #to_hash)
Returns a Hash of the data underlying this object.
- #to_json(*a) ⇒ String
- #to_s ⇒ String
- #to_yaml(*a) ⇒ String
Methods included from Converter
coerce, dump, meta_info, new_coerce_state, type_info
Methods included from Util::SorbetRuntimeSupport
const_missing, define_sorbet_constant!, sorbet_constant_defined?, to_sorbet_type
Constructor Details
#initialize(data = {}) ⇒ BaseModel
Create a new instance of a model.
475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/anthropic/internal/type/base_model.rb', line 475 def initialize(data = {}) @data = {} @coerced = {} Anthropic::Internal::Util.coerce_hash!(data).each do if self.class.known_fields.key?(_1) public_send(:"#{_1}=", _2) else @data.store(_1, _2) @coerced.store(_1, false) end end end |
Class Method Details
.==(other) ⇒ Boolean
207 208 209 |
# File 'lib/anthropic/internal/type/base_model.rb', line 207 def ==(other) other.is_a?(Class) && other <= Anthropic::Internal::Type::BaseModel && other.fields == fields end |
.coerce(value, state:) ⇒ self, 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.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/anthropic/internal/type/base_model.rb', line 247 def coerce(value, state:) exactness = state.fetch(:exactness) if value.is_a?(self) exactness[:yes] += 1 return value end unless (val = Anthropic::Internal::Util.coerce_hash(value)).is_a?(Hash) exactness[:no] += 1 state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}") return value end exactness[:yes] += 1 keys = val.keys.to_set instance = new data = instance.to_h status = instance.instance_variable_get(:@coerced) # rubocop:disable Metrics/BlockLength fields.each do |name, field| mode, required, target = field.fetch_values(:mode, :required, :type) api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const) src_name = state.fetch(:translate_names) ? api_name : name unless val.key?(src_name) if required && mode != :dump && const == Anthropic::Internal::OMIT exactness[nilable ? :maybe : :no] += 1 else exactness[:yes] += 1 end next end item = val.fetch(src_name) keys.delete(src_name) state[:error] = nil converted = if item.nil? && (nilable || !required) exactness[nilable ? :yes : :maybe] += 1 nil else coerced = Anthropic::Internal::Type::Converter.coerce(target, item, state: state) case target in Anthropic::Internal::Type::Converter | Symbol coerced else item end end status.store(name, state.fetch(:error) || true) data.store(name, converted) end # rubocop:enable Metrics/BlockLength keys.each { data.store(_1, val.fetch(_1)) } instance end |
.dump(value, state:) ⇒ Hash{Object=>Object}, 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.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/anthropic/internal/type/base_model.rb', line 318 def dump(value, state:) unless (coerced = Anthropic::Internal::Util.coerce_hash(value)).is_a?(Hash) return super end acc = {} coerced.each do |key, val| name = key.is_a?(String) ? key.to_sym : key case (field = known_fields[name]) in nil acc.store(name, super(val, state: state)) else api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn) case mode in :coerce next else target = type_fn.call acc.store(api_name, Anthropic::Internal::Type::Converter.dump(target, val, state: state)) end end end known_fields.each_value do |field| api_name, mode, const = field.fetch_values(:api_name, :mode, :const) next if mode == :coerce || acc.key?(api_name) || const == Anthropic::Internal::OMIT acc.store(api_name, const) end acc end |
.fields ⇒ Hash{Symbol=>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.
31 32 33 34 35 |
# File 'lib/anthropic/internal/type/base_model.rb', line 31 def fields known_fields.transform_values do |field| {**field.except(:type_fn), type: field.fetch(:type_fn).call} end end |
.hash ⇒ Integer
214 |
# File 'lib/anthropic/internal/type/base_model.rb', line 214 def hash = fields.hash |
.inherited(child) ⇒ 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.
Assumes superclass fields are totally defined before fields are accessed / defined on subclasses.
18 19 20 21 |
# File 'lib/anthropic/internal/type/base_model.rb', line 18 def inherited(child) super child.known_fields.replace(known_fields.dup) end |
.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.
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/anthropic/internal/type/base_model.rb', line 494 def inspect(depth: 0) return super() if depth.positive? depth = depth.succ deferred = fields.transform_values do |field| type, required, nilable = field.fetch_values(:type, :required, :nilable) inspected = [ Anthropic::Internal::Type::Converter.inspect(type, depth: depth), !required || nilable ? "nil" : nil ].compact.join(" | ") -> { inspected }.tap { _1.define_singleton_method(:inspect) { call } } end "#{name}[#{deferred.inspect}]" end |
.known_fields ⇒ Hash{Symbol=>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.
26 |
# File 'lib/anthropic/internal/type/base_model.rb', line 26 def known_fields = @known_fields ||= {} |
.optional(name_sym, type_info, spec = {}) ⇒ 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.
173 174 175 |
# File 'lib/anthropic/internal/type/base_model.rb', line 173 def optional(name_sym, type_info, spec = {}) add_field(name_sym, required: false, type_info: type_info, spec: spec) end |
.recursively_to_h(model, convert:) ⇒ 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.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/anthropic/internal/type/base_model.rb', line 366 def recursively_to_h(model, convert:) rec = ->(x) do case x in Anthropic::Internal::Type::BaseModel if convert fields = x.class.known_fields x.to_h.to_h do |key, val| [key, rec.call(fields.key?(key) ? x.public_send(key) : val)] rescue Anthropic::Errors::ConversionError [key, rec.call(val)] end else rec.call(x.to_h) end in Hash x.transform_values(&rec) in Array x.map(&rec) else x end end rec.call(model) end |
.required(name_sym, type_info, spec = {}) ⇒ 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.
154 155 156 |
# File 'lib/anthropic/internal/type/base_model.rb', line 154 def required(name_sym, type_info, spec = {}) add_field(name_sym, required: true, type_info: type_info, spec: spec) end |
.to_sorbet_type ⇒ 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.
354 355 356 |
# File 'lib/anthropic/internal/type/base_model.rb', line 354 def to_sorbet_type self end |
Instance Method Details
#==(other) ⇒ Boolean
222 |
# File 'lib/anthropic/internal/type/base_model.rb', line 222 def ==(other) = self.class == other.class && @data == other.to_h |
#[](key) ⇒ Object?
Returns the raw value associated with the given key, if found. Otherwise, nil is returned.
It is valid to lookup keys that are not in the API spec, for example to access undocumented features. This method does not parse response data into higher-level types. Lookup by anything other than a Symbol is an ArgumentError.
404 405 406 407 408 409 410 |
# File 'lib/anthropic/internal/type/base_model.rb', line 404 def [](key) unless key.instance_of?(Symbol) raise ArgumentError.new("Expected symbol key for lookup, got #{key.inspect}") end @data[key] end |
#deconstruct_keys(keys) ⇒ Hash{Symbol=>Object}
446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/anthropic/internal/type/base_model.rb', line 446 def deconstruct_keys(keys) (keys || self.class.known_fields.keys) .filter_map do |k| unless self.class.known_fields.key?(k) next end [k, public_send(k)] end .to_h end |
#deep_to_h ⇒ Hash{Symbol=>Object}
In addition to the behaviour of ‘#to_h`, this method will recursively call `#to_h` on nested models.
434 |
# File 'lib/anthropic/internal/type/base_model.rb', line 434 def deep_to_h = self.class.recursively_to_h(@data, convert: false) |
#hash ⇒ Integer
227 |
# File 'lib/anthropic/internal/type/base_model.rb', line 227 def hash = [self.class, @data].hash |
#inspect ⇒ 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.
519 520 521 522 |
# File 'lib/anthropic/internal/type/base_model.rb', line 519 def inspect converted = self.class.recursively_to_h(self, convert: true) "#<#{self.class}:0x#{object_id.to_s(16)} #{converted}>" end |
#to_h ⇒ Hash{Symbol=>Object} Also known as: to_hash
Returns a Hash of the data underlying this object. O(1)
Keys are Symbols and values are the raw values from the response. The return value indicates which values were ever set on the object. i.e. there will be a key in this hash if they ever were, even if the set value was nil.
This method is not recursive. The returned value is shared by the object, so it should not be mutated.
424 |
# File 'lib/anthropic/internal/type/base_model.rb', line 424 def to_h = @data |
#to_json(*a) ⇒ String
463 |
# File 'lib/anthropic/internal/type/base_model.rb', line 463 def to_json(*a) = Anthropic::Internal::Type::Converter.dump(self.class, self).to_json(*a) |
#to_s ⇒ String
514 |
# File 'lib/anthropic/internal/type/base_model.rb', line 514 def to_s = deep_to_h.to_s |