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.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 capability deferred-loading graph/message-history handling and the OpenAI/DeepSeek serialization path, then add the smallest compatible fix. I’ll inspect the repository and relevant tests first, reproduce via focused tests or a temporary mock if possible, implement, and run affected tests.
- Assistant
The failure mechanism is clear: loading a deferred capability injects a synthetic
ModelResponse/ModelRequesttool-search exchange immediately after the real DeepSeek assistant response. The OpenAI Chat mapper serializes that synthetic response as a second assistant message, which has noreasoning_content; DeepSeek rejects the follow-up history even though the original response did contain it. I’ll coalesce that synthetic tool-search call into the preceding assistant message on the wire, preserving the original reasoning field exactly while retaining the framework’s history structure. - Assistant
The fix is implemented and validated. It is narrowly scoped to synthetic
ToolSearchCallPartresponses, so ordinary assistant messages and tool calls retain their existing serialization.compileall,ruff,git diff --check, and all 200 OpenAI model tests pass; the two DeepSeek cassette tests also pass. A live DeepSeek reproduction was not possible without credentials, but the exact serialized-history defect is covered by the code path and the fix preservesreasoning_contenton the assistant message. I’m submitting now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.