Class: Braintrust::Contrib::Anthropic::BetaMessagesPatcher
- Defined in:
- lib/braintrust/contrib/anthropic/patcher.rb
Overview
Note:
Beta APIs are experimental and subject to change between SDK versions. Braintrust will make reasonable efforts to maintain compatibility, but breaking changes may require SDK updates.
Patcher for Anthropic beta messages API. Instruments client.beta.messages.create and stream methods.
Constant Summary collapse
- MAXIMUM_VERSION =
Version constraints for beta patcher. Set MAXIMUM_VERSION when a breaking change is discovered to disable beta instrumentation on incompatible versions until a fix is released. Currently nil = rely on class existence check only.
nil
Class Method Summary collapse
- .applicable? ⇒ Boolean
- .patched?(**options) ⇒ Boolean
-
.perform_patch(**options) ⇒ void
Perform the actual patching.
Methods inherited from Patcher
Class Method Details
.applicable? ⇒ Boolean
79 80 81 82 83 84 |
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 79 def applicable? return false unless defined?(::Anthropic::Client) return false unless defined?(::Anthropic::Resources::Beta::Messages) return false if MAXIMUM_VERSION && !version_compatible? true end |
.patched?(**options) ⇒ Boolean
86 87 88 89 |
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 86 def patched?(**) target_class = get_singleton_class([:target]) || ::Anthropic::Resources::Beta::Messages Instrumentation::BetaMessages.applied?(target_class) end |
.perform_patch(**options) ⇒ void
This method returns an undefined value.
Perform the actual patching.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 96 def perform_patch(**) return unless applicable? Braintrust::Log.debug("Instrumenting Anthropic beta.messages API (experimental)") # MessageStream is shared with stable API - already patched by MessagesPatcher. # The BetaMessages instrumentation sets api_version: "beta" in context, # which MessageStream uses to include in metadata. if [:target] # Instance-level (for only this client instance) raise ArgumentError, "target must be a kind of ::Anthropic::Client" unless [:target].is_a?(::Anthropic::Client) get_singleton_class([:target]).include(Instrumentation::BetaMessages) else # Class-level (for all client instances) ::Anthropic::Resources::Beta::Messages.include(Instrumentation::BetaMessages) end end |