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.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:
3.14 - Pydantic AI:
1.106.0 - LLM provider SDK: deepseek
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pydantic-ai, current file: none) bash-$
Assistant
Now let me write a repro script.
Assistant
Now let me implement the fix.
Assistant
Now let's run the repro to see the effect.
Assistant
I've completed the fix. Here's my checklist review:
1. If an error was reported in the issue description, I have successfully reproduced it. Yes. I wrote a repro script using the repo's
MockOpenAIclient withOpenAIChatModel('deepseek-reasoner', provider=DeepSeekProvider(...)), an agent withCapability(id='DICE_ROLL', tools=[roll_dice], defer_loading=True), and mocked responses carryingreasoning_content. Dumping the outgoingmessagespayload showed the root cause: after the model callsload_capability,ToolSearch.before_model_requestappends a framework-synthesizedModelResponse(parts=[ToolSearchCallPart(...)])(auto_load_*search_toolscall). On the wire that becomes anassistantmessage withtool_callsand noreasoning_content, which is exactly what DeepSeek rejects with400 - The 'reasoning_content' in the thinking mode must be passed back to the API. Withdefer_loading=Falseno such synthetic message exists — matching the issue report.2. If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes. In
OpenAIChatModel._map_messagesI now fold framework-synthesized, tool-call-onlyModelResponses into the last real model response, but only when that response's thinking is actually echoed back in a dedicated field (DeepSeek/Moonshot-stylereasoning_content). After the fix, the repro's payloads contain no thinking-less assistant message: thesearch_toolscall rides on the assistant message that carriesreasoning_content, and both tool returns follow it, so call/return pairing is intact. I also verified with a direct_map_messagescomparison that a plain OpenAI model's wire shape is completely unchanged.3. I have thought about edge cases and made sure my fix handles them as well.
- Only responses with no provider/model identity (
provider_response_id/provider_name/model_nameallNone) and containing exclusivelyToolCallParts are folded — real provider responses are never mangled. - Folding only happens when the target response has a
ThinkingPartthat this profile sends back in a field, so'tags',Falseand plain-OpenAI histories go out byte-identical (no cache/behavior changes for other providers). - Cross-provider histories,
'auto'detection (id/provider/custom-field matching) and the'field'mode are all handled via a shared helper_auto_thinking_field, which I refactored out of_map_response_thinking_partso both paths can't drift. - Message history (
all_messages()) is untouched, so serialization,discovered_tool_namesparsing and cross-provider replay keep working; the change is wire-only, applies to streaming and non-streaming (shared_map_messages) and is inherited by subclasses likeOpenRouterModelthat callsuper()._map_messages.
4. I have run existing tests in the repository that might have been affected and confirmed that they pass. Yes.
tests/models/test_openai.py,tests/models/test_deepseek.py,tests/models/test_openrouter.py,tests/models/test_ollama.py,tests/models/test_cerebras.py,tests/test_capabilities.py,tests/test_tool_search.py,tests/test_agent.pyand the wholetests/modelstree all pass. The full suite showed only pre-existing failures (ahuggingface_hubdeprecation warning intest_init/test_model_*and three MCP subprocess tests), which I confirmed fail identically on a stashed (clean) tree.ruff format --check,ruff checkandpyrightare clean on the modified file. No tests were modified or added.- Only responses with no provider/model identity (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →