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’m using the
complete-partial-prworkflow because this provider-specific symptom may involve message round-tripping beyond the immediate deferred-capability path. I’ll inspect the repository guidance and existing DeepSeek/reasoning handling, then add a focused regression test and verify the fix locally. I’ll proceed from the issue description since external access is explicitly unavailable. - Assistant
The repository’s normalized message layer is meant to preserve provider-specific reasoning data, while capabilities should stay provider-agnostic. I’m tracing where deferred loading rewrites history and where the OpenAI-compatible adapter serializes reasoning, including streaming and non-streaming variants.
- Assistant
The failure is reproducible locally. The original DeepSeek
reasoning_contentis preserved correctly, but loading a capability also inserts a framework-generatedsearch_toolsassistant/tool exchange into history. That synthetic assistant message has no DeepSeek reasoning block, which makes the next DeepSeek request invalid. I’m checking whether capability-owned tools should bypass the separate deferred-tool discovery step once their capability is loaded; that appears to be the actual abstraction boundary. - Assistant
The regression now passes: the real DeepSeek reasoning block is replayed unchanged, and the framework-generated capability-discovery assistant message carries an explicit empty
reasoning_contentfield. This is limited to OpenAI-compatible profiles configured for field-based reasoning replay, so normal OpenAI and tag-based reasoning histories are unchanged. - Assistant
Targeted verification is clean: all DeepSeek model tests pass, the existing field/tag/disabled reasoning cases pass, deferred-capability replay remains singular, and both Ruff and Pyright report no issues. I’m doing the final diff review now, with particular attention to ensuring ordinary tool-search calls do not receive synthetic reasoning fields.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.