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
- Python:
3.14 - Pydantic AI:
1.106.0 - LLM provider SDK: deepseek
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pydantic-ai, current file: none) bash-$
Assistant
Let me understand the issue:
-
When using DeepSeek reasoning models with
Capability(..., defer_loading=True), it throws a 400 error because DeepSeek requires thereasoning_contentfrom the thinking phase to be passed back in subsequent conversation messages. -
When
defer_loading=True, the graph loop seems to drop or fail to serialize/propagate thereasoning_contenttoken block.
Let me explore the repository structure first to understand how it's organized and find relevant code.
-
Assistant
Let me explore the key files related to capabilities and the agent execution loop.
Assistant
Let me look at the relevant files. First, let me understand the deepseek provider and how reasoning_content is handled
Assistant
Now let's look at the OpenAI model implementation and the deferred capability loader
Assistant
Let me search for the key profile fields and how thinking parts are handled in message construction.
Assistant
Let me look at the critical section of
openai.pyaround line 1372 where thinking parts are handled in message constructionAssistant
Let me use the proper command to open the file.
Assistant
Now let me look at the deferred capability loader
Assistant
Let me look at the
DeferredCapabilityLoaderToolsetand also the agent graph loop to understand how messages are processedAssistant
Let me look at the agent graph/loop to understand how messages are processed
Assistant
Let me look at
_agent_graph.pywhich is the core agent graph loop.Assistant
Let me look at the ModelRequestNode class in `_agent_graph
Assistant
Let me look at the ModelRequestNode and how it handles the model response, specifically the
_make_requestmethod and how the response is stored.Assistant
Let me use the bash command to open the file.
Assistant
Let me continue reading
_make_requestand the response handling code.Assistant
Now let me look at
_finish_handlingmethodAssistant
Let me look at these methods:
Assistant
Let me look at how the OpenAI model converts internal messages to the OpenAI API messages format:
Assistant
Let me look at
_map_messagesat line 1445 to understand how messages are converted:Assistant
Let me use the appropriate command.
Assistant
Now let me look at
_map_model_responseto see how it maps internal responses to OpenAI APIAssistant
Let me look at
_map_model_responseat line 1427:Assistant
Let me look at how the model response is constructed from the OpenAI streaming response, specifically how thinking parts are handled
Assistant
Let me look at
_process_thinkingto understand how thinking parts are extracted from non-streaming responsesAssistant
Let me look at
_map_thinking_deltaand how streaming thinking parts are handled:Assistant
Let me continue reading the
_run_streammethod to understand how tool calls are processedAssistant
Let me look at how the non-streaming model response is processed to create the ModelResponse with thinking
Assistant
Let me look at
_process_responseat line 1056 to see how the ModelResponse is constructedAssistant
Let me look at
process_tool_callsto understand how tool calls are processed:Assistant
Let me read the
process_tool_callsAssistant
Let me continue reading the function to see how results are processed
Assistant
Let me look at the capability hooks, specifically
after_model_requestto see if any capability might strip thinking partsAssistant
Let me look at the
process_history.pycapability since it's the one that likely processes history before model requests.Assistant
Let me look at the thinking capability
Assistant
Let me look at the deepseek tests to understand the expected
Assistant
Let me look for tests related to deferred capability tools
Assistant
Let me check the profile update mechanism and the deepseek model
Assistant
Let me look at the
updatemethod onOpenAIModelProfile:Assistant
Let me look at the ModelProfile class to find
updateAssistant
Let me look at the
updatemethod:Assistant
Let me look at the
ThinkingPartclass in the messages moduleAssistant
Let me look at the
ThinkingPartclass:Assistant
Let me check the OpenAI SDK types to see if
ChatCompletionAssistantMessageParamsupports extra fields likereasoning_contentAssistant
Let me look at the full definition of
ChatCompletionAssistantMessageParam.Assistant
Let me check how the OpenAI SDK handles the messages before sending them to the API
Assistant
Let me look at the
createmethod in the OpenAI SDK to see if it transformsAssistant
Let me check how the OpenAI SDK sends the request - specifically if it strips extra
Assistant
Let me check
maybe_transformin the OpenAIAssistant
Let me look at the
maybe_transformfunction:Assistant
Let me see the full transform_recursive:
Assistant
Let me look at
_transform_recursivefrom line 154:Assistant
Let me look at
_transform_typeddict:Assistant
Let me look at
_transform_typeddict:Assistant
Let me check
ChatCompletionMessageParam- the union type for messagesAssistant
Let me look at `ChatCompletionMessageParam
Assistant
Let me check if there are any subclasses of
_MapModelResponseContextthat might override behavior:Assistant
Let me look at OpenRouter's
_MapModelResponseContextAssistant
Let me check
OpenAIModelProfile.from_profile:Assistant
Let me search more broadly for
from_profile:Assistant
Let me look at
from_profile:Assistant
Let me look at how
OpenAIChatModelsets its profile:Assistant
Let me look at the
__init__ofOpenAIChatModelto understand how the profile is setAssistant
Let me look at the
Modelbase class__init__:Assistant
Let me look at
Model.__init__:Assistant
Let me look at the
profileproperty ofModel:Assistant
Let me look at the
profileproperty at line 629:Assistant
Let me check
is_givenin the OpenAI SDK:Assistant
Let me look at
is_given:Assistant
Let me search for
after_model_requestimplementations that might modify the response, particularly around thinking partsAssistant
Let me check the hooks
after_model_requestimplementation:Assistant
Let me look more closely at the test files for thinking-related tests and deferred capabilities
Assistant
Let me look at the git log for any recent changes related to this issue:
Assistant
Let me look at the diff for the deferred loading feature commit to understand the changes
Assistant
Let me look at the changes in
combined.pyfor the deferred loading featureAssistant
Let me search for how the model name with provider prefix is resolved
Assistant
Let me look at
parse_model_idandinfer_modelto understandAssistant
Let me continue reading `infer_model
Assistant
Let me check what
OpenAIChatCompatibleProviderincludes:Assistant
Let me look at the `OpenAIChatCompatibleProvider
Assistant
Let me write a script to test the message mapping with thinking
Assistant
Let me run the script from within the repository properly:
Assistant
Let me use the virtual environment
Assistant
Let me test if the OpenAI SDK's
maybe_transformpreservesreasoning_content:Assistant
Let me look at
_call_toolsto understand how tool results are processedAssistant
Let me read
_call_tools:Assistant
Let me look at
_call_tool:Assistant
Let me look at
_call_toolAssistant
Let me look at how instructions are built for the model request, particularly around deferred
Assistant
Let me look at
_get_instructionsin_agent_graph.pyAssistant
Let me look at
_get_instructionsAssistant
Let me look at
_clean_message_historymore carefullyAssistant
Let me find and look at
_clean_message_history:Assistant
Let me look at
_clean_message_historyAssistant
Let me look at
_translate_thinkingmethod in openai.pyAssistant
Let me look at the thinking-related tests, especially those involving deferred capabilities
Assistant
Let me test what the OpenAI SDK sends when
contentisNone:Assistant
Let me test with two assistant messages (simulating multiple turns):
Assistant
Let me look at how
ModelResponsePartsManagerhandles thinkingAssistant
Let me look at
ModelResponsePartsManagerandhandle_thinking_deltain `_parts_managerAssistant
Let me look at how
thinkingis set in model request parametersAssistant
Let me search for where
openai_chat_send_back_thinking_partsis set toFalse:Assistant
Let me search for tests that combine thinking and deferred capabilities:
Assistant
Let me look at the issue from yet another angle. Let me look at
_process_responseto understand the flowAssistant
Let me look at the diff for
_agent_graph.pyin the deferred loading commit:Assistant
Let me search for all overrides of
_map_response_thinking_part:Assistant
Let me look at the Groq model's thinking handling as it also has reasoning support:
Assistant
Let me check if DeepSeek API may require
contentto be a string rather thannullwhenreasoning_contentis present:Assistant
Let me look at
_completions_createto see how the final request is constructedAssistant
Let me look at
_completions_create:Assistant
Let me look at
_drop_sampling_params_for_reasoning:Assistant
Let me look at these functions:
Assistant
Let me check the default value of
openai_supports_reasoninginOpenAIModelProfile:Assistant
Let me check the OpenAI SDK version:
Assistant
Let me check what
model_response_str_and_user_contentdoes with dict content:Assistant
Let me look at
model_response_str:Assistant
Let me look at
_get_tool_choice:Assistant
Let me look at
_get_tool_choice:Assistant
Let me check if the OpenAI SDK strips
Nonevalues before sending:Assistant
Let me look at the
Instrumentationcapability, particularly itsafter_model_request:Assistant
Let me search for any related issues in the codebase or git log:
Assistant
Let me check if
ThinkingPart.contentcould be empty or if there's a case where it's not stored:Assistant
Let me check if
reasoning_contentexists in the OpenAI SDK'sChatCompletionMessage:Assistant
Let me look at the full
ChatCompletionMessagemodel:Assistant
Let me check if
ChatCompletionMessageallows extra fields:Assistant
Let me check the OpenAI SDK's BaseModel configuration for extra fields:
Assistant
Let me verify that Pydantic BaseModel with
extra="allow"supportsgetattrfor extra fields:Assistant
Let me look at
build_run_context:Assistant
Let me search for where
openai_chat_send_back_thinking_partsis set toFalseor where it might be overridden:Assistant
Let me check if the model could be wrapped in a way that changes the profile:
Assistant
Let me check if the agent wraps the model in an InstrumentedModel:
Assistant
Let me look at the agent code to see if the model is wrapped:
Assistant
Let me look at the
requestmethod ofOpenAIChatModel:Assistant
Let me look at the full
_MapModelResponseContextclass definition:Assistant
Let me check if
DeferredCapabilityLoaderhasdefer_loadingset:Assistant
Let me check the default
defer_loadinginAbstractCapability:Assistant
Let me search for
capability-loadin the codebase to see if it has any special handling:Assistant
Let me look at the deepseek reasoning_content streaming fix:
Assistant
Let me look at the diff for the deepseek reasoning_content streaming fix:
Assistant
Let me search for code that might filter or transform model response parts in a way that could drop thinking:
Assistant
Let me look at
process_history.pyagain:Assistant
Let me run the thinking-related tests to see if any fail:
Assistant
Let me look at
parse_loaded_capabilities:Assistant
Let me check if the OpenAI SDK strips
nullvalues from the request body:Assistant
Let me look for tests that combine deferred capabilities with deepseek:
Assistant
Let me test
OpenAIModelProfile.from_profileto make sure it preserves the thinking fields:Assistant
Let me look at how the agent sets up capabilities and the run:
Assistant
Let me look at the code around line 1394:
Assistant
Let me check if the deferred loading commit changed anything in
openai.py:Assistant
Let me look at the full context around the
_prepare_requestmethod to understand the ordering:Assistant
Now I have a good understanding of the codebase and the issue. Let me implement the fix. The key issue is in
_map_response_thinking_partwhich lacks anelseclause for whenopenai_chat_send_back_thinking_partsisFalseor unexpected - thinking parts are silently dropped. But more importantly, I need to check if the fix should be elsewhere.Let me re-examine the most likely root cause. Let me trace the exact flow for
defer_loading=Truewith DeepSeekAssistant
Let me fix and rerun the script:
Assistant
I've thoroughly analyzed the code. The mapping correctly preserves
reasoning_contentin assistant messages. However, I found a critical issue: the_map_response_thinking_partmethod lacks anelseclause for whenopenai_chat_send_back_thinking_partsisFalseor unexpected - thinking parts are silently dropped. Let me fix this:Assistant
Let me view the relevant section of the code
Assistant
Let me now look at
_into_message_paramAssistant
Let me look at how
StreamedResponse.get()works and how parts are collected from streaming:Assistant
Let me look at the base
StreamedResponse.get():Assistant
Let me look at
StreamedResponse.get():Assistant
Now let me implement the fix. I'll add an
elseclause to handle unexpectedinclude_methodvalues and also ensurecontentis not set toNonewhen tool_calls are present (to avoid potential issues withcontent: null):Assistant
Let me edit the file to add the
elseclauseAssistant
Let me use the replace command to make the edit:
Assistant
Now let me also fix the
_into_message_parammethod to not setcontent=Nonewhen tool_calls are present (which is more correct per OpenAI API spec):Assistant
Let me check the exact content:
Assistant
Let me try with the correct indentation:
Assistant
Let me fix the escaping:
Assistant
Let me debug the pattern matching:
Assistant
Let me check the raw content more carefully:
Assistant
I see the issue - I need to use
\\nin the Python string to match the file content. Let me fix:Assistant
Now let me verify the changes:
Assistant
Now let me verify the fix works correctly:
Assistant
Let me check and fix the syntax error:
Assistant
Let me check the raw bytes at the problematic line:
Assistant
Let me use od:
Assistant
Let me see the full line:
Assistant
The replacement introduced a real newline. Let me fix it:
Assistant
Let me verify the fix and run the test:
Assistant
Let me verify with od:
Assistant
Now let me run the test:
Assistant
The fix works correctly. Let me now run the existing tests to make sure nothing is broken:
Assistant
Let me also run the broader test suite for affected areas:
Assistant
Let me look at the failing test to understand what's expected:
Assistant
I need to fix the
elseclause. Wheninclude_methodisFalse, thinking should be dropped (as the user explicitly disabled it). Let me update:Assistant
Let me verify and rerun the tests:
Assistant
Let me run the full test suite:
Assistant
Let me also run the capabilities and thinking tests:
Assistant
Let me check the git diff to see the changes:
Assistant
I believe the key fix is the
contentchange. Let me verify this scenario:Assistant
The fix looks correct. Let me run a broader set of tests to make sure nothing is broken:
Assistant
Let me verify one more edge case - when there's thinking with text and tool calls:
Assistant
Now let me submit the changes. But first, let me verify the final diff
Assistant
I'm confident the changes are correct and resolve the issue. Let me submit
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →