struct

Anthropic::Message

Inherits JSON::Serializable < Struct < Value < Object

Message struct for API responses

Content blocks are typed - use pattern matching or type checks:

message.content.each do |block|
  case block
  when TextContent
    puts block.text
  when ToolUseContent
    puts "Tool: #{block.name}"
  when ThinkingContent
    puts "Thinking: #{block.thinking}"
  end
end

Constructors

new(*, __pull_for_json_serializable pull : JSON::PullParser)
Source

Instance methods

container
Source
container_id
Source
content
Source
diagnostics
Source
model
Source
parsed_output

Get parsed output for structured outputs

When using structured outputs, Claude returns JSON in the text content. This method parses that JSON into a JSON::Any for easy access.

output = message.parsed_output
puts output["summary"].as_s
puts output["score"].as_f
Source
parsed_output_as(type : T.class) : T | Nil forall T

Parse structured output into a typed Crystal value

Source
parsed_output_as!(type : T.class) : T forall T

Parse structured output into a typed Crystal value, raising on failure

Source
redacted_thinking_blocks

Extract redacted thinking blocks (typed)

Source
refusal?

True when stop_reason == "refusal".

Source
refusal_stop_details

Returns the refusal stop details when the model refused the request.

Equivalent to a typed downcast of stop_details when its variant is RefusalStopDetails. Returns nil for any other (or absent) stop_details variant.

Source
role
Source
stop_details
Source
stop_reason
Source
stop_sequence
Source
structured_output?

Check if the response contains structured output

Source
text

Get combined text from all text blocks

Returns empty string if no text blocks exist.

puts message.text
Source
text_blocks

Extract text blocks (typed)

Source
thinking_blocks

Extract thinking blocks (typed)

Source
tool_use?

Check if tool use is requested

Source
tool_use_blocks

Extract tool use blocks (typed)

Source
type
Source
usage
Source