Skip to content

fix: Anthropic extended thinking with tool use and minor fixes #2487

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
site
.venv
.venv*
dist
__pycache__
.env
Expand All @@ -11,7 +11,7 @@ env*/
/postgres-data/
.DS_Store
examples/pydantic_ai_examples/.chat_app_messages.sqlite
.cache/
*cache/
.vscode/
/question_graph_history.json
/docs-site/.wrangler/
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co

### Core Components

**Agent System (`pydantic_ai_slim/pydantic_ai/agent.py`)**
**Agent System (`pydantic_ai_slim/pydantic_ai/agent/`)**
- `Agent[AgentDepsT, OutputDataT]`: Main orchestrator class with generic types for dependency injection and output validation
- Entry points: `run()`, `run_sync()`, `run_stream()` methods
- Handles tool management, system prompts, and model interaction
Expand Down
23 changes: 20 additions & 3 deletions pydantic_ai_slim/pydantic_ai/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,29 @@ async def _messages_create(
tools = self._get_tools(model_request_parameters)
tools += self._get_builtin_tools(model_request_parameters)
tool_choice: BetaToolChoiceParam | None
thinking = model_settings.get('anthropic_thinking', NOT_GIVEN)
temperature = model_settings.get('temperature', NOT_GIVEN)
top_p = model_settings.get('top_p', NOT_GIVEN)

if not tools:
tool_choice = None
else:
if not model_request_parameters.allow_text_output:
tool_choice = {'type': 'any'}
if thinking and thinking.get('type') == 'enabled':
tool_choice = {'type': 'auto'}
if temperature and temperature != 1:
warnings.warn(
'Anthropic only allows temperature set to 1 or not given, if thinking enabled using 1 for now',
UserWarning,
)
temperature = 1
if top_p and top_p < 0.95:
warnings.warn(
'Anthropic only allows top_p greater than or equal to 0.95 or not given, if thinking enabled using 0.95 for now',
UserWarning,
)
top_p = 0.95
else:
tool_choice = {'type': 'auto'}

Expand All @@ -273,10 +290,10 @@ async def _messages_create(
tools=tools or NOT_GIVEN,
tool_choice=tool_choice or NOT_GIVEN,
stream=stream,
thinking=model_settings.get('anthropic_thinking', NOT_GIVEN),
thinking=thinking,
stop_sequences=model_settings.get('stop_sequences', NOT_GIVEN),
temperature=model_settings.get('temperature', NOT_GIVEN),
top_p=model_settings.get('top_p', NOT_GIVEN),
temperature=temperature,
top_p=top_p,
timeout=model_settings.get('timeout', NOT_GIVEN),
metadata=model_settings.get('anthropic_metadata', NOT_GIVEN),
extra_headers=extra_headers,
Expand Down
Loading
Loading