Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 14 additions & 12 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,10 @@ def _wrap_synchronous_message_iterator(
_accumulate_event_data(stream, event)
yield event
finally:
exc_info = sys.exc_info()
with capture_internal_exceptions():
if hasattr(stream, "_span"):
_finish_streaming_span(
_set_streaming_output_data(
span=stream._span,
integration=stream._integration,
model=stream._model,
Expand All @@ -487,6 +488,7 @@ def _wrap_synchronous_message_iterator(
response_id=stream._response_id,
finish_reason=stream._finish_reason,
)
stream._span.__exit__(*exc_info)
del stream._span


Expand Down Expand Up @@ -519,9 +521,10 @@ async def _wrap_asynchronous_message_iterator(
_accumulate_event_data(stream, event)
yield event
finally:
exc_info = sys.exc_info()
with capture_internal_exceptions():
if hasattr(stream, "_span"):
_finish_streaming_span(
_set_streaming_output_data(
span=stream._span,
integration=stream._integration,
model=stream._model,
Expand All @@ -530,6 +533,7 @@ async def _wrap_asynchronous_message_iterator(
response_id=stream._response_id,
finish_reason=stream._finish_reason,
)
stream._span.__exit__(*exc_info)
del stream._span


Expand All @@ -542,7 +546,6 @@ def _set_output_data(
cache_read_input_tokens: "int | None",
cache_write_input_tokens: "int | None",
content_blocks: "list[Any]",
finish_span: bool = False,
response_id: "str | None" = None,
finish_reason: "str | None" = None,
) -> None:
Expand Down Expand Up @@ -586,9 +589,6 @@ def _set_output_data(
input_tokens_cache_write=cache_write_input_tokens,
)

if finish_span:
span.__exit__(None, None, None)


def _sentry_patched_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "Any":
integration = kwargs.pop("integration")
Expand Down Expand Up @@ -667,10 +667,10 @@ def _sentry_patched_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "A
cache_read_input_tokens=cache_read_input_tokens,
cache_write_input_tokens=cache_write_input_tokens,
content_blocks=content_blocks,
finish_span=True,
response_id=getattr(result, "id", None),
finish_reason=getattr(result, "stop_reason", None),
)
span.__exit__(None, None, None)
else:
span.set_data("unknown_response", True)
span.__exit__(None, None, None)
Expand Down Expand Up @@ -751,7 +751,7 @@ def _accumulate_event_data(
stream._finish_reason = finish_reason


def _finish_streaming_span(
def _set_streaming_output_data(
span: "Span",
integration: "AnthropicIntegration",
model: "Optional[str]",
Expand All @@ -761,7 +761,7 @@ def _finish_streaming_span(
finish_reason: "Optional[str]",
) -> None:
"""
Set output attributes on the AI Client Span and end the span.
Set output attributes on the AI Client Span.
"""
# Anthropic's input_tokens excludes cached/cache_write tokens.
# Normalize to total input tokens for correct cost calculations.
Expand All @@ -780,7 +780,6 @@ def _finish_streaming_span(
cache_read_input_tokens=usage.cache_read_input_tokens,
cache_write_input_tokens=usage.cache_write_input_tokens,
content_blocks=[{"text": "".join(content_blocks), "type": "text"}],
finish_span=True,
response_id=response_id,
finish_reason=finish_reason,
)
Expand All @@ -802,7 +801,7 @@ def close(self: "Union[Stream, MessageStream]") -> None:
del self._span
return f(self)

_finish_streaming_span(
_set_streaming_output_data(
span=self._span,
integration=self._integration,
model=self._model,
Expand All @@ -811,6 +810,8 @@ def close(self: "Union[Stream, MessageStream]") -> None:
response_id=self._response_id,
finish_reason=self._finish_reason,
)

self._span.__exit__(None, None, None)
del self._span

return f(self)
Expand Down Expand Up @@ -872,7 +873,7 @@ async def close(self: "AsyncStream") -> None:
del self._span
return await f(self)

_finish_streaming_span(
_set_streaming_output_data(
span=self._span,
integration=self._integration,
model=self._model,
Expand All @@ -881,6 +882,7 @@ async def close(self: "AsyncStream") -> None:
response_id=self._response_id,
finish_reason=self._finish_reason,
)
self._span.__exit__(None, None, None)
del self._span

return await f(self)
Expand Down
Loading
Loading