Skip to content

Commit 4d76ed1

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 ff0fbe3 commit 4d76ed1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,3 +2723,12 @@ async def test_anthropic_web_search_tool_stream(allow_model_requests: None, anth
27232723
PartDeltaEvent(index=17, delta=TextPartDelta(content_delta=' disruptions affecting North America.')),
27242724
]
27252725
)
2726+
2727+
2728+
def test_pause_turn_finish_reason_mapping():
2729+
"""Test that pause_turn is mapped to None so the agent continues."""
2730+
from pydantic_ai.models.anthropic import _FINISH_REASON_MAP # pyright: ignore[reportPrivateUsage]
2731+
2732+
assert _FINISH_REASON_MAP['pause_turn'] is None
2733+
assert _FINISH_REASON_MAP['end_turn'] == 'stop'
2734+
assert _FINISH_REASON_MAP['tool_use'] == 'tool_call'

0 commit comments

Comments
 (0)