Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,95 @@
"method": "stream",
"span_name": "anthropic.chat",
},
# Regular messages with_raw_response methods
{
"package": "anthropic.resources.messages.messages",
"object": "MessagesWithRawResponse",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.messages.messages",
"object": "AsyncMessagesWithRawResponse",
"method": "create",
"span_name": "anthropic.chat",
},
# Beta API methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API with_raw_response methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "MessagesWithRawResponse",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessagesWithRawResponse",
"method": "create",
"span_name": "anthropic.chat",
},
# Beta API methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API with_raw_response methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "MessagesWithRawResponse",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessagesWithRawResponse",
"method": "create",
"span_name": "anthropic.chat",
},
]

WRAPPED_AMETHODS = [
Expand Down Expand Up @@ -130,8 +219,8 @@ async def _aset_token_usage(
token_histogram: Histogram = None,
choice_counter: Counter = None,
):
if not isinstance(response, dict):
response = response.__dict__
from opentelemetry.instrumentation.anthropic.utils import _extract_response_data
response = _extract_response_data(response)

if usage := response.get("usage"):
prompt_tokens = usage.input_tokens
Expand Down Expand Up @@ -223,8 +312,8 @@ def _set_token_usage(
token_histogram: Histogram = None,
choice_counter: Counter = None,
):
if not isinstance(response, dict):
response = response.__dict__
from opentelemetry.instrumentation.anthropic.utils import _extract_response_data
response = _extract_response_data(response)

if usage := response.get("usage"):
prompt_tokens = usage.input_tokens
Expand Down Expand Up @@ -710,7 +799,9 @@ def _instrument(self, **kwargs):
wrapped_method,
),
)
except ModuleNotFoundError:
logger.debug(f"Successfully wrapped {wrap_package}.{wrap_object}.{wrap_method}")
except Exception as e:
logger.debug(f"Failed to wrap {wrap_package}.{wrap_object}.{wrap_method}: {e}")
pass # that's ok, we don't want to fail if some methods do not exist

for wrapped_method in WRAPPED_AMETHODS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
dont_throw,
model_as_dict,
should_send_prompts,
_extract_response_data,
)
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
GEN_AI_RESPONSE_ID,
Expand Down Expand Up @@ -170,6 +171,7 @@ def _set_span_completions(span, response):
return
from opentelemetry.instrumentation.anthropic import set_span_attribute

response = _extract_response_data(response)
index = 0
prefix = f"{SpanAttributes.LLM_COMPLETIONS}.{index}"
set_span_attribute(span, f"{prefix}.finish_reason", response.get("stop_reason"))
Expand Down Expand Up @@ -236,8 +238,7 @@ def _set_span_completions(span, response):
def set_response_attributes(span, response):
from opentelemetry.instrumentation.anthropic import set_span_attribute

if not isinstance(response, dict):
response = response.__dict__
response = _extract_response_data(response)
set_span_attribute(span, SpanAttributes.LLM_RESPONSE_MODEL, response.get("model"))
set_span_attribute(span, GEN_AI_RESPONSE_ID, response.get("id"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,35 @@ def _handle_exception(e, func, logger):
return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper


def _extract_response_data(response):
"""Extract the actual response data from both regular and with_raw_response wrapped responses."""
if isinstance(response, dict):
return response

# Handle with_raw_response wrapped responses
if hasattr(response, 'parse') and callable(response.parse):
try:
# For with_raw_response, parse() gives us the actual response object
parsed_response = response.parse()
if not isinstance(parsed_response, dict):
parsed_response = parsed_response.__dict__
return parsed_response
except Exception as e:
import logging
logger = logging.getLogger(__name__)
logger.debug(f"Failed to parse response: {e}, response type: {type(response)}")

# Fallback to __dict__ for regular response objects
if hasattr(response, '__dict__'):
response_dict = response.__dict__
return response_dict

return {}


@dont_throw
def shared_metrics_attributes(response):
if not isinstance(response, dict):
response = response.__dict__
response = _extract_response_data(response)

common_attributes = Config.get_common_metrics_attributes()

Expand Down
Loading
Loading