Skip to content

ToolResultMessage

Result from tool execution.

Class: RobotLab::ToolResultMessage

message = ToolResultMessage.new(
  id: "call_abc123",
  result: { temperature: 72, conditions: "sunny" }
)

Constructor

ToolResultMessage.new(id:, result:)

Parameters:

Name Type Description
id String Matching tool call ID
result Object Tool execution result

Attributes

id

message.id  # => String

Identifier matching the corresponding ToolCallMessage.

result

message.result  # => Object

The result returned by the tool. Can be any serializable object.

role

message.role  # => :tool

Always returns :tool.

Methods

to_h

message.to_h  # => Hash

Hash representation.

Returns:

{
  role: :tool,
  tool_result: {
    id: "call_abc123",
    result: { temperature: 72, conditions: "sunny" }
  }
}

to_json

message.to_json  # => String

JSON representation.

Examples

Basic Result

result = ToolResultMessage.new(
  id: "call_1",
  result: { success: true, order_id: "ord_123" }
)

String Result

result = ToolResultMessage.new(
  id: "call_time",
  result: "2024-01-15T10:30:00Z"
)

Array Result

result = ToolResultMessage.new(
  id: "call_search",
  result: [
    { id: 1, name: "Product A" },
    { id: 2, name: "Product B" }
  ]
)

Error Result

result = ToolResultMessage.new(
  id: "call_order",
  result: { success: false, error: "Order not found" }
)

Matching with Tool Calls

# Process all tool interactions
result.output.each_cons(2) do |a, b|
  if a.is_a?(ToolCallMessage) && b.is_a?(ToolResultMessage)
    if a.id == b.id
      puts "#{a.name}(#{a.input}) => #{b.result}"
    end
  end
end

In Result History

# Find all tool results from execution
tool_results = state.results
  .flat_map(&:output)
  .select { |m| m.is_a?(ToolResultMessage) }

tool_results.each do |tr|
  puts "Tool result: #{tr.result}"
end

See Also