Module: OpenAI::Internal::Type::BaseStream Private

Includes:
Enumerable, Enumerable[Elem]
Included in:
Helpers::Streaming::ChatCompletionStream, Helpers::Streaming::ResponseStream, Stream
Defined in:
lib/openai/internal/type/base_stream.rb,
sig/openai/internal/type/base_stream.rbs

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

This module provides a base implementation for streaming responses in the SDK.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headersHash{String=>String} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash{String=>String})


20
# File 'lib/openai/internal/type/base_stream.rb', line 20

def headers = last_response.headers

#last_responseOpenAI::ResponseMetadata (readonly)

Metadata from the HTTP response that opened this stream.



27
28
29
# File 'lib/openai/internal/type/base_stream.rb', line 27

def last_response
  @last_response
end

#statusInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)


17
# File 'lib/openai/internal/type/base_stream.rb', line 17

def status = last_response.status

Instance Method Details

#closevoid

This method returns an undefined value.



32
# File 'lib/openai/internal/type/base_stream.rb', line 32

def close = OpenAI::Internal::Util.close_fused!(@iterator)

#each(&blk) {|, arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • blk (Proc)

Yields:

Yield Parameters:

  • (generic<Elem>)
  • arg0 (Elem)

Yield Returns:

  • (void)


73
74
75
76
77
78
79
# File 'lib/openai/internal/type/base_stream.rb', line 73

def each(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end

  @iterator.each(&blk)
end

#initialize(model:, url:, response_metadata:, response:, unwrap:, stream:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:



96
97
98
99
100
101
102
103
104
# File 'lib/openai/internal/type/base_stream.rb', line 96

def initialize(model:, url:, response_metadata:, response:, unwrap:, stream:)
  @model = model
  @url = url
  @last_response = 
  @response = response
  @unwrap = unwrap
  @stream = stream
  @iterator = iterator
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


109
110
111
112
113
# File 'lib/openai/internal/type/base_stream.rb', line 109

def inspect
  model = OpenAI::Internal::Type::Converter.inspect(@model, depth: 1)

  "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)}>"
end

#observe(context:, response:) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attach request lifecycle logging without changing the stream's identity.

Parameters:

Returns:

  • (self)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openai/internal/type/base_stream.rb', line 41

def observe(context:, response:)
  source = @iterator
  @iterator = OpenAI::Internal::Util.chain_fused(source) do |yielder|
    loop do
      event = begin
        source.next
      rescue StopIteration
        context.completed(response)
        break
      rescue StandardError => e
        context.request_failed(e)
        raise
      end

      yielder << event
    end
  end

  self
end

#to_enumEnumerator<generic<Elem>> Also known as: enum_for

Returns:

  • (Enumerator<generic<Elem>>)


84
# File 'lib/openai/internal/type/base_stream.rb', line 84

def to_enum = @iterator