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.
- .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
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 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 15 def build_chunk(data) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength 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 response_data = data['response'] || {} usage = response_data['usage'] || {} cached_tokens = usage.dig('input_tokens_details', 'cached_tokens') 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'] ) when 'response.output_item.added' # New output item started (function call, message, etc.) item = data['item'] || {} if item['type'] == 'function_call' Chunk.new( role: :assistant, content: nil, tool_calls: { item['call_id'] => 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
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 91 def build_streaming_tool_call(data) call_id = data['call_id'] || data['item_id'] return nil unless call_id { call_id => ToolCall.new( id: call_id, name: data['name'], arguments: data['delta'] || '' ) } end |
.parse_streaming_error(data) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 104 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 |