Skip to content

Commit 77eebbe

Browse files
Merge branch 'webb/anthropic/reasoning' into webb/anthropic/streaming-reasoning
2 parents dcfcd0c + d6f725b commit 77eebbe

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
MessageDeltaEvent,
5858
MessageStartEvent,
5959
MessageStopEvent,
60-
TextBlock,
61-
ToolUseBlock,
6260
)
6361

6462
if TYPE_CHECKING:
@@ -101,7 +99,7 @@ class _PatchedRawMessageStream(Stream[Any]):
10199

102100
_model: Optional[ModelParam]
103101
_usage: "_RecordedUsage"
104-
_content_blocks: list[dict[str, str]]
102+
_content_blocks: "list[dict[str, Any]]"
105103
_response_id: Optional[str]
106104
_finish_reason: Optional[str]
107105

@@ -111,7 +109,7 @@ class _PatchedMessageStream(MessageStream):
111109

112110
_model: Optional[ModelParam]
113111
_usage: "_RecordedUsage"
114-
_content_blocks: list[dict[str, str]]
112+
_content_blocks: "list[dict[str, Any]]"
115113
_response_id: Optional[str]
116114
_finish_reason: Optional[str]
117115

@@ -121,7 +119,7 @@ class _PatchedRawAsyncMessageStream(AsyncStream[Any]):
121119

122120
_model: Optional[ModelParam]
123121
_usage: "_RecordedUsage"
124-
_content_blocks: list[dict[str, str]]
122+
_content_blocks: "list[dict[str, Any]]"
125123
_response_id: Optional[str]
126124
_finish_reason: Optional[str]
127125

@@ -131,7 +129,7 @@ class _PatchedAsyncMessageStream(AsyncMessageStream):
131129

132130
_model: Optional[ModelParam]
133131
_usage: "_RecordedUsage"
134-
_content_blocks: list[dict[str, str]]
132+
_content_blocks: "list[dict[str, Any]]"
135133
_response_id: Optional[str]
136134
_finish_reason: Optional[str]
137135

@@ -373,10 +371,10 @@ def _collect_ai_data(
373371
event: "MessageStreamEvent",
374372
model: "str | None",
375373
usage: "_RecordedUsage",
376-
content_blocks: "list[dict[str, str]]",
374+
content_blocks: "list[dict[str, Any]]",
377375
response_id: "str | None" = None,
378376
finish_reason: "str | None" = None,
379-
) -> "tuple[str | None, _RecordedUsage, list[dict[str, str]], str | None, str | None]":
377+
) -> "tuple[str | None, _RecordedUsage, list[dict[str, Any]], str | None, str | None]":
380378
"""
381379
Collect model information, token usage, and collect content blocks from the AI streaming response.
382380
"""
@@ -387,16 +385,16 @@ def _collect_ai_data(
387385
event.content_block, ThinkingBlock
388386
):
389387
content_blocks.append({"type": "thinking", "thinking": ""})
390-
elif isinstance(event.content_block, (TextBlock, ToolUseBlock)):
388+
elif event.content_block.type in ("text", "tool_use"):
391389
content_blocks.append({"type": "text", "text": ""})
392390
elif event.type == "content_block_delta" and content_blocks:
393391
delta = event.delta
394392
if ThinkingDelta is not None and isinstance(delta, ThinkingDelta):
395393
content_blocks[-1]["thinking"] += delta.thinking
396-
elif hasattr(event.delta, "text"):
397-
content_blocks[-1]["text"] += event.delta.text
398-
elif hasattr(event.delta, "partial_json"):
399-
content_blocks[-1]["text"] += event.delta.partial_json
394+
elif hasattr(delta, "text"):
395+
content_blocks[-1]["text"] += delta.text
396+
elif hasattr(delta, "partial_json"):
397+
content_blocks[-1]["text"] += delta.partial_json
400398
elif event.type == "content_block_stop":
401399
pass
402400

@@ -723,7 +721,7 @@ def _set_output_data(
723721
output_tokens: "int | None",
724722
cache_read_input_tokens: "int | None",
725723
cache_write_input_tokens: "int | None",
726-
content_blocks: "list[dict[str, str]]",
724+
content_blocks: "list[dict[str, Any]]",
727725
response_id: "str | None" = None,
728726
finish_reason: "str | None" = None,
729727
) -> None:
@@ -754,7 +752,7 @@ def _set_output_data(
754752
if output["type"] == "text":
755753
parts.append({"type": "text", "content": output["text"]})
756754
elif output["type"] == "thinking":
757-
parts.append({"type": "reasoning", "content": output.get("thinking")})
755+
parts.append({"type": "reasoning", "content": output["thinking"]})
758756
elif output["type"] == "tool_use":
759757
tool_calls.append(output)
760758

@@ -1058,7 +1056,7 @@ def _set_streaming_output_data(
10581056
integration: "AnthropicIntegration",
10591057
model: "Optional[str]",
10601058
usage: "_RecordedUsage",
1061-
content_blocks: "list[dict[str, str]]",
1059+
content_blocks: "list[dict[str, Any]]",
10621060
response_id: "Optional[str]",
10631061
finish_reason: "Optional[str]",
10641062
) -> None:

0 commit comments

Comments
 (0)