Skip to content

Commit 04ebcdc

Browse files
feat(anthropic): Record reasoning parts in output
1 parent dbc50bd commit 04ebcdc

2 files changed

Lines changed: 918 additions & 902 deletions

File tree

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
ToolUnionParam,
8686
)
8787

88-
from sentry_sdk._types import TextPart
88+
from sentry_sdk._types import ReasoningPart, TextPart
8989

9090
class _PatchedRawMessageStream(Stream[Any]):
9191
_span: Union[Span, StreamedSpan]
@@ -731,30 +731,32 @@ def _set_output_data(
731731
record_outputs = True
732732

733733
if record_outputs:
734-
output_messages: "dict[str, list[Any]]" = {
735-
"response": [],
736-
"tool": [],
737-
}
734+
parts: "list[Union[TextPart, ReasoningPart]]" = []
735+
tool_calls: "list" = []
738736

739737
for output in content_blocks:
740738
if output["type"] == "text":
741-
output_messages["response"].append(output["text"])
739+
parts.append({"type": "text", "content": output["text"]})
740+
elif output["type"] == "thinking":
741+
parts.append(
742+
{"type": "reasoning", "content": output.get("thinking", "")}
743+
)
742744
elif output["type"] == "tool_use":
743-
output_messages["tool"].append(output)
745+
tool_calls.append(output)
744746

745-
if len(output_messages["tool"]) > 0:
747+
if len(tool_calls) > 0:
746748
set_data_normalized(
747749
span,
748750
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
749-
output_messages["tool"],
751+
tool_calls,
750752
unpack=False,
751753
)
752754

753-
if len(output_messages["response"]) > 0:
755+
if len(parts) > 0:
754756
set_data_normalized(
755757
span,
756-
SPANDATA.GEN_AI_RESPONSE_TEXT,
757-
output_messages["response"],
758+
SPANDATA.GEN_AI_OUTPUT_MESSAGES,
759+
[{"role": "assistant", "parts": parts}],
758760
unpack=False,
759761
)
760762

0 commit comments

Comments
 (0)