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:
- Assistant
Now I'll implement the fix in
ToolSearch.before_model_request: - Assistant
Now verify the repro is fixed:
- Assistant
The synthetic exchange is gone and the run completes. Let me confirm the revealed tools still reach the wire in the
toolsparam after the load: - Assistant
Tools are hidden pre-load and revealed post-load via the
toolsparam. Now run the existing test suites that cover this path: - Assistant
All pass. Now add a regression test for the new behavior, next to the existing synthetic-exchange tests. Let me check the helpers available in the test file:
- Assistant
All failures are
NotImplementedErrorintest_fallback.py—FallbackModel.profilelikely raises. Let me check: - Assistant
Docs remain accurate (they never promised a synthetic exchange on local providers — only on native surfaces). Let me run the full test suite in the background to catch anything else:
- Assistant
While that runs, review the final production diff:
- Assistant
The full suite is still running (output arrives when it completes). Let me wait for it:
- Assistant
Waiting on the full-suite run to finish — I'll report as soon as it completes.
- Assistant
One gap: the
except NotImplementedErrorbranch only triggers when a deferred capability loads under aFallbackModel— no existing test covers that, and the repo requires 100% coverage. Add a test: - Assistant
Working tree is in the expected state (one production file + two test files modified, uncommitted). Awaiting the full-suite result from the background run — I'll confirm final status when it lands.
- Assistant
I'll stop here and let the monitors notify me when the suite finishes.
- Assistant
make testuses pytest-xdist (-n auto --dist=loadgroup) — my single-process invocation isn't the supported configuration, which explains the Temporal dev-server clashes. Waiting for the no-temporal suite run to confirm everything else is green. - Assistant
While that runs, check the likely durable-exec suspects standalone (
IntegrityErrorat line 1717 smells like Prefect's SQLite under contention): - Assistant
Waiting for the run excluding both flaky durable-exec suites — that will show whether any remaining failure is real.
- Assistant
Confirmed pre-existing: the Outlines/llama.cpp failures are a
huggingface_hubdeprecationUserWarningraised as an error inside the third-partyllama_cpp.from_pretrained— nothing to do with this change. Waiting for the final confirmation run (suite minus temporal/dbos) to land. - Assistant
All verification is complete. Final suite state (excluding the environment-broken suites): 7681 passed, with the only failures being 2 llama.cpp tests + 6 llamacpp setup errors caused by a third-party deprecation warning — pre-existing dependency drift, unrelated to this change. The other pre-existing environment failures (Temporal: passes 94/94 standalone, fails only under single-process whole-suite contention; DBOS: 2.10.0 SQLite schema bug) are also untouched by the diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.