Skip to content

ToolResultMessage

Result from tool execution.

Class: RobotLab::ToolResultMessage

tool = ToolMessage.new(id: "call_abc123", name: "get_weather", input: { city: "NYC" })

message = ToolResultMessage.new(
  tool: tool,
  content: { data: { temperature: 72, conditions: "sunny" } }
)

Constructor

ToolResultMessage.new(tool:, content:, stop_reason: nil)

Parameters:

Name Type Description
tool ToolMessage The tool call that was executed
content Hash Result with :data key (success) or :error key (failure)
stop_reason String, nil Stop reason (defaults to "tool")

Attributes

tool

message.tool  # => ToolMessage

The ToolMessage representing the tool call that produced this result. Provides access to tool.id, tool.name, and tool.input.

content

message.content  # => Hash

The result content. Contains either a :data key (success) or an :error key (failure).

role

message.role  # => "tool_result"

Always returns "tool_result".

type

message.type  # => "tool_result"

Always returns "tool_result".

stop_reason

message.stop_reason  # => "tool"

Defaults to "tool".

Methods

success?

message.success?  # => Boolean

Returns true if the content contains a :data key.

error?

message.error?  # => Boolean

Returns true if the content contains an :error key.

data

message.data  # => Object | nil

Returns the result data if successful, nil otherwise.

error

message.error  # => String | nil

Returns the error message if there was an error, nil otherwise.

to_h

message.to_h  # => Hash

Hash representation.

Returns:

{
  type: "tool_result",
  role: "tool_result",
  tool: { type: "tool", id: "call_abc123", name: "get_weather", input: { city: "NYC" } },
  content: { data: { temperature: 72, conditions: "sunny" } },
  stop_reason: "tool"
}

to_json

message.to_json  # => String

JSON representation.

Predicates

message.tool_result?  # => true
message.tool_call?    # => false
message.text?         # => false
message.tool_stop?    # => true

Examples

Successful Result

tool = ToolMessage.new(id: "call_1", name: "search_orders", input: { user_id: "123" })

result = ToolResultMessage.new(
  tool: tool,
  content: { data: { order_id: "ord_123", status: "shipped" } }
)

result.success?  # => true
result.data      # => { order_id: "ord_123", status: "shipped" }

Error Result

tool = ToolMessage.new(id: "call_order", name: "get_order", input: { id: "bad" })

result = ToolResultMessage.new(
  tool: tool,
  content: { error: "Order not found" }
)

result.error?  # => true
result.error   # => "Order not found"
result.data    # => nil

Accessing Tool Information

result = ToolResultMessage.new(
  tool: ToolMessage.new(id: "call_1", name: "get_weather", input: { city: "Berlin" }),
  content: { data: { temperature: 15, unit: "celsius" } }
)

result.tool.name   # => "get_weather"
result.tool.id     # => "call_1"
result.tool.input  # => { city: "Berlin" }
result.data        # => { temperature: 15, unit: "celsius" }

Matching Tool Calls with Results

# Given a ToolCallMessage and its results
tool_call_msg.tools.each do |tool|
  # Find the matching result
  matching_result = results.find { |r| r.tool.id == tool.id }

  if matching_result&.success?
    puts "#{tool.name}(#{tool.input}) => #{matching_result.data}"
  elsif matching_result&.error?
    puts "#{tool.name} failed: #{matching_result.error}"
  end
end

In Memory History

# Find all tool results from memory
tool_results = memory.messages.select(&:tool_result?)

tool_results.each do |tr|
  if tr.success?
    puts "#{tr.tool.name}: #{tr.data}"
  else
    puts "#{tr.tool.name} error: #{tr.error}"
  end
end

See Also