Class: Braintrust::Contrib::Anthropic::BetaMessagesPatcher

Inherits:
Patcher
  • Object
show all
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

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 86

def patched?(**options)
  target_class = get_singleton_class(options[: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.

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



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(**options)
  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.
  patch_message_stream

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

    get_singleton_class(options[:target]).include(Instrumentation::BetaMessages)
  else
    # Class-level (for all client instances)
    ::Anthropic::Resources::Beta::Messages.include(Instrumentation::BetaMessages)
  end
end