Skip to content

Commit 42d6ea8

Browse files
committed
Python: Replay Anthropic redacted_thinking blocks in tool loops
redacted_thinking blocks were parsed through the default branch and recorded as dropped content, so the function-invocation loop replayed the assistant turn without them. Anthropic requires the thinking blocks of the last assistant turn, including redacted ones, to be returned unmodified while tool use continues. Parse redacted_thinking into its own reasoning item carrying the opaque payload in Content.protected_data plus an anthropic namespace marker, following the existing Bedrock redacted-reasoning pattern. Serialize marked items back as redacted_thinking blocks, give each parsed reasoning block its own identity so streaming aggregation keeps thinking and redacted_thinking blocks separate, and fail before the request when a marked item cannot be reconstructed. pause_turn continuations can now replay redacted reasoning instead of stopping early. Fixes microsoft#10104
1 parent 230a3aa commit 42d6ea8

4 files changed

Lines changed: 327 additions & 21 deletions

File tree

‎docs/specs/004-python-function-calling-loop.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ that manually replay messages own the equivalent rule: do not resend an approval
616616
| Declaration-only mixed batch | Known local siblings are never converted into Host pauses. With an authoritative session they remain deferred until every Host pause occurrence is answered, survive session serialization with a process-portable logical-run budget (including a reclassified approval continuation), reclassify against the current tool policy, execute exactly once using the configured concurrent or sequential invocation mode, replay results in original model order, and remain inert on replay; without one they execute on the same turn under the same configured invocation mode. Streaming updates and finalization preserve one Host pause and one result per local sibling. | `test_mixed_declaration_only_batch_defers_siblings_until_all_host_results`, `test_mixed_declaration_only_deferred_call_keeps_ordinary_middleware_semantics`, `test_mixed_declaration_only_deferred_call_reclassifies_changed_approval_policy`, `test_mixed_declaration_only_budget_survives_reclassification_to_approval`, `test_mixed_declaration_only_deferred_call_does_not_execute_removed_tool`, `test_mixed_declaration_only_budget_is_process_portable_without_approval_middleware`, `test_sessionless_mixed_declaration_only_batch_executes_siblings_sequentially`, `test_sessionless_mixed_declaration_only_batch_executes_siblings_concurrently`, `test_streaming_sessionless_mixed_declaration_only_batch_returns_pause_and_result` |
617617
| Function invocation disabled | The client bypasses the invocation loop without losing invocation kwargs. | `test_function_invocation_config_enabled_false`, `test_function_invocation_config_enabled_false_preserves_invocation_kwargs`, `test_streaming_function_invocation_config_enabled_false` |
618618
| Runtime tool changes | Added tools become available on the next iteration and retain approval behavior. | `test_add_tools_available_next_iteration`, `test_add_tools_with_approval_required_tool` |
619-
| Anthropic pause-turn continuation | `pause_turn` replays the returned assistant content with the same tools until Anthropic completes the server-side turn in streaming and non-streaming modes when every native content block can be reconstructed faithfully. Intermediate messages and usage remain caller-visible and retain provider message boundaries. Unsupported parsed content, native blocks dropped during parsing (including redacted reasoning), streamed MCP calls whose argument deltas are not retained, and bounded-continuation exhaustion keep `finish_reason="pause_turn"` instead of sending a lossy transcript or reporting normal completion. | `packages/anthropic/tests/test_anthropic_client.py::test_agent_continues_anthropic_pause_turn`, `test_streaming_hosted_tool_pause_turn_continues`, `test_streaming_pause_turn_preserves_message_boundaries_for_structured_output`, `test_non_streaming_code_execution_pause_turn_is_not_auto_continued`, `test_streaming_code_execution_pause_turn_is_not_auto_continued`, `test_streaming_mcp_pause_turn_is_not_auto_continued`, `test_non_streaming_redacted_thinking_pause_turn_is_not_auto_continued`, `test_streaming_redacted_thinking_pause_turn_is_not_auto_continued`, `test_anthropic_pause_turn_limit_preserves_finish_reason` |
619+
| Anthropic pause-turn continuation | `pause_turn` replays the returned assistant content with the same tools until Anthropic completes the server-side turn in streaming and non-streaming modes when every native content block can be reconstructed faithfully. Intermediate messages and usage remain caller-visible and retain provider message boundaries. Unsupported parsed content, native blocks dropped during parsing, streamed MCP calls whose argument deltas are not retained, and bounded-continuation exhaustion keep `finish_reason="pause_turn"` instead of sending a lossy transcript or reporting normal completion. | `packages/anthropic/tests/test_anthropic_client.py::test_agent_continues_anthropic_pause_turn`, `test_streaming_hosted_tool_pause_turn_continues`, `test_streaming_pause_turn_preserves_message_boundaries_for_structured_output`, `test_non_streaming_code_execution_pause_turn_is_not_auto_continued`, `test_streaming_code_execution_pause_turn_is_not_auto_continued`, `test_streaming_mcp_pause_turn_is_not_auto_continued`, `test_non_streaming_redacted_thinking_pause_turn_continues`, `test_streaming_redacted_thinking_pause_turn_continues`, `test_anthropic_pause_turn_limit_preserves_finish_reason` |
620620
| Direct delegated session propagation | A custom loop must explicitly identify parent provider-owned state before sharing a non-empty session; missing ownership fails before the child runs, while declared parent keys remain isolated and application state still propagates. | `packages/core/tests/core/test_agents.py::test_chat_agent_as_tool_direct_propagation_requires_parent_provider_ownership`, `test_chat_agent_as_tool_direct_propagation_uses_explicit_parent_provider_ownership` |
621621
| In-run compaction summaries | Summaries inserted for tool groups excluded by in-run compaction are returned in the final non-streaming response transcript before the group they replace, so history loaded with `skip_excluded` keeps the summarized content; summaries of caller-owned input messages are not added. | `packages/core/tests/core/test_clients.py::test_function_loop_returns_compaction_summaries_in_final_response`, `test_function_loop_returns_compaction_summaries_when_iteration_budget_exhausted`, `test_function_loop_reconciles_nested_compaction_summaries`, `test_function_loop_returns_compacted_transcript_on_early_terminal_exit`, `packages/core/tests/core/test_agents.py::test_agent_run_returns_and_persists_compaction_summaries` |
622622
| Per-service-call provider context | When framework-managed history is loaded around each model call, tools and instructions contributed by `HistoryProvider.before_run` remain available to the model and function loop without duplication in streaming or non-streaming execution. | `packages/core/tests/core/test_agents.py::test_vector_history_search_tool_is_available_with_per_service_call_persistence` |

‎docs/specs/005-python-content-identity-and-protocol-metadata.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Example:
179179
| OpenAI | OAuth consent request server | `server_label` | `openai["server_label"]` | Read indefinitely; dual-write through the writer package's next major version. |
180180
| Foundry | reasoning content | `__foundry_reasoning_replay_item__` | `foundry["reasoning_item"]` | Read indefinitely; dual-write through the Foundry package's next major version. |
181181
| Bedrock | redacted reasoning marker | `bedrock.redacted_reasoning` | `bedrock["redacted_reasoning"]` | Read indefinitely; dual-write through the Bedrock package's next major version. |
182+
| Anthropic | native reasoning block type | N/A | `anthropic["reasoning_block"]` | New metadata; values are the Anthropic block types `thinking` and `redacted_thinking`, and a redacted payload uses `Content.protected_data`. |
182183
| Mistral | response service tier | `service_tier` | `mistral["service_tier"]` | Read indefinitely; dual-write through the Mistral package's next major version. |
183184
| A2A | response/update/message metadata | `a2a_metadata` | `a2a` | Read indefinitely with a warning for legacy-only input; dual-write through the A2A package's next major version. |
184185
| all | data/URI/hosted-file display name | `filename` | `Content.name` | Read indefinitely where accepted; dual-write where older adapters require it. |
@@ -242,7 +243,7 @@ merge_additional_properties(existing, incoming, *, incoming_wins=True)
242243
| AG-UI stream merge modes, hosted/local ownership, and approval replay | `python/packages/ag-ui/tests/ag_ui/test_event_converters.py`, `test_run.py`, `test_workflow_run.py`, and `test_endpoint.py` |
243244
| A2A stream merge modes and namespaced metadata round trip | `python/packages/a2a/tests/test_a2a_agent.py` |
244245
| Hosting A2A message metadata conversion | `python/packages/hosting-a2a/tests/hosting_a2a/test_conversion.py` |
245-
| Anthropic streamed merge modes and provider-hosted tool routing | `python/packages/anthropic/tests/test_anthropic_client.py` |
246+
| Anthropic streamed merge modes, provider-hosted tool routing, and redacted-thinking replay metadata | `python/packages/anthropic/tests/test_anthropic_client.py` |
246247
| Bedrock streamed merge modes and redacted-reasoning replay metadata | `python/packages/bedrock/tests/test_bedrock_client.py` |
247248
| Gemini streamed merge modes and persisted provider-hosted tool replay | `python/packages/gemini/tests/test_gemini_client.py` |
248249
| Mistral streamed merge modes and response metadata | `python/packages/mistral/tests/mistral/test_mistral_chat_client.py` |

‎python/packages/anthropic/agent_framework_anthropic/_chat_client.py‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collections.abc import AsyncIterable, Awaitable, Callable, Mapping, Sequence
1010
from dataclasses import dataclass, field
1111
from typing import Any, ClassVar, Final, Generic, Literal, TypeAlias, TypedDict, cast
12+
from uuid import uuid4
1213

1314
from agent_framework import (
1415
Annotation,
@@ -106,6 +107,12 @@
106107
"web_search_tool_result",
107108
})
108109
_MAX_PAUSE_TURN_CONTINUATIONS: Final[int] = 10
110+
# Marks text_reasoning content parsed from a native Anthropic reasoning block; the value is the
111+
# Anthropic block type. Distinguishes native reasoning items from provider reasoning summaries
112+
# that carry a Content.id without a protected payload.
113+
_REASONING_BLOCK_KEY: Final[str] = "reasoning_block"
114+
_REASONING_BLOCK_THINKING: Final[str] = "thinking"
115+
_REASONING_BLOCK_REDACTED_THINKING: Final[str] = "redacted_thinking"
109116
_PAUSE_TURN_REPLAYABLE_CONTENT_TYPES: Final[frozenset[str]] = frozenset({
110117
"text",
111118
"text_reasoning",
@@ -835,6 +842,8 @@ def _append_pause_turn_messages_for_anthropic(
835842

836843
def _is_pause_turn_content_replayable(self, content: Content) -> bool:
837844
"""Return whether the Anthropic serializer preserves this paused content item."""
845+
if content.type == "text_reasoning" and self._is_redacted_thinking_content(content):
846+
return bool(content.protected_data)
838847
if content.type in _PAUSE_TURN_REPLAYABLE_CONTENT_TYPES:
839848
return True
840849
if content.type == "function_call":
@@ -1130,6 +1139,36 @@ def _message_has_tool_use(self, message: dict[str, Any]) -> bool:
11301139

11311140
return False
11321141

1142+
@staticmethod
1143+
def _generate_reasoning_id() -> str:
1144+
"""Return a fresh identity for one reasoning item.
1145+
1146+
Anthropic reasoning blocks have no provider identity of their own, and response
1147+
aggregation folds adjacent reasoning chunks without ids into one item. Giving each
1148+
parsed reasoning block its own id keeps consecutive ``thinking`` and
1149+
``redacted_thinking`` blocks distinct when a streamed response is finalized.
1150+
"""
1151+
return f"reasoning-{uuid4().hex}"
1152+
1153+
@staticmethod
1154+
def _native_reasoning_block_type(content: Content) -> Any:
1155+
"""Return the Anthropic native block type recorded on a reasoning item, if any."""
1156+
return get_namespaced_value(
1157+
content.additional_properties,
1158+
_ANTHROPIC_ADDITIONAL_PROPS_NAMESPACE,
1159+
_REASONING_BLOCK_KEY,
1160+
)
1161+
1162+
@classmethod
1163+
def _is_redacted_thinking_content(cls, content: Content) -> bool:
1164+
"""Return whether this reasoning item carries an Anthropic redacted-thinking payload."""
1165+
return cls._native_reasoning_block_type(content) == _REASONING_BLOCK_REDACTED_THINKING
1166+
1167+
@classmethod
1168+
def _is_thinking_content(cls, content: Content) -> bool:
1169+
"""Return whether this reasoning item was parsed from an Anthropic ``thinking`` block."""
1170+
return cls._native_reasoning_block_type(content) == _REASONING_BLOCK_THINKING
1171+
11331172
@staticmethod
11341173
def _server_tool_block(content: Content) -> Mapping[str, Any] | None:
11351174
"""Return validated persisted Anthropic server-tool metadata."""
@@ -1363,6 +1402,11 @@ def _prepare_message_for_anthropic(self, message: Message) -> dict[str, Any]:
13631402
mcp_result["is_error"] = is_error
13641403
a_content.append(mcp_result)
13651404
case "text_reasoning":
1405+
if self._is_redacted_thinking_content(content):
1406+
if not content.protected_data:
1407+
raise ContentError("Anthropic redacted thinking content is missing its opaque payload.")
1408+
a_content.append({"type": "redacted_thinking", "data": content.protected_data})
1409+
continue
13661410
if content.text is None:
13671411
if (
13681412
content.protected_data
@@ -1372,7 +1416,7 @@ def _prepare_message_for_anthropic(self, message: Message) -> dict[str, Any]:
13721416
):
13731417
a_content[-1]["signature"] = content.protected_data
13741418
continue
1375-
if content.id and not content.protected_data:
1419+
if content.id and not content.protected_data and not self._is_thinking_content(content):
13761420
a_content.append({"type": "text", "text": content.text})
13771421
continue
13781422
thinking_block: dict[str, Any] = {"type": "thinking", "thinking": content.text}
@@ -2081,11 +2125,37 @@ def _parse_contents_from_anthropic(
20812125
raw_representation=content_block,
20822126
)
20832127
)
2128+
case "redacted_thinking":
2129+
# Redacted reasoning is opaque provider state. Anthropic requires it to be replayed
2130+
# unchanged while tool use continues, so keep it as its own reasoning item.
2131+
contents.append(
2132+
Content.from_text_reasoning(
2133+
id=self._generate_reasoning_id(),
2134+
protected_data=content_block.data,
2135+
additional_properties={
2136+
_ANTHROPIC_ADDITIONAL_PROPS_NAMESPACE: {
2137+
_REASONING_BLOCK_KEY: _REASONING_BLOCK_REDACTED_THINKING
2138+
}
2139+
},
2140+
merge_mode="replace" if streaming else None,
2141+
raw_representation=content_block,
2142+
)
2143+
)
20842144
case "thinking" | "thinking_delta":
20852145
contents.append(
20862146
Content.from_text_reasoning(
2147+
id=self._generate_reasoning_id() if content_block.type == "thinking" else None,
20872148
text=content_block.thinking,
20882149
protected_data=getattr(content_block, "signature", None),
2150+
additional_properties=(
2151+
{
2152+
_ANTHROPIC_ADDITIONAL_PROPS_NAMESPACE: {
2153+
_REASONING_BLOCK_KEY: _REASONING_BLOCK_THINKING
2154+
}
2155+
}
2156+
if content_block.type == "thinking"
2157+
else None
2158+
),
20892159
merge_mode="append" if streaming else None,
20902160
raw_representation=content_block,
20912161
)

0 commit comments

Comments
 (0)