Module: Braintrust::Contrib::OpenAI::Instrumentation::ResponseStream::InstanceMethods

Defined in:
lib/braintrust/contrib/openai/instrumentation/responses.rb

Instance Method Summary collapse

Instance Method Details

#each(&block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/braintrust/contrib/openai/instrumentation/responses.rb', line 128

def each(&block)
  ctx = Braintrust::Contrib::Context.from(self)
  return super unless ctx&.[](:tracer) && !ctx[:consumed]

  ctx[:consumed] = true

  tracer = ctx[:tracer]
  params = ctx[:params]
   = ctx[:metadata]
  responses_instance = ctx[:responses_instance]
  aggregated_events = []
  start_time = Braintrust::Internal::Time.measure
  time_to_first_token = nil

  tracer.in_span("openai.responses.create") do |span|
    responses_instance.send(:set_input, span, params)
    Support::OTel.set_json_attr(span, "braintrust.metadata", )

    begin
      super do |event|
        time_to_first_token ||= Braintrust::Internal::Time.measure(start_time)
        aggregated_events << event
        block&.call(event)
      end
    rescue => e
      span.record_exception(e)
      span.status = ::OpenTelemetry::Trace::Status.error("Streaming error: #{e.message}")
      raise
    end

    finalize_stream_span(span, aggregated_events, time_to_first_token, )
  end
end