Skip to content

🚨 Fix instructions when using Anthropic #2190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions pydantic_ai_slim/pydantic_ai/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
from ..providers import Provider, infer_provider
from ..settings import ModelSettings
from ..tools import ToolDefinition
from . import Model, ModelRequestParameters, StreamedResponse, check_allow_model_requests, download_item, get_user_agent
from . import (
Model,
ModelRequestParameters,
StreamedResponse,
check_allow_model_requests,
download_item,
get_user_agent,
)

try:
from anthropic import NOT_GIVEN, APIStatusError, AsyncAnthropic, AsyncStream
Expand Down Expand Up @@ -238,7 +245,7 @@ async def _messages_create(
extra_headers.setdefault('User-Agent', get_user_agent())
return await self.client.beta.messages.create(
max_tokens=model_settings.get('max_tokens', 4096),
system=system_prompt or NOT_GIVEN,
system=system_prompt.strip() or NOT_GIVEN,
Copy link
Preview

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .strip() will remove all leading and trailing whitespace, including intentional newlines or spaces used for formatting the system prompt. Consider using .rstrip() if you only want to remove trailing whitespace, or explicitly handle trimming of blank-only strings.

Suggested change
system=system_prompt.strip() or NOT_GIVEN,
system=system_prompt.rstrip() if system_prompt.strip() else NOT_GIVEN,

Copilot uses AI. Check for mistakes.

messages=anthropic_messages,
model=self._model_name,
tools=tools or NOT_GIVEN,
Expand Down Expand Up @@ -348,7 +355,9 @@ async def _map_message(self, messages: list[ModelMessage]) -> tuple[str, list[Be
if response_part.signature is not None: # pragma: no branch
assistant_content_params.append(
BetaThinkingBlockParam(
thinking=response_part.content, signature=response_part.signature, type='thinking'
thinking=response_part.content,
signature=response_part.signature,
type='thinking',
)
)
else:
Expand Down Expand Up @@ -383,7 +392,11 @@ async def _map_user_prompt(
elif isinstance(item, BinaryContent):
if item.is_image:
yield BetaImageBlockParam(
source={'data': io.BytesIO(item.data), 'media_type': item.media_type, 'type': 'base64'}, # type: ignore
source={
'data': io.BytesIO(item.data),
'media_type': item.media_type,
'type': 'base64',
}, # type: ignore
type='image',
)
elif item.media_type == 'application/pdf':
Expand Down
36 changes: 31 additions & 5 deletions tests/models/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,16 +509,25 @@ async def retrieve_entity_info(name: str) -> str:
part_kind='text',
),
ToolCallPart(
tool_name='retrieve_entity_info', args={'name': 'Alice'}, tool_call_id=IsStr(), part_kind='tool-call'
tool_name='retrieve_entity_info',
args={'name': 'Alice'},
tool_call_id=IsStr(),
part_kind='tool-call',
),
ToolCallPart(
tool_name='retrieve_entity_info', args={'name': 'Bob'}, tool_call_id=IsStr(), part_kind='tool-call'
Copy link
Preview

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This ToolCallPart for Bob remains on one line while the others are broken out into multiple lines. For consistency and readability, apply the same multiline formatting here.

Suggested change
tool_name='retrieve_entity_info', args={'name': 'Bob'}, tool_call_id=IsStr(), part_kind='tool-call'
tool_name='retrieve_entity_info',
args={'name': 'Bob'},
tool_call_id=IsStr(),
part_kind='tool-call',

Copilot uses AI. Check for mistakes.

),
ToolCallPart(
tool_name='retrieve_entity_info', args={'name': 'Charlie'}, tool_call_id=IsStr(), part_kind='tool-call'
tool_name='retrieve_entity_info',
args={'name': 'Charlie'},
tool_call_id=IsStr(),
part_kind='tool-call',
),
ToolCallPart(
tool_name='retrieve_entity_info', args={'name': 'Daisy'}, tool_call_id=IsStr(), part_kind='tool-call'
tool_name='retrieve_entity_info',
args={'name': 'Daisy'},
tool_call_id=IsStr(),
part_kind='tool-call',
),
]
)
Expand Down Expand Up @@ -953,6 +962,17 @@ def simple_instructions():
)


async def test_anthropic_model_following_instructions(allow_model_requests: None, anthropic_api_key: str):
class UserName(BaseModel):
name: str

m = AnthropicModel('anthropic:claude-sonnet-4-20250514', provider=AnthropicProvider(api_key=anthropic_api_key))
agent = Agent(m, instructions='my name is hamza')

result = await agent.run('what is my name?', output_type=UserName)
assert result.output.name.lower() == 'hamza'


async def test_anthropic_model_thinking_part(allow_model_requests: None, anthropic_api_key: str):
m = AnthropicModel('claude-3-7-sonnet-latest', provider=AnthropicProvider(api_key=anthropic_api_key))
settings = AnthropicModelSettings(anthropic_thinking={'type': 'enabled', 'budget_tokens': 1024})
Expand Down Expand Up @@ -1228,7 +1248,10 @@ def anth_msg(usage: BetaUsage) -> BetaMessage:
lambda: anth_msg(BetaUsage(input_tokens=1, output_tokens=1)),
snapshot(
Usage(
request_tokens=1, response_tokens=1, total_tokens=2, details={'input_tokens': 1, 'output_tokens': 1}
request_tokens=1,
response_tokens=1,
total_tokens=2,
details={'input_tokens': 1, 'output_tokens': 1},
)
),
id='AnthropicMessage',
Expand Down Expand Up @@ -1258,7 +1281,10 @@ def anth_msg(usage: BetaUsage) -> BetaMessage:
),
snapshot(
Usage(
request_tokens=1, response_tokens=1, total_tokens=2, details={'input_tokens': 1, 'output_tokens': 1}
request_tokens=1,
response_tokens=1,
total_tokens=2,
details={'input_tokens': 1, 'output_tokens': 1},
)
),
id='RawMessageStartEvent',
Expand Down
Loading