Class: Braintrust::Contrib::OpenAI::ResponsesPatcher
- Defined in:
- lib/braintrust/contrib/openai/patcher.rb
Overview
Patcher for OpenAI integration - implements class-level patching. All new OpenAI::Client instances created after patch! will be automatically instrumented.
Class Method Summary collapse
- .applicable? ⇒ Boolean
- .patch_response_stream ⇒ Object
- .patched?(**options) ⇒ Boolean
-
.perform_patch(**options) ⇒ void
Perform the actual patching.
Methods inherited from Patcher
Class Method Details
.applicable? ⇒ Boolean
78 79 80 |
# File 'lib/braintrust/contrib/openai/patcher.rb', line 78 def applicable? defined?(::OpenAI::Client) && ::OpenAI::Client.instance_methods.include?(:responses) end |
.patch_response_stream ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/braintrust/contrib/openai/patcher.rb', line 113 def patch_response_stream # Patch ResponseStream for stream() method if defined?(::OpenAI::Helpers::Streaming::ResponseStream) unless Instrumentation::ResponseStream.applied?(::OpenAI::Helpers::Streaming::ResponseStream) ::OpenAI::Helpers::Streaming::ResponseStream.include(Instrumentation::ResponseStream) end end end |
.patched?(**options) ⇒ Boolean
82 83 84 85 86 87 |
# File 'lib/braintrust/contrib/openai/patcher.rb', line 82 def patched?(**) # Use the target's singleton class if provided, otherwise check the base class. target_class = get_singleton_class([:target]) || ::OpenAI::Resources::Responses Instrumentation::Responses.applied?(target_class) end |
.perform_patch(**options) ⇒ void
This method returns an undefined value.
Perform the actual patching.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/braintrust/contrib/openai/patcher.rb', line 94 def perform_patch(**) return unless applicable? # Stream class is shared across all clients, patch at class level. # The instrumentation short-circuits when no context is present, # so uninstrumented clients' streams pass through unaffected. patch_response_stream if [:target] # Instance-level (for only this client) raise ArgumentError, "target must be a kind of ::OpenAI::Client" unless [:target].is_a?(::OpenAI::Client) get_singleton_class([:target]).include(Instrumentation::Responses) else # Class-level (for all clients) ::OpenAI::Resources::Responses.include(Instrumentation::Responses) end end |