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.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
I’ll trace how deferred capability turns reconstruct message history for OpenAI-compatible models, compare that with normal tool-call turns, then add a regression test for DeepSeek reasoning content and implement the smallest compatible fix. I’ll stay local as requested.
- Assistant
The repository’s architecture confirms this should be fixed in normalized message/history or the OpenAI-compatible adapter—not in the capability itself—so deferred loading remains provider-agnostic. I’m now locating the exact history rewrite and DeepSeek serialization paths.
- Assistant
I reproduced the failure shape locally. The original DeepSeek
reasoning_contentis preserved correctly; the problematic entry is a syntheticsearch_toolsassistant/tool pair inserted afterload_capability. That synthetic assistant message has a tool call but no reasoning field, which DeepSeek rejects when replaying thinking-mode history. - Assistant
The correct compatibility behavior is to emit the configured thinking field with an empty string for assistant tool-call messages that have no
ThinkingPart. In DeepSeek field mode, that represents the exact absence of reasoning for framework-synthesized calls while preserving real reasoning verbatim on model-produced calls. I’m adding an end-to-end agent regression test that inspects the second request payload. - Assistant
The regression test now passes and confirms both cases in the same payload: DeepSeek’s real reasoning is replayed unchanged, and the synthetic tool-search call carries
reasoning_content: "". I’m running the adjacent reasoning tests plus targeted lint/type checks to catch compatibility or typing regressions. - Assistant
All 201 OpenAI adapter tests pass, along with the deferred-capability workflow test, Ruff, Pyright, and whitespace checks. The fix remains confined to Chat Completions message serialization plus its DeepSeek regression test.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.