Class: OpenRouter::ResponsesResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/open_router/responses_response.rb

Overview

Response wrapper for the Responses API Beta (/api/v1/responses) This API differs from chat completions in its response structure, using an ‘output` array with typed items instead of `choices`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ ResponsesResponse

Returns a new instance of ResponsesResponse.



10
11
12
# File 'lib/open_router/responses_response.rb', line 10

def initialize(raw)
  @raw = raw || {}
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



8
9
10
# File 'lib/open_router/responses_response.rb', line 8

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object

Hash-like access for raw data



174
175
176
# File 'lib/open_router/responses_response.rb', line 174

def [](key)
  raw[key]
end

#build_follow_up_input(original_input:, tool_results:, follow_up_message: nil) ⇒ Array

Build a follow-up input array that includes tool results Use this to continue the conversation after executing tools

Examples:

# First call with tools
response = client.responses("What's the weather?", model: "...", tools: [...])

# Execute tools
results = response.execute_tool_calls { |name, args| ... }

# Build follow-up input
next_input = response.build_follow_up_input(
  original_input: "What's the weather?",
  tool_results: results,
  follow_up_message: "Is that good for a picnic?"
)

# Continue conversation
next_response = client.responses(next_input, model: "...")

Parameters:

  • original_input (String, Array)

    The original input sent to the API

  • tool_results (Array<ResponsesToolResult>)

    Results from execute_tool_calls

  • follow_up_message (String, nil) (defaults to: nil)

    Optional follow-up user message

Returns:

  • (Array)

    Input array for the next API call



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/open_router/responses_response.rb', line 117

def build_follow_up_input(original_input:, tool_results:, follow_up_message: nil)
  input_items = []

  # Add original user message
  if original_input.is_a?(String)
    input_items << {
      "type" => "message",
      "role" => "user",
      "content" => [{ "type" => "input_text", "text" => original_input }]
    }
  elsif original_input.is_a?(Array)
    input_items.concat(original_input)
  end

  # Add function calls from this response
  tool_calls_raw.each do |tc|
    input_items << tc
  end

  # Add function call outputs
  tool_results.each do |result|
    input_items << result.to_input_item
  end

  # Add assistant message if present
  input_items << message_output if message_output

  # Add follow-up user message if provided
  if follow_up_message
    input_items << {
      "type" => "message",
      "role" => "user",
      "content" => [{ "type" => "input_text", "text" => follow_up_message }]
    }
  end

  input_items
end

#contentObject

Convenience method to get the assistant’s text content



41
42
43
# File 'lib/open_router/responses_response.rb', line 41

def content
  message_output&.dig("content", 0, "text")
end

#created_atObject



28
29
30
# File 'lib/open_router/responses_response.rb', line 28

def created_at
  raw["created_at"]
end

#dig(*keys) ⇒ Object



178
179
180
# File 'lib/open_router/responses_response.rb', line 178

def dig(*keys)
  raw.dig(*keys)
end

#execute_tool_calls {|name, arguments| ... } ⇒ Array<ResponsesToolResult>

Execute all tool calls and return results

Examples:

results = response.execute_tool_calls do |name, args|
  case name
  when "get_weather" then fetch_weather(args["location"])
  when "search" then search_web(args["query"])
  end
end

Yields:

  • (name, arguments)

    Block to execute each tool

Returns:



89
90
91
# File 'lib/open_router/responses_response.rb', line 89

def execute_tool_calls(&block)
  tool_calls.map { |tc| tc.execute(&block) }
end

#idObject

Core accessors



15
16
17
# File 'lib/open_router/responses_response.rb', line 15

def id
  raw["id"]
end

#input_tokensObject

Token counts



157
158
159
# File 'lib/open_router/responses_response.rb', line 157

def input_tokens
  usage["input_tokens"] || 0
end

#modelObject



24
25
26
# File 'lib/open_router/responses_response.rb', line 24

def model
  raw["model"]
end

#outputObject



32
33
34
# File 'lib/open_router/responses_response.rb', line 32

def output
  raw["output"] || []
end

#output_tokensObject



161
162
163
# File 'lib/open_router/responses_response.rb', line 161

def output_tokens
  usage["output_tokens"] || 0
end

#reasoning?Boolean Also known as: has_reasoning?

Check if reasoning was included in the response

Returns:

  • (Boolean)


51
52
53
# File 'lib/open_router/responses_response.rb', line 51

def reasoning?
  !reasoning_output.nil?
end

#reasoning_summaryObject

Get reasoning summary steps (array of strings)



46
47
48
# File 'lib/open_router/responses_response.rb', line 46

def reasoning_summary
  reasoning_output&.dig("summary") || []
end

#reasoning_tokensObject



169
170
171
# File 'lib/open_router/responses_response.rb', line 169

def reasoning_tokens
  usage.dig("output_tokens_details", "reasoning_tokens") || 0
end

#statusObject



19
20
21
22
# File 'lib/open_router/responses_response.rb', line 19

def status
  # Status can be at top level or derived from message output
  raw["status"] || message_output&.dig("status")
end

#tool_callsArray<ResponsesToolCall>

Get tool/function calls from the response as ResponsesToolCall objects

Returns:



59
60
61
62
63
# File 'lib/open_router/responses_response.rb', line 59

def tool_calls
  @tool_calls ||= output
                  .select { |o| o["type"] == "function_call" }
                  .map { |tc| ResponsesToolCall.new(tc) }
end

#tool_calls?Boolean Also known as: has_tool_calls?

Returns:

  • (Boolean)


72
73
74
# File 'lib/open_router/responses_response.rb', line 72

def tool_calls?
  tool_calls.any?
end

#tool_calls_rawArray<Hash>

Get raw tool call data (hashes) from the response

Returns:

  • (Array<Hash>)

    Array of raw tool call hashes



68
69
70
# File 'lib/open_router/responses_response.rb', line 68

def tool_calls_raw
  output.select { |o| o["type"] == "function_call" }
end

#total_tokensObject



165
166
167
# File 'lib/open_router/responses_response.rb', line 165

def total_tokens
  usage["total_tokens"] || 0
end

#usageObject



36
37
38
# File 'lib/open_router/responses_response.rb', line 36

def usage
  raw["usage"] || {}
end