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.codeopenai.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
Python - Assistant
Investigating how deferred capabilities interact with DeepSeek's
reasoning_contenthandling in the message history. - Assistant
Writing a unit test to verify whether synthetic tool-search messages cause missing
reasoning_contentin the mapped request. - Assistant
Implementing the fix: synthetic tool-search assistant messages from deferred capability loading lack
reasoning_content, which DeepSeek requires. - Assistant
Root Cause
When
defer_loading=True, loading a deferred capability injects a synthetic tool-search exchange into message history (ToolSearchCallPart+ToolSearchReturnPart) so discovered tools can be replayed on later turns.That synthetic exchange is stored as an assistant
ModelResponsewith only a tool call — noThinkingPart. On the next API request, DeepSeek maps history back to chat completions and expects every assistant message in thinking mode to includereasoning_content. The synthetic message didn't have it, producing:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.