Class: Braintrust::Contrib::RubyOpenAI::ResponsesPatcher

Inherits:
Patcher
  • Object
show all
Defined in:
lib/braintrust/contrib/ruby_openai/patcher.rb

Overview

Patcher for ruby-openai responses API. Instruments OpenAI::Responses#create method.

Class Method Summary collapse

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 49

def applicable?
  defined?(::OpenAI::Client) && ::OpenAI::Client.method_defined?(:responses)
end

.patched?(**options) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 53

def patched?(**options)
  if options[:target]
    responses_obj = options[:target].responses
    Instrumentation::Responses.applied?(responses_obj.singleton_class)
  else
    # For class-level, check if the responses class is patched
    defined?(::OpenAI::Responses) && Instrumentation::Responses.applied?(::OpenAI::Responses)
  end
end

.perform_patch(**options) ⇒ void

This method returns an undefined value.

Perform the actual patching.

Parameters:

  • options (Hash)

    Configuration options passed from integration

Options Hash (**options):

  • :target (Object)

    Optional target instance to patch

  • :tracer_provider (OpenTelemetry::SDK::Trace::TracerProvider)

    Optional tracer provider



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 68

def perform_patch(**options)
  return unless applicable?

  if options[:target]
    # Instance-level (for only this client)
    raise ArgumentError, "target must be a kind of ::OpenAI::Client" unless options[:target].is_a?(::OpenAI::Client)

    responses_obj = options[:target].responses
    responses_obj.singleton_class.include(Instrumentation::Responses)
  else
    # Class-level (for all clients)
    ::OpenAI::Responses.include(Instrumentation::Responses)
  end
end