-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
fix: Anthropic extended thinking with tool use and minor fixes #2487
Conversation
- In anthropic extended thinking we cannot send `tool_choice: {"type": "any"}` or `tool_choice: {"type": "tool", "name": "..."}` will give an error. - Source: https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#extended-thinking-with-tool-use - Fixes anthropic models file to explicitly handle thinking enabled flag. - Add testcases for output variations in pydantic as: `pydantic.BaseModel, ToolOutput and StructuredDict`. Failed earlier without changes - Add a warning for `temperature < 1`, as anthropic only allows `temperature == 1` if enabled thinking and `0.95 <= top_p <= 1`. - Source: https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#feature-compatibility - Minor fix in .gitignore as .venv 3.9, 3.10, ... handled and pycache, ruff cache handled as previously dependent on .gitignore file inside this folders. - Fix CLAUDE.md to use `pydantic_ai_slim/pydantic_ai/agent/` instead of `pydantic_ai_slim/pydantic_ai/agent.py` as file doesn't exists.
PR Change SummaryEnhanced Anthropic extended thinking functionality and addressed several issues.
Modified Files
How can I customize these reviews?Check out the Hyperlint AI Reviewer docs for more information on how to customize the review. If you just want to ignore it on this PR, you can add the Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add |
@Kaushal-26 Simply setting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert these change that seem specific to your environment
|
||
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather have the user see an error and adjust this manually, than to change it for them and log warnings. So please remove this from the PR for now and keep it focused on the tool choice issue. If others report the temperature/top_p issue and find it hard to work around, we can consider something like this then.
@@ -2163,3 +2163,80 @@ async def test_anthropic_web_search_tool_stream(allow_model_requests: None, anth | |||
|
|||
Additional notable stories include Vietnam's plan to ban fossil-fuel motorcycles in the heart of Hanoi starting July 2026, aiming to cut air pollution and move toward cleaner transport, and ongoing restoration efforts for Copenhagen's Old Stock Exchange, which is taking shape 15 months after a fire destroyed more than half of the 400-year-old building.\ | |||
""") | |||
|
|||
|
|||
async def test_anthropic_extended_thinking_with_tool_use(allow_model_requests: None, anthropic_api_key: str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the suggested change to raise an error instead, we can modify this to try just one output_type
and then use pytest.raises
to verify the error
Okay, I agree with you and am up for the requested changes. But let's talk about this:
from here: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use#forcing-tool-use I feel we should allow both. Let me know your pov |
@Kaushal-26 Ah, good find. That says we should
It may help to make that more strongly worded, but I'm not sure if that will give the same good result as having it in the user message. |
- content: | ||
- text: |- | ||
Validation feedback: | ||
Plain text responses are not permitted, please include your response in a tool call | ||
Fix the errors and try again. | ||
type: text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DouweM I tested a few things, you are right, though the tool is not 100% sure to be called.
Even in the test cassette, it gave a response after a retry
Fixes: #2425 (issue)
tool_choice: {"type": "any"}
ortool_choice: {"type": "tool", "name": "..."}
will give an error.pydantic.BaseModel, ToolOutput and StructuredDict
. Failed earlier without changestemperature < 1
, as anthropic only allowstemperature == 1
if enabled thinking and0.95 <= top_p <= 1
.pydantic_ai_slim/pydantic_ai/agent/
instead ofpydantic_ai_slim/pydantic_ai/agent.py
as file doesn't exists.