Deferred capabilities throw error 400 with Deepseek models Initial Checks - [x] I'm…
- User
Deferred capabilities throw error 400 with Deepseek models
Initial Checks
- I'm using the latest version of Pydantic AI
- I've searched for my issue in the issue tracker before opening this issue
Description
When running an agent using a DeepSeek reasoning model, using
Capability(..., defer_loading=True)causes aModelHTTPError: status_code: 400during execution loop turns.openai.BadRequestError: Error code: 400 - {'error': {'message': 'The `reasoning_content` in the thinking mode must be passed back to the API.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}This is triggered because DeepSeek requires the original
reasoning_contentproduced during the thinking phase to be passed back exactly as-is in the subsequent conversation message history array. Whendefer_loading=True, the graph loop intercepted by PydanticAI's capability runner seems to drop or fail to serialize/propagate thereasoning_contenttoken block back to DeepSeek's chat completions endpoint.Setting
defer_loading=Falsesolves the issue, and the agent executes successfully.Minimal, Reproducible Example
# Remember to export the DEEPSEEK_API_KEY environment variable first, then run this code import random from pydantic_ai import Agent, RunContext from pydantic_ai.capabilities import Capability def roll_dice() -> str: """Roll a six-sided die and return the result.""" return str(random.randint(1, 6)) def get_player_name(ctx: RunContext[str]) -> str: """Get the player's name.""" return ctx.deps agent = Agent( 'deepseek:deepseek-v4-flash', deps_type=str, instructions=( "You're a dice game, you should roll the die and see if the number " "you get back matches the user's guess. If so, tell them they're a winner. " "Use the player's name in the response." ), capabilities=[Capability(id='DICE_ROLL', tools=[get_player_name, roll_dice], defer_loading=True)] # Change this to False and the agent will run successfully. ) dice_result = agent.run_sync('My guess is 4', deps='Anne') print(dice_result.output)Logfire Trace
No response
Python, Pydantic AI & LLM client version
- Python:
- Assistant
Investigating how deferred capabilities interact with DeepSeek's
reasoning_contentin message history. - Assistant
Writing a test to reproduce the message-mapping issue.
- Assistant
Identifying the issue: deferred capabilities inject synthetic
auto_load_*tool-search messages withoutreasoning_content, which DeepSeek rejects in thinking mode. Implementing a fix to strip those from the wire-format history while keeping them in persisted history. - Assistant
Adding tests for the fix:
- Assistant
Root cause
With
defer_loading=True, afterload_capabilitysucceeds,ToolSearch.before_model_requestinjects a synthetic tool-search exchange into message history (auto_load_*ToolSearchCallPart+ToolSearchReturnPart). That pair is needed sodiscovered_tool_namescan be reconstructed on history replay.Those synthetic messages were also being sent to the API. DeepSeek thinking models require every assistant turn to include
reasoning_content. The fake assistant message hastool_callsbut noreasoning_content, which triggers:The
reasoning_contentin the thinking mode must be passed back to the API.With
defer_loading=False, there is noload_capabilitycall and no synthetic exchange, so the agent works.Fix
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.