Class: OpenAI::Helpers::Streaming::ResponseEventDecoder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/helpers/streaming/response_event_decoder.rb,
sig/openai/helpers/streaming/response_event_decoder.rbs

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

Instance Method Summary collapse

Constructor Details

#initialize(model:) ⇒ ResponseEventDecoder

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 a new instance of ResponseEventDecoder.

Parameters:

  • model: (Object)


10
11
12
13
14
15
16
17
18
# File 'lib/openai/helpers/streaming/response_event_decoder.rb', line 10

def initialize(model:)
  @model = model
  return unless response_stream_model?

  @known_types = model.variants.to_h do |variant|
    type = variant.known_fields.fetch(:type).fetch(:const)
    [type.to_s, true]
  end
end

Instance Method Details

#decode(data) ⇒ 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:

  • data (Object)

Returns:

  • (Object)


20
21
22
23
24
25
26
# File 'lib/openai/helpers/streaming/response_event_decoder.rb', line 20

def decode(data)
  if unknown_response_event?(data)
    UnknownStreamEvent.new(data: data)
  else
    OpenAI::Internal::Type::Converter.coerce(@model, data)
  end
end

#response_stream_model?Boolean

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:



30
31
32
33
# File 'lib/openai/helpers/streaming/response_event_decoder.rb', line 30

def response_stream_model?
  @model.equal?(OpenAI::Models::Responses::ResponseStreamEvent) ||
    @model.equal?(OpenAI::Models::Beta::BetaResponseStreamEvent)
end

#unknown_response_event?(data) ⇒ Boolean

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:

  • data (Object)

Returns:



35
36
37
38
39
40
# File 'lib/openai/helpers/streaming/response_event_decoder.rb', line 35

def unknown_response_event?(data)
  return false unless @known_types && data.is_a?(Hash)

  type = data[:type]
  (type.is_a?(String) || type.is_a?(Symbol)) && !@known_types.key?(type.to_s)
end