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

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
90
91
92
93
94
# 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 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'
      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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 96

def build_streaming_tool_call(data)
  call_id = data['call_id'] || data['item_id']
  return nil unless call_id

  # Argument delta events don't carry a tool name — only the initial
  # output_item.added event does. Omit `id` on nameless deltas so
  # StreamAccumulator appends arguments to the latest tool call
  # instead of creating a new entry that overwrites the named one.
  {
    call_id => ToolCall.new(
      id: data['name'] ? call_id : nil,
      name: data['name'],
      arguments: data['delta'] || ''
    )
  }
end

.parse_streaming_error(data) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 113

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 = error['message']

  case error_type
  when 'server_error', 'internal_error'
    [500, error_message]
  when 'rate_limit_exceeded', 'insufficient_quota'
    [429, error_message]
  when 'invalid_request_error', 'invalid_api_key'
    [400, error_message]
  else
    [400, error_message]
  end
rescue JSON::ParserError
  [500, data]
end

.stream_urlObject



11
12
13
# File 'lib/ruby_llm/providers/openai_responses/streaming.rb', line 11

def stream_url
  'responses'
end