Class: Anthropic::Models::Message

Inherits:
Internal::Type::BaseModel show all
Defined in:
lib/anthropic/models/message.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Internal::Type::BaseModel

==, #==, #[], coerce, #deconstruct_keys, #deep_to_h, dump, fields, hash, #hash, inherited, inspect, #inspect, known_fields, optional, recursively_to_h, required, #to_h, #to_json, #to_s, to_sorbet_type, #to_yaml

Methods included from Internal::Type::Converter

#coerce, coerce, #dump, dump, #inspect, inspect, meta_info, new_coerce_state, type_info

Methods included from Internal::Util::SorbetRuntimeSupport

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

Constructor Details

#initialize(id:, container:, content:, model:, stop_details:, stop_reason:, stop_sequence:, usage:, role: :assistant, type: :message) ⇒ Object

Some parameter documentations has been truncated, see Anthropic::Models::Message for more details.

Parameters:



# File 'lib/anthropic/models/message.rb', line 152

Instance Attribute Details

#containerAnthropic::Models::Container?

Information about the container used in the request (for the code execution tool)



22
# File 'lib/anthropic/models/message.rb', line 22

required :container, -> { Anthropic::Container }, nil?: true

#contentArray<Anthropic::Models::TextBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock, Anthropic::Models::WebFetchToolResultBlock, Anthropic::Models::CodeExecutionToolResultBlock, Anthropic::Models::BashCodeExecutionToolResultBlock, Anthropic::Models::TextEditorCodeExecutionToolResultBlock, Anthropic::Models::ToolSearchToolResultBlock, Anthropic::Models::ContainerUploadBlock>

Content generated by the model.

This is an array of content blocks, each of which has a ‘type` that determines its shape.

Example:

“‘json

{ “type”: “text”, “text”: “Hi, I’m Claude.” }

“‘

If the request input ‘messages` ended with an `assistant` turn, then the response `content` will continue directly from that last turn. You can use this to constrain the model’s output.

For example, if the input ‘messages` were:

“‘json [

{
  "role": "user",
  "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
},
{ "role": "assistant", "content": "The best answer is (" }

] “‘

Then the response ‘content` might be:

“‘json

{ “type”: “text”, “text”: “B)” }

“‘



59
# File 'lib/anthropic/models/message.rb', line 59

required :content, -> { Anthropic::Internal::Type::ArrayOf[union: Anthropic::ContentBlock] }

#idString

Unique object identifier.

The format and length of IDs may change over time.

Returns:

  • (String)


15
# File 'lib/anthropic/models/message.rb', line 15

required :id, String

#modelSymbol, ...

The model that will complete your prompt.nnSee [models](docs.anthropic.com/en/docs/models-overview) for additional details and options.

Returns:



79
# File 'lib/anthropic/models/message.rb', line 79

required :model, union: -> { Anthropic::Model }

#roleSymbol, :assistant

Conversational role of the generated message.

This will always be ‘“assistant”`.

Returns:

  • (Symbol, :assistant)


87
# File 'lib/anthropic/models/message.rb', line 87

required :role, const: :assistant

#stop_detailsAnthropic::Models::RefusalStopDetails?

Structured information about a refusal.



93
# File 'lib/anthropic/models/message.rb', line 93

required :stop_details, -> { Anthropic::RefusalStopDetails }, nil?: true

#stop_reasonSymbol, ...

The reason that we stopped.

This may be one the following values:

  • ‘“end_turn”`: the model reached a natural stopping point

  • ‘“max_tokens”`: we exceeded the requested `max_tokens` or the model’s maximum

  • ‘“stop_sequence”`: one of your provided custom `stop_sequences` was generated

  • ‘“tool_use”`: the model invoked one or more tools

  • ‘“pause_turn”`: we paused a long-running turn. You may provide the response back as-is in a subsequent request to let the model continue.

  • ‘“refusal”`: when streaming classifiers intervene to handle potential policy violations

In non-streaming mode this value is always non-null. In streaming mode, it is null in the ‘message_start` event and non-null otherwise.

Returns:



113
# File 'lib/anthropic/models/message.rb', line 113

required :stop_reason, enum: -> { Anthropic::StopReason }, nil?: true

#stop_sequenceString?

Which custom stop sequence was generated, if any.

This value will be a non-null string if one of your custom stop sequences was generated.

Returns:

  • (String, nil)


122
# File 'lib/anthropic/models/message.rb', line 122

required :stop_sequence, String, nil?: true

#typeSymbol, :message

Object type.

For Messages, this is always ‘“message”`.

Returns:

  • (Symbol, :message)


130
# File 'lib/anthropic/models/message.rb', line 130

required :type, const: :message

#usageAnthropic::Models::Usage

Billing and rate-limit usage.

Anthropic’s API bills and rate-limits by token counts, as tokens represent the underlying cost to our systems.

Under the hood, the API transforms requests into a format suitable for the model. The model’s output then goes through a parsing stage before becoming an API response. As a result, the token counts in ‘usage` will not match one-to-one with the exact visible content of an API request or response.

For example, ‘output_tokens` will be non-zero, even for an empty string response from Claude.

Total input tokens in a request is the summation of ‘input_tokens`, `cache_creation_input_tokens`, and `cache_read_input_tokens`.



150
# File 'lib/anthropic/models/message.rb', line 150

required :usage, -> { Anthropic::Usage }

Instance Method Details

#parsed_outputObject?

Parsed value of response when a JSON output schema object has been specified via :output_config

Returns:

  • (Object, nil)


64
65
66
67
68
69
70
71
# File 'lib/anthropic/models/message.rb', line 64

def parsed_output
  case content
  in [*, {parsed:}]
    parsed
  else
    nil
  end
end