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
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
I’ll first inspect the repository structure and locate capability deferral, DeepSeek/OpenAI message serialization, and relevant tests. Then I’ll reproduce the history transformation locally (without requiring an API key), patch the message propagation, and run focused tests plus the appropriate broader suite.
- Assistant
The initial combined search returned no matches, so I’ll inspect package layout and search repository-wide with broader capability/model terms.
- Assistant
ripgrepis unavailable in this environment, so I’ll use recursivegrepand inspect the matched implementation files directly. - Assistant
The likely failure point is the graph’s deferred-capability interception: it may construct a replacement/synthetic
ModelResponse. I’ll inspect that branch and the OpenAI chat mapping logic to determine whether thinking is discarded before serialization or merely omitted on the wire. - Assistant
OpenAI mapping correctly round-trips a DeepSeek
ThinkingPart(id='reasoning_content', provider_name=...), so the loss likely occurs when capability loading rewrites tool calls or filters response parts. I’ll inspect the deferred loader toolset and all graph/model history filtering and replacement operations. - Assistant
Deferred capabilities also activate a local tool-search capability, which creates a synthetic response. I’ll inspect that interception and the request-history preparation/cleaning code around the response normalization points identified above.
- Assistant
The synthetic exchange does not directly replace the original response, and cleaning only merges synthetic responses lacking provider identity. A more subtle possibility is provider identity mismatch: field-mode serialization requires
ThinkingPart.provider_name == model.system. I’ll inspect DeepSeek provider/profile construction and build a mocked DeepSeek agent run that captures each outgoing HTTP payload, reproducing the actual multi-turn behavior without network access. - Assistant
DeepSeek’s profile always uses field mode, so ordinary round-trip is correct. The problematic payload likely contains an additional framework-synthetic assistant tool-search call with no
reasoning_content; DeepSeek thinking mode rejects such assistant tool-call messages. The robust fix is to fold the synthetic discovery call into the preceding real assistant response (which retains its exact reasoning block), and fold its return into the following request, rather than append a second fake assistant turn. I’ll verify hook mutability/ordering and existing tests for this synthetic exchange before implementing that general history-preserving shape. - Assistant
Existing tests intentionally assert that the synthetic exchange persists, so changing the normal history shape globally would be disruptive. The fix should be narrowly activated when the preceding model response contains thinking. Before choosing between folding and suppressing synthesis, I’ll inspect how loaded capability tools become visible and why discovered-tool history is needed; if local OpenAI-compatible providers do not need that exchange for visibility, avoiding the fabricated assistant message is safer than altering a model-authored tool-call set.
- Assistant
Loaded capability tools are already revealed directly from
available_capability_ids; the synthetic record mainly establishes durable tool-search discovery semantics and native-provider replay. Nonetheless, suppressing it only for DeepSeek would be an undesirable provider special case. I’ll now reproduce the exact failing payload with anhttpx.MockTransport, including server-side validation that rejects any assistant tool-call message missingreasoning_content. This will confirm which message triggers the 400 and provide a deterministic verification harness. - Assistant
The reproduction confirms the original DeepSeek reasoning is present exactly, but the framework-generated
auto_load_*assistant call lacks the field and triggers rejection. The least invasive fix is in OpenAI Chat serialization: when a framework-synthetic assistant tool-call response (no provider/model/response identity) is sent to a profile requiring field-mode thinking, include that required field as an empty string. This truthfully represents that the synthetic framework turn has no model reasoning, preserves the original reasoning unchanged, and leaves stored history/snapshots/provider-neutral behavior intact. I’ll implement this with a narrow helper and then rerun the mock validation and focused tests. - Assistant
My first mock validator treated an empty field as absent, so it did not establish whether DeepSeek accepts empty reasoning; relying on that undocumented distinction is too fragile. I’ll instead use the structurally valid solution only for thinking responses: merge the framework-generated discovery call into the immediately preceding model assistant response and its return into the current tool-result request. This produces one assistant tool-call message carrying the exact original reasoning, with both corresponding tool results following it. Non-thinking providers retain the existing synthetic exchange and snapshots. I’ll revert the serializer experiment and implement this at the synthesis source.
- Assistant
The deterministic reproduction now succeeds: the second payload contains the exact
EXACT THOUGHT, both assistant tool calls in the same message, and both tool results in the following request. I’ll run the focused deferred-capability/tool-search tests (including persistence and exact snapshots) plus OpenAI thinking serialization tests, then lint/type-check the changed file. - Assistant
The first test invocation was aborted because two guessed node IDs did not match actual function names, so none ran. I’ll retrieve the exact nearby test definitions and rerun valid nodes without masking failures.
- Assistant
The baseline deferred-capability snapshot and OpenAI thinking tests pass, confirming non-thinking history remains unchanged. I’ll now run the exact persistence/long-trajectory tests and the full tool-search test module, followed by Ruff and Pyright (or the repository’s available equivalent) on the changed file.
- Assistant
All 159 focused tests pass, and Ruff/Pyright report no issues. I’ll inspect the final diff for accidental changes, then run the full capabilities module because the changed hook is shared across capability combinations and sequential loads.
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
Qwen/Qwen3.6-27BCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
deepseek/deepseek-v4-proCompare side by side →