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.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
- Assistant
I'll start by exploring the repository structure and locating the capability/defer_loading and DeepSeek-related code to understand why
reasoning_contentis dropped. - Assistant
I'll inspect deferred capability loading and how DeepSeek serializes
reasoning_contentin the message history. - Assistant
rg is not available, let me use grep instead.
- Assistant
I'll inspect OpenAI thinking-part mapping and the deferred capability loader next.
- Assistant
Let me read more of the
_map_response_thinking_partmethod and also look at deferred capability loading. - Assistant
I'll look for tests and message-history handling around deferred capabilities and thinking parts.
- Assistant
I'll inspect deferred tool loading and process_history next, looking for places that strip thinking content.
- Assistant
I'll dig into deferred capability loading message flow and any history rewriting.
- Assistant
I'll search for message rewriting paths that might drop ThinkingParts after load_capability.
- Assistant
I'll inspect
_clean_message_historyand run message-mapping paths that could strip thinking. - Assistant
Let me look at
_clean_message_historycarefully and also the modelRequestContext and before/after flows that might create synthetic responses without thinking. - Assistant
I'll check git history and related tests for deferred capabilities + thinking.
- Assistant
I'll open deferred-capability tests and the thinking wire-contract tests.
- Assistant
I'll inspect deferred-hookability and model-request wrapping when capabilities load mid-run.
- Assistant
I'll reproduce by mocking DeepSeek/OpenAI responses with
reasoning_contentand inspecting the second request. - Assistant
I'll inspect how the OpenAI chat request is built and whether
reasoning_contentis preserved. - Assistant
I'll search for related comments and how thinking + tool results are handled in the agent graph.
- Assistant
I'll check DeepSeek model construction and whether profile settings apply through agent runs.
- Assistant
I'll draft a mock reproduction script comparing deferred vs non-deferred message mapping.
- Assistant
I'll rerun the reproduction with model requests allowed.
- Assistant
The synthetic auto-load
search_toolsassistant message lacksreasoning_content— I'll inspect that injection path. - Assistant
I'll read the auto-load injection logic in tool search.
- Assistant
I'll check how provider_name is set on ThinkingParts for DeepSeek.
- Assistant
I'll check how the OpenAI chat path maps ToolSearch parts and whether they should be hidden from DeepSeek.
- Assistant
I'll inspect ToolSearch auto-load comments and related tests for reasoning edge cases.
- Assistant
I'll verify whether the OpenAI client strips
reasoning_contenton the wire. - Assistant
Let me try another approach to check OpenAI client serialization.
- Assistant
I'll check when ToolSearch is auto-injected and how search_tools is exposed for DeepSeek.
- Assistant
Let me look at agent init around auto-injection and also how messages get append to ctx.messages for the synthetic exchange.
- Assistant
I'll check how request_context message extensions get into the persisted history.
- Assistant
I'll inspect model filtering of synthetic tool-search messages and related comments.
- Assistant
I'll search for prior fixes or comments of synthetic tool-search messages and thinking.
- Assistant
I'll inspect how discovered tools are tracked after load_capability.
- Assistant
I'll inspect surrounding code and tests to choose the safest place for the fix.
- Assistant
Identified the issue: synthetic auto-load
search_toolsassistant messages omitreasoning_content, which DeepSeek rejects in thinking mode. I'll fix OpenAI chat mapping to include an empty field-mode thinking value for tool-call-only messages. - Assistant
I'll update
_into_message_paramso field-mode providers always sendreasoning_contenton tool-call assistant messages. - Assistant
The edit landed in the wrong file — I'll revert profiles and apply the fix to the OpenAI model.
- Assistant
Good, now edit the correct file.
- Assistant
I'll drop the extra type-ignore comments so the style matches the surrounding code.
- Assistant
I'll re-run the reproduction to confirm synthetic messages include empty
reasoning_content. - Assistant
I'll run a focused mapping unit check and related existing tests.
- Assistant
I'll run the existing OpenAI thinking and capabilities tests next.
- Assistant
I'll run deferred-capability-related tests next.
- Assistant
I'll verify the final diff is clean and correct.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.