ISSUE DESCRIPTION Deferred capabilities throw error 400 with Deepseek models Initial…
- User
ISSUE DESCRIPTION
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
Good, this feature exists. Let's look at the deferred loading toolset and how it interacts with message history / reasoning content.
Assistant
Now let's write a reproduction script using this MockOpenAI setup and Agent with Capability(defer_loading=True), OpenAIChatModel with deepseek-like profile, to inspect chat_completion_kwargs across turns.
Assistant
Need to allow model requests.
Assistant
Now let's implement the fix in
_into_message_param.Assistant
Now let's rerun our reproduction script to confirm the fix.
Assistant
The fix works — the synthetic
search_toolsassistant message now includesreasoning_content: ''satisfying DeepSeek's requirement. Let's clean up the repro script and run the existing test suite for openai models and capabilities/tool_search.Assistant
Good, tests/init.py already existed (dated Jul 22), not created by me. Now let's run relevant tests.
Assistant
All pass. Now let's run broader tool_search / capabilities test suites, and full test suite time-permitting.
Assistant
All pass. Let's run the full test suite for pydantic_ai_slim to be safe (may take a while but let's try with reasonable scope).
Assistant
This failure is unrelated to our change (huggingface_hub deprecation warning). Let's skip that test and continue full suite run.
Assistant
These test_dbos/test_temporal failures look like pre-existing environment issues unrelated to our change. Let's wait for completion and then verify by checking out clean repo state to confirm these fail without our fix too.
Assistant
These are all dbos/temporal tests unrelated to our change (environment/infra issues like SQLAlchemy IntegrityError, temporal RuntimeError). Let's confirm these fail on a clean checkout too (i.e., pre-existing failures unrelated to our fix).
Assistant
Confirmed: this failure exists without our changes too (pre-existing environment issue, likely DBOS SQLite database state pollution across test runs). Our change is unrelated. Now let's review our diff and check the checklist.
Assistant
Good, only one implementation, used generically for the OpenAI-compatible Chat Completions API. Let's also check other model files (e.g., other custom models that might build similar deepseek-style requests) to see if similar issue exists elsewhere, but likely this is the only one relevant.
Assistant
Only in
openai.py, which is what we've fixed. Let's also check that pyright/type-checking doesn't complain, and run a lint check.Assistant
Excellent, no type errors. Let's also run the deepseek-specific/openrouter related tests to double check.
Assistant
All relevant tests pass. Let's do a final review of the diff.
Assistant
This is a minimal, targeted fix. Let's go through the checklist:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.