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
Now let me understand how
before_model_requestmessages relate to persisted history: - Assistant
Let me read the required guideline docs before implementing:
- Assistant
Now the transformation in
_tool_search.py: - Assistant
Now wire it into
Model.prepare_messages: - Assistant
Now set the flag in
DeepSeekProvider.model_profile: - Assistant
Now let me verify the behavior end-to-end with a scratch script before writing tests:
- Assistant
Now let me look at how existing tests construct mock tool call completions:
- Assistant
Now let me write the tests. First the DeepSeek wire-level regression test:
- Assistant
Now append the regression test to
test_deepseek.py: - Assistant
That edit was a mistake — revert it:
- Assistant
Now the
prepare_messages-level tests intests/test_tool_search.py. First add imports: - Assistant
Now add the tests after
test_prepare_messages_passes_through_on_native_model: - Assistant
Now let's check documentation touchpoints:
- Assistant
Final verification run after the lint fixes:
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.