Module: RubyLLM::Providers::OpenAI::Streaming

Included in:
RubyLLM::Providers::OpenAI
Defined in:
lib/ruby_llm/providers/openai/streaming.rb

Overview

Streaming methods of the OpenAI API integration

Class Method Summary collapse

Class Method Details

.build_chunk(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/providers/openai/streaming.rb', line 14

def build_chunk(data)
  usage = data['usage'] || {}
  cached_tokens = usage.dig('prompt_tokens_details', 'cached_tokens')
  delta = data.dig('choices', 0, 'delta') || {}
  content_source = delta['content'] || data.dig('choices', 0, 'message', 'content')
  content, thinking_from_blocks = OpenAI::Chat.extract_content_and_thinking(content_source)

  Chunk.new(
    role: :assistant,
    model_id: data['model'],
    content: content,
    thinking: Thinking.build(
      text: thinking_from_blocks || delta['reasoning_content'] || delta['reasoning'],
      signature: delta['reasoning_signature']
    ),
    tool_calls: parse_tool_calls(delta['tool_calls'], parse_arguments: false),
    input_tokens: usage['prompt_tokens'],
    output_tokens: usage['completion_tokens'],
    cached_tokens: cached_tokens,
    cache_creation_tokens: 0,
    thinking_tokens: usage.dig('completion_tokens_details', 'reasoning_tokens')
  )
end

.parse_streaming_error(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_llm/providers/openai/streaming.rb', line 38

def parse_streaming_error(data)
  error_data = JSON.parse(data)
  return unless error_data['error']

  case error_data.dig('error', 'type')
  when 'server_error'
    [500, error_data['error']['message']]
  when 'rate_limit_exceeded', 'insufficient_quota'
    [429, error_data['error']['message']]
  else
    [400, error_data['error']['message']]
  end
end

.stream_urlObject



10
11
12
# File 'lib/ruby_llm/providers/openai/streaming.rb', line 10

def stream_url
  completion_url
end