Skip to content

Anthropic thinking returns error: "Thinking may not be enabled when tool_choice forces tool use." #2425

@fremis-LF

Description

@fremis-LF

Initial Checks

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions