Skip to content

Commit 2cb63d3

Browse files
authored
Python: preserve empty signed Anthropic thinking blocks on streaming replay (microsoft#8171)
* fix(python): preserve empty signed Anthropic thinking on stream replay Empty thinking="" plus a signature_delta was coalesced to text=None, so the Anthropic serializer treated it as an orphan signature and dropped it before tool_use replay. Keep "" distinct from None through merge. Fixes microsoft#8168 * fix(python): satisfy ruff SIM108 on empty reasoning merge Use a ternary for combined_text so Package Checks lint passes. * fix(python): type streaming replay events for zuban
1 parent b197bf8 commit 2cb63d3

3 files changed

Lines changed: 117 additions & 2 deletions

File tree

‎python/packages/anthropic/tests/test_anthropic_client.py‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,96 @@ def test_prepare_message_for_anthropic_attaches_signature_only_reasoning(
550550
]
551551

552552

553+
def test_prepare_message_for_anthropic_empty_signed_thinking_is_replayed(
554+
mock_anthropic_client: MagicMock,
555+
) -> None:
556+
"""Empty signed thinking blocks must serialize as thinking, not be skipped.
557+
558+
Anthropic requires thinking blocks (including empty thinking text) to be
559+
replayed unchanged before tool results. See microsoft/agent-framework#8168.
560+
"""
561+
client = create_test_anthropic_client(mock_anthropic_client)
562+
message = Message(
563+
role="assistant",
564+
contents=[
565+
Content.from_text_reasoning(text="", protected_data="synthetic-signature"),
566+
Content.from_function_call(
567+
call_id="toolu_test",
568+
name="lookup",
569+
arguments={},
570+
),
571+
],
572+
)
573+
574+
result = client._prepare_message_for_anthropic(message)
575+
576+
assert result["content"][0] == {
577+
"type": "thinking",
578+
"thinking": "",
579+
"signature": "synthetic-signature",
580+
}
581+
assert result["content"][1]["type"] == "tool_use"
582+
assert result["content"][1]["id"] == "toolu_test"
583+
584+
585+
def test_streaming_replay_preserves_empty_signed_thinking_block(
586+
mock_anthropic_client: MagicMock,
587+
) -> None:
588+
"""Stream empty thinking + signature_delta + tool_use, then replay via from_updates.
589+
590+
Offline synthetic SDK events — no network. Regression for #8168.
591+
"""
592+
from anthropic.types.beta import (
593+
BetaRawContentBlockDeltaEvent,
594+
BetaRawContentBlockStartEvent,
595+
BetaRawMessageStreamEvent,
596+
)
597+
598+
client = create_test_anthropic_client(mock_anthropic_client)
599+
600+
events: list[BetaRawMessageStreamEvent] = [
601+
BetaRawContentBlockStartEvent.model_validate(
602+
{
603+
"type": "content_block_start",
604+
"index": 0,
605+
"content_block": {"type": "thinking", "thinking": "", "signature": ""},
606+
}
607+
),
608+
BetaRawContentBlockDeltaEvent.model_validate(
609+
{
610+
"type": "content_block_delta",
611+
"index": 0,
612+
"delta": {"type": "signature_delta", "signature": "synthetic-signature"},
613+
}
614+
),
615+
BetaRawContentBlockStartEvent.model_validate(
616+
{
617+
"type": "content_block_start",
618+
"index": 1,
619+
"content_block": {
620+
"type": "tool_use",
621+
"id": "toolu_test",
622+
"name": "lookup",
623+
"input": {},
624+
},
625+
}
626+
),
627+
]
628+
629+
updates = [client._process_stream_event(event) for event in events]
630+
response = ChatResponse.from_updates([u for u in updates if u is not None])
631+
message = response.messages[0]
632+
reasoning = next(c for c in message.contents if c.type == "text_reasoning")
633+
assert reasoning.text == ""
634+
assert reasoning.protected_data == "synthetic-signature"
635+
636+
prepared = client._prepare_message_for_anthropic(message)
637+
assert prepared["content"][0]["type"] == "thinking"
638+
assert prepared["content"][0]["thinking"] == ""
639+
assert prepared["content"][0]["signature"] == "synthetic-signature"
640+
assert prepared["content"][1]["type"] == "tool_use"
641+
642+
553643
def test_prepare_message_for_anthropic_skips_orphan_signature_only_reasoning(
554644
mock_anthropic_client: MagicMock,
555645
) -> None:

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,11 @@ def _add_text_reasoning_content(self, other: Content) -> Content:
15581558
)
15591559
combined_id = self.id or other.id
15601560

1561-
# Concatenate text, handling None values
1561+
# Concatenate text, handling None values.
1562+
# Preserve empty string "" as distinct from None. Anthropic can emit a thinking
1563+
# block with thinking="" followed by a signature_delta; collapsing "" to None
1564+
# makes a real empty signed thinking block look like an orphan signature and
1565+
# gets dropped on replay (see microsoft/agent-framework#8168).
15621566
self_text = self.text or ""
15631567
other_text = other.text or ""
15641568
if (
@@ -1567,7 +1571,7 @@ def _add_text_reasoning_content(self, other: Content) -> Content:
15671571
and ("reasoning_text" in self.additional_properties) != ("reasoning_text" in other.additional_properties)
15681572
):
15691573
raise AdditionItemMismatch("Cannot merge reasoning text with a reasoning summary")
1570-
combined_text = self_text + other_text if (self_text or other_text) else None
1574+
combined_text = None if self.text is None and other.text is None else self_text + other_text
15711575

15721576
# Handle protected_data replacement
15731577
protected_data = other.protected_data if other.protected_data is not None else self.protected_data

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,27 @@ def test_text_reasoning_content_add_conflicting_ids_raises():
21332133
_ = t1 + t2
21342134

21352135

2136+
def test_text_reasoning_content_add_preserves_empty_text_with_signature():
2137+
"""Empty thinking text plus a signature delta must keep text="" (not None).
2138+
2139+
Regression for microsoft/agent-framework#8168: collapsing "" to None makes a
2140+
real empty signed Anthropic thinking block look like an orphan signature.
2141+
"""
2142+
2143+
empty_thinking = Content.from_text_reasoning(text="")
2144+
signature_only = Content.from_text_reasoning(text=None, protected_data="synthetic-signature")
2145+
2146+
result = empty_thinking + signature_only
2147+
assert result.text == ""
2148+
assert result.protected_data == "synthetic-signature"
2149+
2150+
both_none = Content.from_text_reasoning(text=None) + Content.from_text_reasoning(
2151+
text=None, protected_data="orphan-sig"
2152+
)
2153+
assert both_none.text is None
2154+
assert both_none.protected_data == "orphan-sig"
2155+
2156+
21362157
def test_text_reasoning_content_add_neither_has_id():
21372158
"""Test that coalescing text_reasoning Content when neither has an id results in None id."""
21382159

0 commit comments

Comments
 (0)