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 : JSON::PullParser)
Sourcenew(*, __pull_for_json_serializable pull : JSON::PullParser)
SourceInstance methods
container
Sourcecontainer_id
Sourcecontent
Sourcediagnostics
Sourceid
Sourcemodel
Sourceparsed_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
parsed_output_as(type : T.class) : T | Nil forall T
Parse structured output into a typed Crystal value
parsed_output_as!(type : T.class) : T forall T
Parse structured output into a typed Crystal value, raising on failure
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.
role
Sourcestop_details
Sourcestop_reason
Sourcestop_sequence
Sourcetext
Get combined text from all text blocks
Returns empty string if no text blocks exist.
puts message.text
type
Sourceusage
Source