Skip to content

Commit a5ae73a

Browse files
committed
Fix Anthropic pause_turn handling to allow agent continuation
Map Anthropic's pause_turn finish reason to None instead of 'stop' so the agent automatically continues with a new request. This fixes issues with long-running builtin tools like web_search that may trigger pause_turn. When finish_reason is None, the agent treats the response as incomplete and makes another request with the same message history, allowing the conversation to continue seamlessly. Fixes #2600
1 parent c3a7058 commit a5ae73a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
from ..tools import ToolDefinition
4343
from . import Model, ModelRequestParameters, StreamedResponse, check_allow_model_requests, download_item, get_user_agent
4444

45-
_FINISH_REASON_MAP: dict[BetaStopReason, FinishReason] = {
45+
_FINISH_REASON_MAP: dict[BetaStopReason, FinishReason | None] = {
4646
'end_turn': 'stop',
4747
'max_tokens': 'length',
4848
'stop_sequence': 'stop',
4949
'tool_use': 'tool_call',
50-
'pause_turn': 'stop',
50+
'pause_turn': None,
5151
'refusal': 'content_filter',
5252
}
5353

tests/models/test_anthropic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,6 +4897,7 @@ async def test_anthropic_web_search_tool_stream(allow_model_requests: None, anth
48974897
)
48984898

48994899

4900+
<<<<<<< HEAD
49004901
async def test_anthropic_text_parts_ahead_of_built_in_tool_call(allow_model_requests: None, anthropic_api_key: str):
49014902
# Verify that text parts ahead of the built-in tool call are not included in the output
49024903

@@ -5112,3 +5113,12 @@ def memory(**command: Any) -> Any:
51125113
51135114
According to my memory, you live in **Mexico City**.\
51145115
""")
5116+
5117+
5118+
def test_pause_turn_finish_reason_mapping():
5119+
"""Test that pause_turn is mapped to None so the agent continues."""
5120+
from pydantic_ai.models.anthropic import _FINISH_REASON_MAP # pyright: ignore[reportPrivateUsage]
5121+
5122+
assert _FINISH_REASON_MAP['pause_turn'] is None
5123+
assert _FINISH_REASON_MAP['end_turn'] == 'stop'
5124+
assert _FINISH_REASON_MAP['tool_use'] == 'tool_call'

0 commit comments

Comments
 (0)