-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Labels
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
Hello! Loving PydanticAI so far so thank you!
Currently running into an issue though:
when calling an Anthropic model, such as Claude Sonnet 4, using an agent with an output_type set as a specific BaseModel, and specifying an anthropic_thinking
field, I get this error:
ModelHTTPError("status_code: 400, model_name: claude-sonnet-4-20250514, body: {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'Thinking may not be enabled when tool_choice forces tool use.'}}")
"Thinking may not be enabled when tool_choice forces tool use."
I'm guessing it's because the output_type is enforced using a tool and forcing the model to use it but that Anthropic doesn't allow that when reasoning is enabled.
Any ideas for workaround or fixes?
Example Code
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings
class OutputFormat(BaseModel):
response: str
model_settings = AnthropicModelSettings(
anthropic_thinking={"type": "enabled", "budget_tokens": 10000},
temperature=0.0,
timeout=300,
max_tokens=50000,
)
agent = Agent(
model=AnthropicModel(model_name="claude-sonnet-4-20250514"),
output_type=OutputFormat,
output_retries=2,
system_prompt="system prompt",
model_settings=model_settings,
)
agent_output = await agent.run("query")
Python, Pydantic AI & LLM client version
python 3.11.11
name : pydantic
version : 2.11.7
name : pydantic-ai
version : 0.5.0