struct

OpenRouter::Message

Inherits JSON::Serializable < Struct < Value < Object

Constructors

new(role : Role, content : Content | Nil, name : String | Nil = nil, tool_call_id : String | Nil = nil, tool_calls : Array(ToolCall) | Nil = nil, reasoning : String | Nil = nil, reasoning_details : JSON::Any | Nil = nil, images : Array(JSON::Any) | Nil = nil)

Initialize for user/assistant/system messages

Source
new(pull : JSON::PullParser)

Override JSON::Serializable's generated self.new(pull) so that response deserialization routes through our custom from_json(JSON::Any), which correctly handles the nested image_url/video_url format from the API.

Source
new(tool_call : ToolCall)

Create a tool result message.

Role will be Role::Tool @name will be set to tool_call.name @tool_call_id will be set to tool_call.id @content will be set to serializd string of tool_call.arguments.to_json()

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

Class methods

from_json(json : JSON::Any)
Source

Instance methods

add_tool_call(tool_call : ToolCall)
Source
add_tool_call(name : String, arguments : Array(ToolCallArgument) = [] of ToolCallArgument, tool_call_type : String = "function", tool_call_id : String | Nil = nil)
Source
content
Source
content=(content : Content | Nil)
Source
content_string

Get the content, or in case of a multi-modal message, the content of the first part.

Source
image_urls

Returns image URLs from the message — either from a separate images field (some models) or from image_url content parts (others).

Source
images

Generated images returned by image generation models. Each entry is an object, typically {"url": "data:image/png;base64,..."}.

Source
images=(images : Array(JSON::Any) | Nil)

Generated images returned by image generation models. Each entry is an object, typically {"url": "data:image/png;base64,..."}.

Source
length
Source
multi_modal?

Check if content is an Array

Source
name
Source
name=(name : String | Nil)
Source
reasoning

Reasoning tokens - visible reasoning trace from the model

Source
reasoning=(reasoning : String | Nil)

Reasoning tokens - visible reasoning trace from the model

Source
reasoning_details

Reasoning details for preserving reasoning blocks (used in tool calling)

Source
reasoning_details=(reasoning_details : JSON::Any | Nil)

Reasoning details for preserving reasoning blocks (used in tool calling)

Source
role

Common fields

Source
role=(role : Role | Nil)

Common fields

Source
to_json(json : JSON::Builder)

Custom serialization for complex multi-modal content and tool calls

Source
tool_call_id

Optional fields for specific roles

Source
tool_call_id=(tool_call_id : String | Nil)

Optional fields for specific roles

Source
tool_calls
Source
tool_calls=(tool_calls : Array(ToolCall) | Nil)
Source