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
Models::APIErrorObject, Models::AuthenticationError, Models::Base64ImageSource, Models::Base64PDFSource, Models::Beta::BetaBase64ImageSource, Models::Beta::BetaBase64PDFBlock, Models::Beta::BetaBase64PDFSource, Models::Beta::BetaCacheControlEphemeral, Models::Beta::BetaCacheCreation, Models::Beta::BetaCitationCharLocation, Models::Beta::BetaCitationCharLocationParam, Models::Beta::BetaCitationContentBlockLocation, Models::Beta::BetaCitationContentBlockLocationParam, Models::Beta::BetaCitationPageLocation, Models::Beta::BetaCitationPageLocationParam, 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::BetaCodeExecutionToolResultBlock, Models::Beta::BetaCodeExecutionToolResultBlockParam, Models::Beta::BetaCodeExecutionToolResultError, Models::Beta::BetaCodeExecutionToolResultErrorParam, Models::Beta::BetaContainer, Models::Beta::BetaContainerUploadBlock, Models::Beta::BetaContainerUploadBlockParam, Models::Beta::BetaContentBlockSource, 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::BetaRequestMCPServerToolConfiguration, Models::Beta::BetaRequestMCPServerURLDefinition, Models::Beta::BetaRequestMCPToolResultBlockParam, Models::Beta::BetaServerToolUsage, Models::Beta::BetaServerToolUseBlock, Models::Beta::BetaServerToolUseBlockParam, Models::Beta::BetaSignatureDelta, Models::Beta::BetaTextBlock, Models::Beta::BetaTextBlockParam, Models::Beta::BetaTextDelta, 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::BetaToolUseBlock, Models::Beta::BetaToolUseBlockParam, Models::Beta::BetaURLImageSource, Models::Beta::BetaURLPDFSource, Models::Beta::BetaUsage, 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::CitationCharLocation, Models::CitationCharLocationParam, Models::CitationContentBlockLocation, Models::CitationContentBlockLocationParam, Models::CitationPageLocation, Models::CitationPageLocationParam, Models::CitationWebSearchResultLocationParam, Models::CitationsConfigParam, Models::CitationsDelta, 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::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::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
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.
437 |
# File 'lib/anthropic/internal/type/base_model.rb', line 437 def initialize(data = {}) = (@data = Anthropic::Internal::Util.coerce_hash!(data).to_h) |
Class Method Details
.==(other) ⇒ Boolean
179 180 181 |
# File 'lib/anthropic/internal/type/base_model.rb', line 179 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.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/anthropic/internal/type/base_model.rb', line 215 def coerce(value, state:) exactness = state.fetch(:exactness) if value.is_a?(self.class) exactness[:yes] += 1 return value end unless (val = Anthropic::Internal::Util.coerce_hash(value)).is_a?(Hash) exactness[:no] += 1 return value end exactness[:yes] += 1 keys = val.keys.to_set instance = new data = instance.to_h # 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) unless val.key?(api_name) if required && mode != :dump && const == Anthropic::Internal::OMIT exactness[nilable ? :maybe : :no] += 1 else exactness[:yes] += 1 end next end item = val.fetch(api_name) keys.delete(api_name) 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 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.
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 308 309 310 311 |
# File 'lib/anthropic/internal/type/base_model.rb', line 280 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
186 |
# File 'lib/anthropic/internal/type/base_model.rb', line 186 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.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/anthropic/internal/type/base_model.rb', line 445 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.
145 146 147 |
# File 'lib/anthropic/internal/type/base_model.rb', line 145 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.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/anthropic/internal/type/base_model.rb', line 328 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.
126 127 128 |
# File 'lib/anthropic/internal/type/base_model.rb', line 126 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.
316 317 318 |
# File 'lib/anthropic/internal/type/base_model.rb', line 316 def to_sorbet_type self end |
Instance Method Details
#==(other) ⇒ Boolean
194 |
# File 'lib/anthropic/internal/type/base_model.rb', line 194 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.
366 367 368 369 370 371 372 |
# File 'lib/anthropic/internal/type/base_model.rb', line 366 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}
408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/anthropic/internal/type/base_model.rb', line 408 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.
396 |
# File 'lib/anthropic/internal/type/base_model.rb', line 396 def deep_to_h = self.class.recursively_to_h(@data, convert: false) |
#hash ⇒ Integer
199 |
# File 'lib/anthropic/internal/type/base_model.rb', line 199 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.
470 471 472 473 |
# File 'lib/anthropic/internal/type/base_model.rb', line 470 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.
386 |
# File 'lib/anthropic/internal/type/base_model.rb', line 386 def to_h = @data |
#to_json(*a) ⇒ String
425 |
# File 'lib/anthropic/internal/type/base_model.rb', line 425 def to_json(*a) = Anthropic::Internal::Type::Converter.dump(self.class, self).to_json(*a) |
#to_s ⇒ String
465 |
# File 'lib/anthropic/internal/type/base_model.rb', line 465 def to_s = deep_to_h.to_s |