Class: OpenRouter::ResponsesResponse
- Inherits:
-
Object
- Object
- OpenRouter::ResponsesResponse
- 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
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash-like access for raw data.
-
#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.
-
#content ⇒ Object
Convenience method to get the assistant’s text content.
- #created_at ⇒ Object
- #dig(*keys) ⇒ Object
-
#execute_tool_calls {|name, arguments| ... } ⇒ Array<ResponsesToolResult>
Execute all tool calls and return results.
-
#id ⇒ Object
Core accessors.
-
#initialize(raw) ⇒ ResponsesResponse
constructor
A new instance of ResponsesResponse.
-
#input_tokens ⇒ Object
Token counts.
- #model ⇒ Object
- #output ⇒ Object
- #output_tokens ⇒ Object
-
#reasoning? ⇒ Boolean
(also: #has_reasoning?)
Check if reasoning was included in the response.
-
#reasoning_summary ⇒ Object
Get reasoning summary steps (array of strings).
- #reasoning_tokens ⇒ Object
- #status ⇒ Object
-
#tool_calls ⇒ Array<ResponsesToolCall>
Get tool/function calls from the response as ResponsesToolCall objects.
- #tool_calls? ⇒ Boolean (also: #has_tool_calls?)
-
#tool_calls_raw ⇒ Array<Hash>
Get raw tool call data (hashes) from the response.
- #total_tokens ⇒ Object
- #usage ⇒ Object
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
#raw ⇒ Object (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
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 << if # Add follow-up user message if provided if input_items << { "type" => "message", "role" => "user", "content" => [{ "type" => "input_text", "text" => }] } end input_items end |
#content ⇒ Object
Convenience method to get the assistant’s text content
41 42 43 |
# File 'lib/open_router/responses_response.rb', line 41 def content &.dig("content", 0, "text") end |
#created_at ⇒ Object
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
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 |
#id ⇒ Object
Core accessors
15 16 17 |
# File 'lib/open_router/responses_response.rb', line 15 def id raw["id"] end |
#input_tokens ⇒ Object
Token counts
157 158 159 |
# File 'lib/open_router/responses_response.rb', line 157 def input_tokens usage["input_tokens"] || 0 end |
#model ⇒ Object
24 25 26 |
# File 'lib/open_router/responses_response.rb', line 24 def model raw["model"] end |
#output ⇒ Object
32 33 34 |
# File 'lib/open_router/responses_response.rb', line 32 def output raw["output"] || [] end |
#output_tokens ⇒ Object
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
51 52 53 |
# File 'lib/open_router/responses_response.rb', line 51 def reasoning? !reasoning_output.nil? end |
#reasoning_summary ⇒ Object
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_tokens ⇒ Object
169 170 171 |
# File 'lib/open_router/responses_response.rb', line 169 def reasoning_tokens usage.dig("output_tokens_details", "reasoning_tokens") || 0 end |
#status ⇒ Object
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"] || &.dig("status") end |
#tool_calls ⇒ Array<ResponsesToolCall>
Get tool/function calls from the response as ResponsesToolCall objects
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?
72 73 74 |
# File 'lib/open_router/responses_response.rb', line 72 def tool_calls? tool_calls.any? end |
#tool_calls_raw ⇒ Array<Hash>
Get raw tool call data (hashes) from the response
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_tokens ⇒ Object
165 166 167 |
# File 'lib/open_router/responses_response.rb', line 165 def total_tokens usage["total_tokens"] || 0 end |
#usage ⇒ Object
36 37 38 |
# File 'lib/open_router/responses_response.rb', line 36 def usage raw["usage"] || {} end |