Module: RubyLLM::Providers::OpenAIResponses::Streaming
- Included in:
- RubyLLM::Providers::OpenAIResponses
- Defined in:
- lib/ruby_llm/providers/openai_responses/streaming.rb
Overview
Streaming methods for the OpenAI Responses API. Handles SSE events with typed event format.
Class Method Summary collapse
-
.build_chunk(data) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity.
- .build_streaming_tool_call(data) ⇒ Object
- .parse_streaming_error(data) ⇒ Object
- .stream_url ⇒ Object
Class Method Details
.build_chunk(data) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 15 def build_chunk(data) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity event_type = data['type'] case event_type when 'response.output_text.delta' # Text content delta Chunk.new( role: :assistant, content: data['delta'], model_id: data.dig('response', 'model') ) when 'response.function_call_arguments.delta' # Function call arguments streaming Chunk.new( role: :assistant, content: nil, tool_calls: build_streaming_tool_call(data), model_id: data.dig('response', 'model') ) when 'response.completed' # Final response with usage stats and any server-side built-in # tool activity (web_search_call, code_interpreter_call, etc.) that # the model executed. StreamAccumulatorExtension forwards # built_in_tool_events onto the assembled Message. response_data = data['response'] || {} usage = response_data['usage'] || {} cached_tokens = usage.dig('input_tokens_details', 'cached_tokens') built_in_events = BuiltInTools.extract_events(response_data['output'] || []) Chunk.new( role: :assistant, content: nil, input_tokens: usage['input_tokens'], output_tokens: usage['output_tokens'], cached_tokens: cached_tokens, cache_creation_tokens: 0, model_id: response_data['model'], response_id: response_data['id'], built_in_tool_events: built_in_events.empty? ? nil : built_in_events ) when 'response.output_item.added' # New output item started (function call, message, etc.) item = data['item'] || {} if item['type'] == 'function_call' stream_key = item['id'] || item['call_id'] Chunk.new( role: :assistant, content: nil, tool_calls: { stream_key => ToolCall.new( id: item['call_id'], name: item['name'], arguments: '' ) } ) else # Other item types - return empty chunk Chunk.new(role: :assistant, content: nil) end when 'response.content_part.added', 'response.content_part.done', 'response.output_item.done', 'response.output_text.done', 'response.function_call_arguments.done', 'response.created', 'response.in_progress' # Status events - return empty chunk Chunk.new(role: :assistant, content: nil) when 'error' # Error event error_data = data['error'] || {} raise RubyLLM::Error.new(nil, error_data['message'] || 'Unknown streaming error') else # Unknown event type - return empty chunk Chunk.new(role: :assistant, content: nil) end end |
.build_streaming_tool_call(data) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 97 def build_streaming_tool_call(data) call_id = data['call_id'] || data['item_id'] return nil unless call_id stream_key = data['item_id'] stream_key ||= data['call_id'] if data['call_id']&.start_with?('call_') # Argument delta events usually carry an item_id, while the final # function call id lives on output_item.added. When only an unmapped # id is available, use nil so RubyLLM 1.16 appends to the latest call. { stream_key => ToolCall.new( id: data['name'] ? call_id : nil, name: data['name'], arguments: data['delta'] || '' ) } end |
.parse_streaming_error(data) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 116 def parse_streaming_error(data) error_data = JSON.parse(data) return unless error_data['error'] || error_data['type'] == 'error' error = error_data['error'] || error_data error_type = error['type'] || error['code'] = error['message'] case error_type when 'server_error', 'internal_error' [500, ] when 'rate_limit_exceeded', 'insufficient_quota' [429, ] when 'invalid_request_error', 'invalid_api_key' [400, ] else [400, ] end rescue JSON::ParserError [500, data] end |
.stream_url ⇒ Object
11 12 13 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 11 def stream_url 'responses' end |