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.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.
- Assistant
I’ll trace the deferred-capability message round trip and fix it without changing public interfaces. I’m using the
complete-partial-prskill because this provider-specific symptom may involve adjacent serialization and regression-test surfaces; I’ll also follow the repository and affected-directory guidelines. I’ll assume the intended invariant is that all provider metadata, especially DeepSeek reasoning content, survives capability deferral exactly. - Assistant
The repository has no
rg, so I’m usinggrep/findfor the local investigation. The working tree is clean and detached; there is no local PR patch to preserve. The key question is now whether capability deferral reconstructs a response from only selected parts instead of preserving the original normalized response. - Assistant
The normalized history path already preserves sibling
ThinkingParts during capability-call promotion, and the OpenAI adapter knows how to serialize them. I’m moving to an end-to-end mock of the exact DeepSeek sequence—reasoning plusload_capability, then the loader result—to identify the actual request payload that differs from the provider contract rather than patching the graph speculatively. - Assistant
The reproduction exposes the fault: the real DeepSeek assistant message retains its exact
reasoning_content, but local deferred loading then injects a second, synthetic assistantsearch_toolscall with no reasoning content before the next request. DeepSeek validates every assistant tool-call message in thinking mode, so that framework-generated message is invalid. The fix should avoid inventing a provider-facing assistant turn for capability-owned tools that are already revealed byload_capability. - Assistant
I’m applying a narrow wire-preparation fix: only OpenAI Chat histories where a framework-generated auto-discovery call follows a reasoning-bearing
load_capabilityresponse are coalesced for serialization. The stored normalized history remains unchanged, preserving replay/idempotence and avoiding changes to OpenAI Responses or native tool-search semantics. - Assistant
The regression test now passes and confirms the second DeepSeek request contains one assistant message with the original reasoning plus both tool calls. I’m checking typing and adjacent behavior next, especially that ordinary capability histories retain their existing stored synthetic exchange and that other thinking serialization modes are unaffected.
- Assistant
All targeted checks now pass, including static typing. The local review confirms the change is wire-only: the synthetic discovery pair remains in Pydantic AI message history for replay and cross-provider state, while Chat Completions receives a valid assistant/tool sequence carrying DeepSeek’s exact reasoning content. No public API or documentation changes are needed.
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
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare 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 →
- fable-5Compare side by side →
- fable-5Compare side by side →