Skip to content
Merged
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
42 changes: 29 additions & 13 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
except ImportError:
raise DidNotEnable("Anthropic not installed")

try:
from anthropic.types import ThinkingBlock, ThinkingDelta
except ImportError:
ThinkingBlock = None # type: ignore[assignment,misc]
ThinkingDelta = None # type: ignore[assignment,misc]

if TYPE_CHECKING:
from typing import (
Any,
Expand Down Expand Up @@ -93,7 +99,7 @@ class _PatchedRawMessageStream(Stream[Any]):

_model: Optional[ModelParam]
_usage: "_RecordedUsage"
_content_blocks: list[Any]
_content_blocks: "list[dict[str, Any]]"
_response_id: Optional[str]
_finish_reason: Optional[str]

Expand All @@ -103,7 +109,7 @@ class _PatchedMessageStream(MessageStream):

_model: Optional[ModelParam]
_usage: "_RecordedUsage"
_content_blocks: list[Any]
_content_blocks: "list[dict[str, Any]]"
_response_id: Optional[str]
_finish_reason: Optional[str]

Expand All @@ -113,7 +119,7 @@ class _PatchedRawAsyncMessageStream(AsyncStream[Any]):

_model: Optional[ModelParam]
_usage: "_RecordedUsage"
_content_blocks: list[Any]
_content_blocks: "list[dict[str, Any]]"
_response_id: Optional[str]
_finish_reason: Optional[str]

Expand All @@ -123,7 +129,7 @@ class _PatchedAsyncMessageStream(AsyncMessageStream):

_model: Optional[ModelParam]
_usage: "_RecordedUsage"
_content_blocks: list[Any]
_content_blocks: "list[dict[str, Any]]"
_response_id: Optional[str]
_finish_reason: Optional[str]

Expand Down Expand Up @@ -365,22 +371,32 @@ def _collect_ai_data(
event: "MessageStreamEvent",
model: "str | None",
usage: "_RecordedUsage",
content_blocks: "list[str]",
content_blocks: "list[dict[str, Any]]",
response_id: "str | None" = None,
finish_reason: "str | None" = None,
) -> "tuple[str | None, _RecordedUsage, list[str], str | None, str | None]":
) -> "tuple[str | None, _RecordedUsage, list[dict[str, Any]], str | None, str | None]":
"""
Collect model information, token usage, and collect content blocks from the AI streaming response.
"""
with capture_internal_exceptions():
if hasattr(event, "type"):
if event.type == "content_block_start":
pass
if ThinkingBlock is not None and isinstance(
event.content_block, ThinkingBlock
):
content_blocks.append({"type": "thinking", "thinking": ""})
elif event.content_block.type in ("text", "tool_use"):
content_blocks.append({"type": "text", "text": ""})
else:
content_blocks.append({"type": event.content_block.type})
elif event.type == "content_block_delta":
if hasattr(event.delta, "text"):
content_blocks.append(event.delta.text)
block = content_blocks[event.index]
if ThinkingDelta is not None and isinstance(event.delta, ThinkingDelta):
block["thinking"] += event.delta.thinking
elif hasattr(event.delta, "text"):
block["text"] += event.delta.text
elif hasattr(event.delta, "partial_json"):
content_blocks.append(event.delta.partial_json)
block["text"] += event.delta.partial_json
elif event.type == "content_block_stop":
pass

Expand Down Expand Up @@ -707,7 +723,7 @@ def _set_output_data(
output_tokens: "int | None",
cache_read_input_tokens: "int | None",
cache_write_input_tokens: "int | None",
content_blocks: "list[Any]",
content_blocks: "list[dict[str, Any]]",
response_id: "str | None" = None,
finish_reason: "str | None" = None,
) -> None:
Expand Down Expand Up @@ -1042,7 +1058,7 @@ def _set_streaming_output_data(
integration: "AnthropicIntegration",
model: "Optional[str]",
usage: "_RecordedUsage",
content_blocks: "list[str]",
content_blocks: "list[dict[str, Any]]",
response_id: "Optional[str]",
finish_reason: "Optional[str]",
) -> None:
Expand All @@ -1065,7 +1081,7 @@ def _set_streaming_output_data(
output_tokens=usage.output_tokens,
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"}],
content_blocks=content_blocks,
response_id=response_id,
finish_reason=finish_reason,
)
Expand Down
Loading
Loading