Module: Braintrust::Contrib::RubyLLM::Instrumentation::Chat::InstanceMethods

Defined in:
lib/braintrust/contrib/ruby_llm/instrumentation/chat.rb

Instance Method Summary collapse

Instance Method Details

#complete(&block) ⇒ Object

Wrap complete() to trace chat completions. Each call creates a span - recursive calls from tool execution create nested spans (each is a separate API call).



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/braintrust/contrib/ruby_llm/instrumentation/chat.rb', line 31

def complete(&block)
  return block ? super : super() unless tracing_enabled?

  tracer = Braintrust::Contrib.tracer_for(self)

  tracer.in_span("ruby_llm.chat") do |span|
    if block
      # Streaming: pass a block that calls super() with the wrapper
      handle_streaming_complete(span, block) do |&wrapper|
        super(&wrapper)
      end
    else
      # Non-streaming: pass a block that calls super()
      handle_non_streaming_complete(span) do
        super()
      end
    end
  end
end