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
Interface
No new interfaces are introduced.
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 start by exploring the repository structure to understand the codebase.
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>Now let me look at where reasoning_content is handled and where DeepSeek related processing is done.
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>Now let me understand what happens with capability loading and how it might intercept the loop. Let me trace where the load_capability call modifies message history.
Assistant
</mm:think>
Assistant
</mm:think>Now let me look at the deferred capability loader to understand how
load_capabilityworks and how instructions get added.Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>Now let me look at the message processing related to tool calls and tool returns, specifically how instructions get added between them when the load_capability is invoked.
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>Now let me look at the tests for DeepSeek to see how they handle this.
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:Think>
Assistant
</mm:think>Let me look at how tool call parts are mapped to OpenAI requests to understand the flow better.
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>Now let me trace what happens during the load_capability call. Specifically, when the model returns a tool call for
load_capability, how does it get back to the model in subsequent turns?Let me first reproduce the issue. Let me look at how to create a test:
Assistant
</mm:think>
Assistant
</mm:Think>Now let me look at how tool calls are mapped to the OpenAI Chat API requests, particularly looking at how the load_capability tool's tool_calls are processed. The issue is that the load_capability tool call is sent back to the DeepSeek API, and DeepSeek's API has special requirements that the original reasoning_content from the assistant must be passed back in the same assistant message.
Let me think about this carefully. When the model generates a thinking block, then a load_capability tool call, the resulting ModelResponse has:
- ThinkingPart with
id='reasoning_content'andprovider_name='deepseek' - ToolCallPart with the tool call
This is then mapped to OpenAI Chat completion message. When
_map_response_thinking_partis called for the ThinkingPart, sinceopenai_chat_send_back_thinking_parts='field', it puts the reasoning_content into thereasoning_contentfield of the assistant message. Then the tool_calls field gets the load_capability tool call. That should work.But wait - looking at the issue more carefully, the
load_capabilitytool hastool_kind='capability-load'. Let me see how that tool call gets processed:- ThinkingPart with
Assistant
</mm:think>
Assistant
</mm:think>Now let me look at how the load_capability tool call is mapped to OpenAI's chat completion assistant message:
Assistant
</mm:Think>
Assistant
</mm:think>Let me look at the issue more carefully. When the model uses
load_capability, the assistant message returned by the model has both a ThinkingPart and a ToolCallPart. The_map_model_responsemethod then maps this to an OpenAI ChatCompletionMessageParam that includes both thinking (asreasoning_contentfield) and tool_calls.Now, looking at the logic in
_map_response_thinking_part, when the thinking part was loaded withid='reasoning_content'andprovider_name='deepseek', and the profile saysopenai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_field='reasoning_content', then it should go intomessage_param['reasoning_content']. Let me verify the logic in more detail.Actually, the logic checks:
if ( item.id and item.id != 'content' and item.provider_name == self._model.system and matches_custom_field ):So we need
item.id == 'reasoning_content',item.id != 'content',item.provider_name == 'deepseek'(or whatever the model's system is), anditem.id == custom_field. This should pass for DeepSeek.Now the
automode is being used here:Assistant
</mm:think>OK so DeepSeek sets:
openai_chat_thinking_field='reasoning_content'openai_chat_send_back_thinking_parts='field'
Now let me check the load_capability flow. The thinking part goes through
_map_response_thinking_part, which usesinclude_method == 'field', which sets:self.thinkings.setdefault(field, []).append(item.content)Now let me check the
load_capabilitytool flow. Let me search for where the load_capability tool'stool_kind='capability-load'might be different:Assistant
</mm:think>
Assistant
</mm:think>
Assistant
</mm:think>Let me check the ToolReturnPart related processing when load_capability is involved. First, let me understand the flow:
Assistant
</mm:think>Now let me look at how the model handles the message history that includes the load_capability tool's call/return pair. The issue is that DeepSeek requires reasoning_content to be passed back unchanged in the same assistant message. Let me check if there's any separation happening.
Let me also check the agent loop and how messages are sent.
Assistant
Now I understand the flow. Let me now create a reproduction script to confirm the issue.
Assistant
Now let me look at how messages are processed when sent back to the model. Specifically, I want to understand if a
load_capabilitytool call would cause thereasoning_contentto be sent back to DeepSeek in a separate message, breaking the API requirements.Assistant
Now let me look at how to actually reproduce the issue. Let me check how the test mock works:
Assistant
Now let me write a reproduction script to confirm the bug:
Assistant
Let me look at the openai mock and create a reproduction script:
Assistant
Let me look at an existing test that uses OpenAI mocks with
reasoning_content:Assistant
Now let me write a reproduction script:
Assistant
Let me write the reproduction differently:
Assistant
I have a problem with my mock - the return type isn't being treated as a chat completion. Let me fix the mock:
Assistant
I need to use the proper chat.Completion return type. Let me look at how the mock works:
Assistant
I've now reproduced the issue. Looking at Request 3 (the last assistant message before getting the final response):
{'role': 'assistant', 'reasoning_content': 'Now let me roll the dice.', 'content': None, 'tool_calls': [{'id': 'call_roll_1', ...}]}This is the previous assistant message that gets sent back. It includes
reasoning_contentwhich DeepSeek requires.Now wait, looking at the issue more carefully - the bug report says the error happens. Let me look at the test output more carefully. It actually went through fine in my reproduction. Let me re-read the issue.
Looking at the issue:
Setting
defer_loading=Falsesolves the issue, and the agent executes successfully.So in this example with
defer_loading=True, the agent fails. Let me look at what's special in the trace... the error message says:The
reasoning_contentin the thinking mode must be passed back to the API.This means the reasoning_content field must be present in the assistant message back to DeepSeek. In my reproduction, the messages were properly sent back, so my reproduction doesn't actually reproduce the bug. Let me check if there's an issue in a more complex scenario.
Wait, the issue might be that the
thinkingpart has the tool call in the same assistant message. The issue description says the bug occurs "during execution loop turns". Let me check what happens after the tool returns.Actually, looking carefully at the output:
- Request 2 (after load_capability returned): The assistant message has
reasoning_contentproperly. - Request 3 (after tool search returned): The previous assistant message in Request 3 should be the one with the tool search tool call. That's the issue - the tool search returned an assistant message (with
ToolSearchCallPart) and thereasoning_contentwas NOT in that assistant message.
Wait, but the tool search is auto-generated by the framework, not by the model. Let me look at how it's created:
- Request 2 (after load_capability returned): The assistant message has
Assistant
Now I see the issue! When
load_capabilitysucceeds, theToolSearchcapability appends a syntheticModelResponsewith aToolSearchCallPartto the message history. This synthetic response is generated by the framework (NOT by the model), so it doesn't have anyreasoning_content. Then when this is sent back to DeepSeek, the prior assistant message (the one with theload_capabilitytool call) HASreasoning_content, but the framework-appended synthetic response is sent AFTER it as a separate message.Wait, but the issue is when DeepSeek thinks, it requires the reasoning_content to be in the same assistant message as the tool calls. Let me trace through what actually happens:
-
Turn 1:
- Request: System prompt (deferred cap catalog) + user prompt
- Response: Assistant message with reasoning_content "I need to load..." + tool_calls=[load_capability]
-
Turn 2:
- Now the model history has:
- System prompt
- User prompt
- Assistant message: reasoning_content + tool_calls=[load_capability]
- User message (tool): ToolReturnPart with content={}
- The framework also adds:
- Assistant message (synthetic): tool_calls=[search_tools] (this is the tool search injection)
- User message (tool): ToolSearchReturnPart with discovered_tools
- Response: Assistant message with reasoning_content "Now let me roll..." + tool_calls=[roll_dice]
- Now the model history has:
-
Turn 3:
- Now the model history has:
- ... all of the above
- Assistant message: reasoning_content + tool_calls=[roll_dice]
- User message (tool): ToolReturnPart content='4'
- Response: Assistant message with text 'The dice shows 4, you win!'
- Now the model history has:
So the model gets back the synthetic ToolSearchCallPart assistant message. According to DeepSeek's API, when a tool call is in an assistant message, the reasoning_content field needs to be in the same message. So when we send the assistant message with the
ToolSearchCallPart(the synthetic one), we should ALSO add thereasoning_contentfield, or the API will reject it.Wait, let me re-read the issue and look more carefully. The error is:
The
reasoning_contentin the thinking mode must be passed back to the API.This suggests DeepSeek is complaining that the reasoning_content is missing. So actually the issue is that the assistant message that has tool calls but no reasoning_content is invalid. Let me look at the DeepSeek docs:
-
Assistant
Now let me look at the ToolSearch more carefully to understand:
Assistant
Now I see! When the load_capability succeeds and unlocks tools, the ToolSearch capability (which is auto-injected) creates a synthetic
ModelResponsewith aToolSearchCallPart(a tool call) and aModelRequestwithToolSearchReturnPart(a tool return).This synthetic response is sent back to the model. The issue is that this synthetic assistant message doesn't have
reasoning_content. With DeepSeek, when the message history includes thinking mode (has previous messages withreasoning_content), then EVERY assistant message that has tool_calls must also havereasoning_content.Wait, but looking at the output of my reproduction, Request 2 has the synthetic message:
{'role': 'assistant', 'content': None, 'tool_calls': [{'id': 'auto_load_2a8a018167a344bc', 'type': 'function', 'function': {'name': 'search_tools', ...}}]}And Request 3 has the previous assistant message:
{'role': 'assistant', 'reasoning_content': 'Now let me roll the dice.', 'content': None, 'tool_calls': [{'id': 'call_roll_1', ...}]}This should be fine because the assistant message in Request 3 DOES have reasoning_content. The synthetic one doesn't matter because it precedes the real assistant message.
Let me look at this more carefully. Maybe the actual error is that the synthetic message itself was sent to DeepSeek but without reasoning_content. Let me re-read the error message:
The
reasoning_contentin the thinking mode must be passed back to the API.OK, this error is saying that reasoning_content must be passed back. So maybe what's happening is that when DeepSeek sees an assistant message with
reasoning_contentfollowed by an assistant message WITHOUTreasoning_content, it errors out?Actually wait - looking at the issue again, the user mentioned
deepseek-v4-flashmodel. Let me check if there's something different:Assistant
Now I see the issue clearly. Look at Request 2:
{'role': 'assistant', 'content': None, 'tool_calls': [{'id': 'auto_load_2a8a018167a344bc', 'type': 'function', 'function': {'name': 'search_tools', 'arguments': '{"queries":["DICE_ROLL"]}'}}]}This is the synthetic assistant message from the ToolSearch capability. It has
content: Noneand tool_calls but NOreasoning_contentfield. According to DeepSeek's API, when a previous message hasreasoning_content, all subsequent assistant messages that have tool_calls should also havereasoning_content.Wait, but DeepSeek's docs may be more specific. Let me search the openai.py for context.
Actually, I think I now understand the issue better. Let me re-read the error message:
The
reasoning_contentin the thinking mode must be passed back to the API.This is saying that when in thinking mode (i.e., the model has previously used reasoning), the
reasoning_contentMUST be passed back. So the framework-appended synthetic message (from ToolSearch) is missing thereasoning_contentfield, and DeepSeek is rejecting the request because of this.Now let me think about the fix. The issue is in
capabilities/_tool_search.py, the synthetic response needs to includereasoning_content. But that's only needed for models withopenai_chat_send_back_thinking_parts='field'.Let me think about this more carefully. Looking at the message:
- The synthetic message has tool_calls, but no
reasoning_contentfield.
Two possible fixes:
- Don't add the synthetic message at all (since it's just for tracking history of tool discoveries).
- Include the
reasoning_contentfield somehow.
Actually, looking at it more carefully, the synthetic message is needed because the model's tool_call needs to be "tracked" - it needs to be passed back to the model on subsequent calls. So we can't just remove it.
Let me look at how the synthetic message is handled in Anthropic and OpenAI Responses models:
- The synthetic message has tool_calls, but no
Assistant
Let me look at how OpenAI responses models handle this - they have a specific handling for ToolSearch:
Assistant
Now let me look at the Chat Completion flow specifically:
Assistant
So when the synthetic message goes through the chat completions API, it gets processed through
_map_model_response. Let me check what happens withToolSearchCallPartin that function:Looking at
map_assistant_message:def map_assistant_message(self, message: ModelResponse) -> chat.ChatCompletionAssistantMessageParam | None: for item in message.parts: if isinstance(item, TextPart): self._map_response_text_part(item) elif isinstance(item, ThinkingPart): self._map_response_thinking_part(item) elif isinstance(item, ToolCallPart): self._map_response_tool_call_part(item) ...ToolSearchCallPartis a subclass ofToolCallPart, so it would go through_map_response_tool_call_part. So that's fine.The issue is that when the synthetic message is added in
_tool_search.py, it has no thinking part. So when this gets sent back to DeepSeek, the assistant message has tool_calls but no reasoning_content. DeepSeek requires that when thinking mode is enabled (i.e., the model has been responding with reasoning_content), ALL assistant messages with tool_calls must also have reasoning_content.Now, the question is - what's the right fix?
Looking at the DeepSeek API docs, when in thinking mode, the assistant messages should preserve the reasoning_content field. When the framework synthesizes a tool call, it doesn't have the reasoning_content, so we should probably:
- Option A: When the model is in thinking mode and the framework synthesizes a tool call, copy the reasoning_content from the previous model message.
- Option B: When the model is in thinking mode and there's a synthetic tool call, use an empty string for reasoning_content.
- Option C: Make this a property of the model - the OpenAI chat model can include reasoning_content in the synthetic message based on the profile.
Actually, looking more carefully, I think the issue is different. The synthetic
ToolSearchCallPartmessage is from theToolSearchcapability. It's specifically a search_tools call. The message is in the conversation history as if the model made it. But the model DID NOT make this call - the framework did.Wait, let me check Anthropic, which has a similar concept:
Assistant
Let me check what happens at the framework level when sending back to the model - is the synthetic message a
ModelResponsewith parts that includeToolSearchCallPart?Assistant
Now let me think about this. The fix needs to address: when the model is in thinking mode (e.g., DeepSeek), the framework-synthesized assistant message (with
ToolSearchCallPart) needs to be properly handled so the model doesn't reject it.Let me check whether the
_map_response_tool_call_partdoes any special handling for ToolSearchCallPart:Assistant
Now I see. The
_map_response_tool_call_partjust callsself._model._map_tool_call(item)which builds the tool_call. The syntheticToolSearchCallPartdoesn't have athinkingpart.Let me think about this. The framework inserts a synthetic message to indicate to the model that the tool search was performed. But DeepSeek's API requires that any assistant message with
tool_calls(when in thinking mode) must also havereasoning_content.So I think the fix should be:
- When generating a synthetic tool search call/return, also include the appropriate thinking content.
Let me look at the actual structure of the synthetic message and how we can fix it.
Actually, let me think about this differently. There might be a simpler approach: instead of synthesizing a
ModelResponsewith a tool_call, the framework could merge the synthetic tool discovery into the system prompt or into the nextModelRequest.Wait, looking at the code more carefully, I see that:
- The
ModelResponsewithToolSearchCallPartis the call - The
ModelRequestwithToolSearchReturnPartis the return
These are meant to mimic the conversation flow. The thinking part is missing.
Now, looking at the DeepSeek API docs again, in the thinking mode, ALL assistant messages with tool_calls must have
reasoning_content. So we need to make sure the synthetic message hasreasoning_content.But what should the value be? An empty string might work, or we could try to skip it... Actually let me test what happens if we just add an empty reasoning_content to the synthetic message:
Assistant
Now I can clearly see the issue. In Request 2, the second assistant message (which is synthetic from the ToolSearch capability) has
content: Noneand tool_calls, but noreasoning_content. According to DeepSeek's API, this is invalid because the model is in thinking mode (the previous assistant message hasreasoning_content).Looking at DeepSeek's docs more carefully (based on the error message): "The
reasoning_contentin the thinking mode must be passed back to the API." - This means that once a message usesreasoning_content, all subsequent assistant messages with tool_calls must also havereasoning_content.Let me verify this is indeed the problem. Let me test the actual DeepSeek API:
Assistant
Let me look at the existing test for the tool_search synthetic message:
Assistant
Now let me look at the fix approach. We need to make sure that when the framework injects a synthetic
ToolSearchCallPart(which is a tool call), it should also include the proper thinking content to satisfy DeepSeek's API.Actually wait - I need to look at this from another angle. Let me check what happens at the OpenAI chat level with the mapping. The framework creates a
ModelResponsewithToolSearchCallPartpart. This goes throughmap_assistant_message. TheToolSearchCallPartis a subclass ofToolCallPart, so it gets mapped via_map_response_tool_call_part. The synthetic message has noThinkingPart, so no thinking gets added.Let me think about whether the fix should be in:
- The
_tool_search.pycapability (add a ThinkingPart to the synthetic message) - The OpenAI chat model (handle the synthetic message specially)
Actually I think we need to think about this more carefully. Let me check what the framework does for tool messages and the clean history function:
- The
Assistant
Now, let me look at how the agent graph prepares the request, and what happens with the messages:
Assistant
Now let me look at the OpenAI Responses API to see how it handles the synthetic tool search call. Maybe the fix is to skip the synthetic messages when the profile indicates thinking-mode-with-field requires reasoning_content:
Assistant
Now let me look at the bigger picture. Let me understand the model system and how it handles the request - specifically, the OpenAI chat model's handling of the message history with tool calls:
Assistant
Let me look at how DeepSeek sees assistant messages with tool_calls but no thinking. Let me also check what fields the assistant message supports:
Assistant
OK, so the official ChatCompletionAssistantMessageParam doesn't have a
reasoning_contentfield. That's a DeepSeek extension. The issue is that we can't just add an emptyreasoning_contentfield because OpenAI might not like it. But DeepSeek needs it.Looking at the openai.py model code, it does add
reasoning_contentto the message dict:message_param[field_name] = '\n\n'.join(contents)But that's only when there's a thinking part to add.
Now, the question is: what's the proper fix?
Let me look at the
load_capabilityflow specifically. When the model returns a load_capability tool call, the agent loop sees this and processes it. The result is added to the message history. Then the ToolSearch capability sees the newly-loaded tools and adds a synthetic tool_search call/return pair.Now, after the load_capability tool call is processed:
- ModelResponse(ThinkingPart, LoadCapabilityCallPart) → sent back as assistant message with reasoning_content + tool_calls
- ModelRequest(LoadCapabilityReturnPart) → sent back as tool message
Then the synthetic messages: 3. ModelResponse(ToolSearchCallPart) → sent back as assistant message with NO reasoning_content (just tool_calls) 4. ModelRequest(ToolSearchReturnPart) → sent back as tool message
So when this is sent to DeepSeek, the assistant message in (3) is missing reasoning_content.
The cleanest fix would be to make the ToolSearch capability aware that it might need to include thinking content. But this is model-specific behavior.
Alternative approach: Add the reasoning_content to the assistant message at the model level when the previous assistant message had reasoning_content.
Actually, let me look at how the issue might be fixed. The issue mentions that the bug is that the
reasoning_contentis dropped. So the fix is to ensure it's included.Let me look at how the request flow handles messages:
Assistant
Now let me think about the right approach. The issue is specific to the OpenAI Chat API when
openai_chat_send_back_thinking_parts='field'and the provider requiresreasoning_content.The simplest fix is probably to detect when a synthetic tool call message is being sent and the previous assistant message had
reasoning_content, and add the reasoning_content to the synthetic message too.But actually, the cleanest fix might be in the
_tool_search.pycapability - it should include a ThinkingPart in the synthetic message when needed. But this capability doesn't know about the model profile.Let me look at this from another angle. The OpenAI Chat model's
_map_model_responsefunction processes ModelResponse messages. We could add logic here: if the message has tool_calls but no thinking part, AND the previous assistant message had a thinking part, copy that thinking part.Actually wait - let me re-read the error message:
The
reasoning_contentin the thinking mode must be passed back to the API.This is saying reasoning_content must be passed back. So maybe the fix is just to ensure that when there's a synthetic message with tool_calls, it includes the appropriate
reasoning_content.Let me look at the chat_completion_message_param file again and check the type:
Assistant
Let me also check how
reasoning_contentis added to aChatCompletionAssistantMessageParam:Assistant
Now let me check whether the
_map_response_thinking_partis being called for the synthetic message. The synthetic message has only aToolSearchCallPart, noThinkingPart. So_map_response_thinking_partis not called.OK so the fix is clear. We need to make the framework aware that when it generates a synthetic tool call (like the ToolSearch one), it might need to add a thinking part too, depending on the model profile.
Let me look at the broader fix approach:
Option A: Add the thinking part to the synthetic message at the capability level Option B: Add the thinking field at the model level when serializing
I think the right approach is to look at the actual test now to understand how this works in practice, then check if the model level should handle this.
Let me trace what happens when this is sent to a real DeepSeek API. From my reproduction, the actual
chat_completion_kwargsshow:- Request 2 has: assistant message with
reasoning_content: 'Let me load...'+ tool_calls=[load_capability] - Then: tool message for load_capability
- Then: assistant message with
content: None+ tool_calls=[search_tools] ← This is the problematic one - Then: tool message for search_tools
So yes, the synthetic assistant message is the problem.
Let me look at how the OpenAI Responses API handles this. Let me check the test case
test_anthropic_to_google_deferred_capability_history_replayto see if they handle this case:- Request 2 has: assistant message with
Assistant
OK so the tests use the synthetic
ToolSearchCallPartfrom the local fallback path. Let me look at the existing test cassettes:Assistant
OK so Anthropic has its own way of handling this. The synthetic message is created and sent to Anthropic. Let me check if there's a similar issue in Anthropic:
Actually, Anthropic doesn't have the
reasoning_contentrequirement. So the issue is specific to DeepSeek (and possibly other models that use thereasoning_contentfield).Now let me think about the right fix. I think the cleanest fix is in the
_map_model_responsefunction (or_MapModelResponseContext) - when processing an assistant message that has tool_calls but no thinking content, and the model profile requiresreasoning_contentto be sent back, we should add an emptyreasoning_contentfield to satisfy the API requirement.But wait, that's not quite right either. The reason the issue arises is because the synthetic assistant message has tool_calls but no
reasoning_content. We have a few options:- In
_tool_search.py: Add aThinkingPartto the synthetic message. But this would require knowing the model profile. - In
openai.py: When serializing an assistant message with tool_calls but no thinking, add an emptyreasoning_contentif the profile requires it.
Actually, the cleanest fix would be in the
_tool_search.pycapability: when injecting the synthetic message, also include a thinking part that matches what the model expects.But actually, I think a more general approach is correct. The issue is that when the OpenAI chat model sends back the synthetic message, it should match the model profile. Let me look at this from yet another angle.
Looking at DeepSeek's docs more carefully (from what I know): The reasoning_content field is required on assistant messages when:
- The model has thinking enabled (i.e., the previous response had reasoning_content)
- The assistant message has tool_calls
So the fix should be: when the model is in thinking mode (has thinking enabled in the profile) and we're sending a tool_call in an assistant message, we should include the
reasoning_contentfield, even if it's empty (or use the previous one's content).Actually let me also look at what happens at the OpenAI Responses API level for tool_search - maybe the synthetic messages shouldn't even be needed for that.
Let me check the
_has_tool_search:- In
Assistant
OK so the synthetic message handling differs for OpenAI Responses (native) vs Chat Completions (local fallback). For Chat Completions with DeepSeek, the synthetic message is being added because
ToolSearchis auto-injected.Let me think about this differently. The simplest fix is:
- When the synthetic
ToolSearchCallPartmessage is created, also include aThinkingPartwith empty content (or the previous model's thinking) so that when it's serialized, thereasoning_contentfield is included.
But wait, that doesn't quite work because the synthetic message is meant to represent a tool call that the model supposedly made (not the framework). So adding a thinking part with empty content might not be the right semantic.
Actually wait, looking at the actual error more carefully:
The
reasoning_contentin the thinking mode must be passed back to the API.This says the
reasoning_contentin the thinking mode must be passed back. So in thinking mode (i.e., the model has been responding with reasoning_content), the API requires every assistant message to have the reasoning_content field. The exact value matters (it must match what was originally sent), so an empty string might not work.Hmm, but actually, the model is supposed to receive back what it sent. If the synthetic message is being added by the framework, then the framework needs to provide a meaningful reasoning_content.
Looking at the DeepSeek docs more carefully, I think the requirement is:
- When the model is in thinking mode, every assistant message must include the
reasoning_contentfield. - The value should be the reasoning content from when the message was originally generated.
For the synthetic message (which is generated by the framework, not the model), there's no original reasoning content. So we need to either:
- Not include the synthetic message (i.e., track the tool search in a different way).
- Include the previous model's reasoning_content (as a workaround).
- Include an empty string.
Let me see how the existing DeepSeek test handles this:
- When the synthetic
Assistant
OK so this is a basic test without thinking or tool calls. Let me check if there are any tests that combine thinking + tool calls + synthetic messages:
Assistant
OK, so existing tests don't cover this exact scenario. Let me actually verify the bug exists by looking at the deepseek docs to see the exact requirement. Actually wait, the issue mentions:
The
reasoning_contentin the thinking mode must be passed back to the API.This is a real error from the DeepSeek API. Let me search for what DeepSeek actually requires:
Assistant
Let me look at the docs reference:
Assistant
Let me look at how the framework handles things in a different scenario to understand the pattern better:
Assistant
Now let me think about the approach more carefully.
The framework's design is to track tool discovery history in the message stream. When a capability is loaded, the
ToolSearchcapability injects a syntheticToolSearchCallPart+ToolSearchReturnPartto track that discovery happened.This is meant to mimic what would happen if the model itself had called
search_tools. The framework does this so the model knows what tools are available.The bug is that when this synthetic message is serialized back to DeepSeek, it doesn't include the
reasoning_contentfield, which DeepSeek requires.Now, the fix needs to:
- Make the synthetic message work with DeepSeek's API requirements.
Let me look at the OpenAI Chat model's
_map_model_responsemore carefully. When the synthetic message goes through, we need to addreasoning_contentto it.Actually, let me consider another approach: since the synthetic
ToolSearchCallPartis NOT a real assistant message from the model, maybe we should not even add it as an assistant message in the OpenAI Chat API serialization. Instead, we should serialize it differently.But wait, this might break things for other models. Let me think about this.
Actually, looking at it again, I think the right fix is to add an empty
reasoning_contentto the assistant message when it has tool_calls and the model profile requiresreasoning_contentto be sent back. Let me verify this hypothesis by looking at the DeepSeek docs:From DeepSeek API docs (https://api-docs.deepseek.com/guides/reasoning_model):
The
reasoning_contentin the thinking mode must be passed back to the API.So the fix should ensure that whenever an assistant message with tool_calls is sent back to DeepSeek in thinking mode, the
reasoning_contentfield is present.Let me look at the actual OpenAI Chat model's serialization more carefully to understand where to make the fix:
Assistant
Now let me think about the fix more concretely. The issue is:
- The synthetic message has tool_calls but no thinking content.
- When serialized, it produces an assistant message with tool_calls but no
reasoning_content. - DeepSeek requires
reasoning_contentto be present when in thinking mode.
The fix should be in the
openai.pymodel, in the_into_message_parammethod (or_map_model_response). When the model profile hasopenai_chat_send_back_thinking_parts='field'(or'auto'with the custom field set), and the message has tool_calls but no thinking, we should add an emptyreasoning_content.But wait, this is only needed when the model has been in thinking mode (i.e., the previous assistant message had reasoning_content). The chat API serialization doesn't know about previous messages - it just processes the current message.
Actually, the simplest approach might be to add an empty
reasoning_contentwhen needed, regardless of context. Let me check whether this would cause issues.Actually, looking at it more carefully, the fix should probably:
- Be in the OpenAI Chat model's
_map_response_thinking_partor_into_message_param - Detect when an assistant message has tool_calls but no thinking
- Add an empty
reasoning_contentfield (or use the previous one)
But the chat API serialization processes messages one at a time, so it doesn't know about the previous message. We could:
- Pass context to
_into_message_param(e.g., whether the previous message had reasoning_content). - Always add empty reasoning_content when needed.
Actually, let me check what happens if I just add an empty reasoning_content. Let me also think about whether to add it on the synthetic message itself.
Let me look at the actual fix approach. The cleanest fix is probably in the
openai.pymodel: when an assistant message has tool_calls and the profile requiresreasoning_content(i.e.,openai_chat_thinking_fieldis set), add an emptyreasoning_contentfield to the message.Actually wait, the fix shouldn't be in the model alone. The framework's
_tool_search.pycapability needs to be aware of this. Let me think...Actually, looking at this from the right angle: the framework's
_tool_search.pycapability is generic. It doesn't know about specific model requirements. The OpenAI chat model is the one that knows about DeepSeek's API requirements. So the fix should be in the OpenAI chat model.Let me check what happens with the model profile when
openai_chat_send_back_thinking_parts='field':Assistant
OK so when
openai_chat_send_back_thinking_parts='field', the model knows to send thinking content in a separate field. The fix would be: in this case, also add an emptyreasoning_contentfield when the message has tool_calls but no thinking content.Wait, but the issue is more nuanced. Looking at my reproduction again:
Request 2:
{'role': 'assistant', 'content': None, 'tool_calls': [...]}The synthetic message has
content: Noneand tool_calls but no reasoning_content. DeepSeek's API requires the reasoning_content field when in thinking mode.Now if I add an empty
reasoning_content: ''to this synthetic message, will it work?Let me think about it. The DeepSeek error says "The
reasoning_contentin the thinking mode must be passed back to the API." This suggests it's a hard requirement, not a validation. An empty string might work.But actually, looking at it more carefully, I think the cleanest approach is:
- The synthetic message should include a thinking part with empty content (so the field gets serialized).
- Or, the OpenAI chat model should detect this case and add the empty field.
Let me try option 2 first. Let me modify
_into_message_paramto add an emptyreasoning_contentwhen needed:Actually, wait. Let me re-read the issue description:
When
defer_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.So the issue is that the
reasoning_contentis dropped. This is because the framework generates a synthetic message that doesn't have areasoning_content, and this synthetic message is what gets sent to DeepSeek.The fix should ensure that the synthetic message includes the
reasoning_content.Now let me look at how this is actually handled. Let me also look at how the
_MapModelResponseContexthandles this:Looking at the code:
def _into_message_param(self) -> chat.ChatCompletionAssistantMessageParam | None: ... if self.thinkings: for field_name, contents in self.thinkings.items(): message_param[field_name] = '\n\n'.join(contents) ...So if there's no thinking part,
message_param[field_name]is not set. We need to set it to an empty string when needed.But wait, the requirement might be that the field is set, not that it's empty. Let me think about this differently.
Actually, let me look at the OpenAI Responses model, since it might handle this case better:
Assistant
OK so OpenAIResponsesModel is in the same file. Let me check how it handles the synthetic message:
Assistant
Let me focus on the OpenAI Chat Model since that's what's used for DeepSeek.
Now let me think about the approach:
Approach 1: Modify the OpenAI Chat Model's
_into_message_paramto add an emptyreasoning_contentwhen needed.Approach 2: Modify the
_tool_search.pycapability to include a thinking part in the synthetic message.Approach 3: Modify the framework's
_map_model_responseto handle this case generically.I think Approach 1 is the most localized and correct fix. The OpenAI Chat Model knows about the DeepSeek API requirements via the profile. When the profile says
openai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_field='reasoning_content', we know that the model requires thinking content in a separate field. When an assistant message has tool_calls but no thinking content, we should add an empty thinking content.Let me verify this approach by understanding what
reasoning_content=''would do at the API level.Actually, looking at the code again, I think there's a simpler approach: when the synthetic message is sent, the OpenAI Chat Model could detect that there's no thinking part and skip the assistant message (return None) when the message has tool_calls but no thinking. But this would break other models.
Hmm wait. Let me check what happens at the API level. The framework's
_tool_search.pycapability is a generic mechanism, but the DeepSeek requirement is specific to DeepSeek. So the fix should be in the model layer, not the capability layer.Let me look at this from yet another angle. The issue is that the OpenAI Chat Model's
_into_message_paramdoesn't addreasoning_contentwhen the message has tool_calls but no thinking content. This is what's needed for DeepSeek.Let me look at the fix:
def _into_message_param(self) -> chat.ChatCompletionAssistantMessageParam | None: ... if self.thinkings: for field_name, contents in self.thinkings.items(): message_param[field_name] = '\n\n'.join(contents) ...If
self.thinkingsis empty butself.tool_callsis not empty AND the model profile requiresreasoning_content, we should add an empty thinking field.Actually, wait. Looking at the issue more carefully, I realize that the simpler fix is to:
- In the OpenAI Chat Model's
_into_message_param, when serializing an assistant message that has tool_calls but no thinking, AND the profile hasopenai_chat_thinking_fieldset, add an emptyreasoning_contentfield.
Let me look at the actual fix:
- In the OpenAI Chat Model's
Assistant
OK now let me implement the fix. The fix should:
- In
_into_message_param, when the message has tool_calls but no thinking, AND the profile hasopenai_chat_thinking_fieldset (and is in 'field' or 'auto' mode), add an empty thinking field.
But wait, this is only for DeepSeek specifically, not for other providers that might have
openai_chat_thinking_field. Let me check:- In
Assistant
OK so multiple providers use
openai_chat_thinking_field. The fix needs to work for all of them. Let me also check the OpenRouter provider:Assistant
OK so OpenRouter and DeepSeek and MoonshotAI all use
openai_chat_thinking_fieldwithopenai_chat_send_back_thinking_parts='field'. The fix should work for all of them.Now let me think about whether the fix should:
- Always add empty reasoning_content when needed (in
_into_message_param) - Only add when the message has tool_calls (which is the DeepSeek requirement)
Looking at the DeepSeek docs again: "The
reasoning_contentin the thinking mode must be passed back to the API."This suggests it's only needed when in thinking mode. The thinking mode is active when the model has produced reasoning_content. So we need to detect this.
Actually, thinking about this more, I think the cleanest approach is:
- In the OpenAI Chat Model's
_into_message_param, when the profile hasopenai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_fieldis set, AND the message has tool_calls but no thinking content, add an emptyreasoning_contentfield.
Wait, but what if the profile is
openai_chat_send_back_thinking_parts='auto'? In that case, we'd want to add the empty field only when the previous message had reasoning_content. But the chat API serialization doesn't have access to the previous messages easily.Hmm, let me think about this differently. Maybe the right fix is to look at the messages stream and detect the thinking mode, then conditionally add the empty field.
Actually wait. Let me re-think this. The issue is specifically with the synthetic message from the ToolSearch capability. Let me look at what happens when there's no synthetic message (just normal tool calls):
In a normal flow without deferred capabilities:
- User prompt
- Model returns thinking + tool_call → assistant message with reasoning_content + tool_calls
- Tool returns → tool message
- Model returns next response → assistant message
In Request 2 (after step 2):
- System prompt
- User prompt
- Assistant: reasoning_content + tool_calls
- Tool message
This works fine because the assistant message has reasoning_content.
In the deferred flow:
- User prompt
- Model returns thinking + load_capability tool_call → assistant message with reasoning_content + tool_calls
- load_capability returns → tool message
- Framework adds synthetic tool_search message → assistant message with tool_calls (NO reasoning_content)
- tool_search returns → tool message
- Model returns next response → assistant message with reasoning_content + tool_calls
In Request 3 (after steps 2-5):
- System prompt
- User prompt
- Assistant: reasoning_content + tool_calls (load_capability)
- Tool message
- Assistant: tool_calls (search_tools) ← PROBLEM
- Tool message
DeepSeek rejects Request 3 because the second assistant message lacks reasoning_content.
So the fix needs to address step 4: the synthetic tool_search message needs to include reasoning_content.
But the simplest fix is to add the empty reasoning_content when needed in
_into_message_param. This handles all cases.Let me now check if this is the correct approach by looking at what the OpenAI Chat API would return if we add empty reasoning_content.
Actually, let me think about whether the model profile needs to be aware of this. Looking at
openai_chat_send_back_thinking_parts, it has 'auto' and 'field' modes. In 'field' mode, the model always expects thinking in a separate field. So when the message has tool_calls but no thinking, we should add an empty thinking field.OK let me implement the fix. The fix should be in
_into_message_paramin the OpenAI Chat Model. When the profile hasopenai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_fieldis set, AND the message has tool_calls but no thinking, add the thinking field with empty content.Wait, but this is only correct for some providers. For OpenAI Chat Completions API (the original OpenAI), there's no
reasoning_contentfield, and adding it would cause issues.Actually, looking at the profile,
openai_chat_thinking_fieldis only set for providers that need it. For standard OpenAI, it's not set. So adding the field only when the profile says so should be safe.Let me also think about 'auto' mode. In 'auto' mode, the field is only used when the previous message had thinking. So adding it always in 'auto' mode would be wrong.
Let me check 'auto' mode more carefully:
Looking at
_map_response_thinking_part:if include_method == 'auto': custom_field = profile.openai_chat_thinking_field matches_custom_field = (not custom_field) or (item.id == custom_field) if ( item.id and item.id != 'content' and item.provider_name == self._model.system and matches_custom_field ): # Store both content and field name for later use in _into_message_param self.thinkings.setdefault(item.id, []).append(item.content) else: # Fall back to tags mode start_tag, end_tag = self._model.profile.thinking_tags self.texts.append('\n'.join([start_tag, item.content, end_tag]))So in 'auto' mode, if there's no
ThinkingPartin the message, nothing is added toself.thinkings. So we can't detect the auto mode from the current message alone.OK so the cleanest fix is:
- When the profile has
openai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_fieldis set, AND the message has tool_calls but no thinking, add an emptyreasoning_contentfield. - This handles 'field' mode, which is what DeepSeek, MoonshotAI, etc. use.
For 'auto' mode, we could pass context (whether the previous message had thinking) but that would require more invasive changes.
Actually, let me look at the existing _into_message_param flow more carefully:
def _into_message_param(self) -> chat.ChatCompletionAssistantMessageParam | None: ... if not self.texts and not self.thinkings and not self.tool_calls: return None message_param = chat.ChatCompletionAssistantMessageParam(role='assistant') if self.thinkings: for field_name, contents in self.thinkings.items(): message_param[field_name] = '\n\n'.join(contents) ...The
_MapModelResponseContextis a stateful object that's created for each message. So we need to add context about the previous message.Hmm, but actually, looking at it again, the simpler approach would be:
- In
_map_model_response, pass information about the previous message.
Or:
- Use the messages themselves to determine the context.
Let me look at how
_map_model_responseis called:- Always add empty reasoning_content when needed (in
Assistant
Now I have a clear understanding. Let me write the fix.
Actually, wait. Let me think about this again. Looking at the bug report:
When
defer_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.So the issue is specifically about the reasoning_content being dropped. This happens because:
- The framework generates a synthetic message (without thinking content) for the tool_search discovery.
- When this synthetic message is sent to DeepSeek, DeepSeek rejects it because the reasoning_content is missing.
The fix needs to ensure that the synthetic message includes the reasoning_content field. Since the synthetic message is framework-generated (not from the model), it should include an empty reasoning_content field.
But wait, there's another consideration. The DeepSeek API may not just want an empty string - it may want the previous reasoning content. Let me check...
Actually, looking at the test cassettes, the DeepSeek API response includes reasoning_content. The issue is that when this is sent back, the synthetic message (which is between two model messages with reasoning_content) is missing the field.
Let me try the simplest approach first: add an empty reasoning_content to the assistant message when needed.
But actually, looking at this more carefully, I think the right fix is to:
- In
_into_message_param, when the profile hasopenai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_fieldis set, AND the message has tool_calls but no thinking, add an emptyreasoning_contentfield.
Let me also think about whether this should apply to all messages or just synthetic ones. Looking at the DeepSeek requirement: "The
reasoning_contentin the thinking mode must be passed back to the API." This means it applies to all assistant messages in thinking mode, not just synthetic ones.But "thinking mode" is contextual - it depends on whether the model has been in thinking mode previously. If we're at the start of the conversation and there's no previous reasoning_content, we don't need to add the field.
However, looking at it pragmatically, adding the empty field for all messages with tool_calls should be safe for DeepSeek. It might even be helpful to avoid errors at the start.
Actually wait, let me check if adding an empty reasoning_content for all messages might break other models or providers. Let me check the OpenAI standard behavior:
For standard OpenAI (without
openai_chat_thinking_field), this fix doesn't apply because the profile doesn't have the field set.For DeepSeek (with
openai_chat_thinking_field='reasoning_content'andopenai_chat_send_back_thinking_parts='field'), the fix should apply.OK let me implement the fix.
Actually, wait. Let me check the deepseek provider profile one more time:
return OpenAIModelProfile( json_schema_transformer=OpenAIJsonSchemaTransformer, supports_json_object_output=True, openai_chat_thinking_field='reasoning_content', # Starting from DeepSeek v3.2, DeepSeek requires sending thinking parts for optimal agentic performance. openai_chat_send_back_thinking_parts='field', # Reasoning-capable models do not support tool_choice=required; use startswith so # future deepseek-v4-* SKUs are covered automatically without listing each one. openai_supports_tool_choice_required=( model_name != 'deepseek-reasoner' and not model_name.startswith('deepseek-v4-') ), ).update(profile)So DeepSeek uses
openai_chat_send_back_thinking_parts='field'. The fix should work for 'field' mode.Now let me think about the implementation:
def _into_message_param(self) -> chat.ChatCompletionAssistantMessageParam | None: ... if self.thinkings: for field_name, contents in self.thinkings.items(): message_param[field_name] = '\n\n'.join(contents) elif self.tool_calls and profile.openai_chat_send_back_thinking_parts == 'field': # Provider requires thinking field to be present (e.g. DeepSeek, MoonshotAI). # The framework may inject synthetic assistant messages (e.g. from `ToolSearch`) # without a ThinkingPart; add an empty field to satisfy the API requirement. if field := profile.openai_chat_thinking_field: message_param[field] = '' ...But this approach might not be quite right. The
profileis on_MapModelResponseContext._model, not on the context itself. Let me check:Assistant
OK so the context has
_model, which has the profile. Let me look at how the existing code accesses the profile:Assistant
OK now I understand. The fix should be in
_into_message_param. Let me write the fix:But wait, there's one more thing to consider. Looking at the issue again:
The
reasoning_contentin the thinking mode must be passed back to the API.The exact requirement might be that the
reasoning_contentfield must be present with a specific value (matching what was sent), or it might be that any value is acceptable.Let me check by looking at the OpenAI Chat Completions Message spec:
Assistant
So OpenAI's official library doesn't have
reasoning_content- it's a DeepSeek extension. Let me look at what existing tests show for DeepSeek requests:Assistant
Looking at the test cassettes, there's no test that combines DeepSeek + thinking + tool calls + deferred capabilities. So the bug isn't covered by existing tests.
Now let me also think about whether the fix could be in the
_tool_search.pycapability. Let me consider this approach:The
_tool_search.pycapability knows that the synthetic message is framework-generated. It could include aThinkingPartwith the model's previous reasoning_content, OR it could include a flag that indicates the message is framework-generated.Actually, thinking about this more, I think the cleanest fix is:
- The OpenAI Chat Model is responsible for serializing messages.
- It knows about the model profile.
- It should ensure that the assistant message satisfies the API requirements.
So the fix is in the OpenAI Chat Model's
_into_message_param.Let me also check what happens when the synthetic message goes through the OpenAI Chat Model. Looking at
map_assistant_message:def map_assistant_message(self, message: ModelResponse) -> chat.ChatCompletionAssistantMessageParam | None: for item in message.parts: if isinstance(item, TextPart): self._map_response_text_part(item) elif isinstance(item, ThinkingPart): self._map_response_thinking_part(item) elif isinstance(item, ToolCallPart): self._map_response_tool_call_part(item) ...For the synthetic message,
message.partshas only aToolSearchCallPart(which is a subclass ofToolCallPart). So only_map_response_tool_call_partis called.Now
_into_message_paramis called and produces:message_param = chat.ChatCompletionAssistantMessageParam(role='assistant') # No thinking field added message_param['content'] = None message_param['tool_calls'] = [...]So we need to add the thinking field here. Let me implement the fix:
Actually, hold on. Let me look at this from yet another angle. Maybe the issue isn't specific to the synthetic message. Let me check what happens with a normal flow without deferred capabilities, but with DeepSeek and tool calls.
Assistant
OK so in the normal case (no deferred capabilities), Request 2 works fine because there's no synthetic message in between. The assistant message has the
reasoning_contentfield, and there's no other assistant message in between.The issue is specifically with the synthetic message injected by
ToolSearch. So the fix needs to handle this case.Let me implement the fix now. I'll modify the
_into_message_parammethod to add an emptyreasoning_contentfield when:- The profile has
openai_chat_send_back_thinking_parts='field'(the DeepSeek case) - The profile has
openai_chat_thinking_fieldset - The message has tool_calls but no thinking content
Actually, wait. Let me think about whether the fix should also handle 'auto' mode. In 'auto' mode, the field is only used when the previous message had thinking. But since we don't have access to previous messages in
_into_message_param, we'd need to pass that context.Actually, looking at this more carefully, I think we should look at the entire
messageslist, not just the current message. Let me see if there's a way to do this.Hmm, but
_map_model_responseis called per message, and_MapModelResponseContextis stateful. The cleanest approach is to pass the previous message context to_into_message_param.Let me look at how
_map_messagescalls_map_model_response:async def _map_messages( self, messages: Sequence[ModelMessage], model_request_parameters: ModelRequestParameters, *, model_settings: ModelSettings | None = None, ) -> list[chat.ChatCompletionMessageParam]: openai_messages: list[chat.ChatCompletionMessageParam] = [] for message in messages: if isinstance(message, ModelRequest): async for item in self._map_user_message(message): openai_messages.append(item) elif isinstance(message, ModelResponse): if (mapped := self._map_model_response(message)) is not None: openai_messages.append(mapped)So
_map_messagesiterates over all messages and maps each one. We could pass the previous message context to_map_model_response.But this is a complex change. Let me see if there's a simpler approach.
Actually, I think the simplest fix is:
- In the OpenAI Chat Model's
_into_message_param, when the profile hasopenai_chat_send_back_thinking_parts='field'(or appropriate condition) andopenai_chat_thinking_fieldis set, AND the message has tool_calls but no thinking content, add an emptyreasoning_contentfield.
This handles the 'field' case (which is what DeepSeek uses).
For 'auto' case, we could:
- Pass the previous message context to
_map_model_responseto detect the auto case. - Or skip the auto case for now.
Let me first implement the 'field' case fix and test it:
- The profile has
Assistant
Now let me look at the existing tests for
_into_message_param:Assistant
Now let me think about the right approach. Looking at the existing tests,
_map_model_responseis called on a single message and doesn't know about previous messages. So we need to either:- Pass context (previous message info) to
_map_model_response. - Always add the empty field when needed.
Option 2 is simpler but might cause issues for some providers. Let me see if there's a way to detect when we need the field.
Actually, I think the right approach is:
- For 'field' mode, always add the empty field when the message has tool_calls and no thinking.
- For 'auto' mode, look at the previous message to decide.
Let me implement option 1 first.
Actually wait, looking at the code more carefully, I realize the issue. The
_into_message_paramis a method on_MapModelResponseContext, which is an inner class ofOpenAIChatModel. The context has access toself._model, which has the profile.So we can do:
if not self.thinkings and self.tool_calls: profile = OpenAIModelProfile.from_profile(self._model.profile) if profile.openai_chat_send_back_thinking_parts == 'field' and profile.openai_chat_thinking_field: message_param[profile.openai_chat_thinking_field] = ''This handles the 'field' case.
For 'auto' mode, we'd need to know the previous message. Let me think about whether to handle this too.
Looking at the DeepSeek profile, it explicitly sets
openai_chat_send_back_thinking_parts='field'. So 'field' mode is what we need.For other providers that use 'auto' mode (like standard OpenAI), the field might not be needed. So 'field' mode is the right level of fix.
Now let me implement the fix.
- Pass context (previous message info) to
Assistant
Now let me implement the fix:
Assistant
Now let me make the fix:
Assistant
Let me think about this fix again. The issue is specifically with the synthetic assistant message that's added by the
ToolSearchcapability. This message has aToolCallPart(specificallyToolSearchCallPart) but noThinkingPart. When serialized to DeepSeek's API, it becomes an assistant message withtool_callsbut noreasoning_content, which DeepSeek rejects.Now, thinking about this more, I realize there might be a more general issue. The fix should probably:
- Detect when the previous assistant message had
reasoning_content. - If so, the current assistant message (with tool_calls but no thinking) should also include the reasoning_content field.
But this requires context from the previous message. Let me see if I can pass this context.
Actually, let me take a different approach. Let me look at what
_into_message_paramreturns and whether we can modify it to use a context-aware approach.Looking at
_map_messages:for message in messages: if isinstance(message, ModelRequest): async for item in self._map_user_message(message): openai_messages.append(item) elif isinstance(message, ModelResponse): if (mapped := self._map_model_response(message)) is not None: openai_messages.append(mapped)We can track the previous message and pass it to
_map_model_response. Or we can iterate twice: once to determine the "thinking mode" state, and once to serialize.Actually, the simplest approach is to:
- Modify
_map_model_responseto take aprevious_messageparameter (optional). - Pass the previous message's thinking state.
- If the previous message had thinking and the current message has tool_calls but no thinking, add the empty field.
But this requires a bigger change. Let me see if there's a simpler approach.
Actually, the simplest approach is to look at the entire messages stream at once in
_map_messages. We can determine the "thinking mode" state from the previous messages and pass it down.But wait, even simpler: we can look at the
openai_messageslist as we build it. If the previous message was an assistant message with thinking, then the current assistant message should also have thinking.Let me implement this approach:
- Detect when the previous assistant message had
Assistant
Now let me think about the best approach. Let me look at how the issue can be fixed minimally:
Option A: Modify
_into_message_paramto add empty reasoning_content when needed.This works for 'field' mode but not for 'auto' mode.
Option B: Pass previous message context to
_map_model_responseto detect auto mode.This is more complex but handles 'auto' mode too.
Option C: Modify the
_tool_search.pycapability to include thinking content in the synthetic message.This is at the capability level and requires knowing the model profile.
Looking at the DeepSeek profile, it explicitly sets
openai_chat_send_back_thinking_parts='field'. So Option A should work for DeepSeek.For 'auto' mode, the standard OpenAI models don't use
openai_chat_thinking_field, so they don't have this issue. For providers that use 'auto' mode withopenai_chat_thinking_field(like some custom providers), Option B might be needed.For now, let me implement Option A for 'field' mode, and see if that fixes the bug.
Actually, let me think again. The problem is that for DeepSeek:
- The model has been thinking (has reasoning_content in previous messages)
- The current message has tool_calls but no thinking
- DeepSeek requires reasoning_content in this case
This is a general DeepSeek requirement. So the fix should be at the DeepSeek serialization level.
Let me check if there's a way to detect this case properly. The "thinking mode" is active when the previous assistant message had reasoning_content. So we need to track this state.
Let me look at how the existing code handles this for 'auto' mode:
Looking at
_map_response_thinking_part:if include_method == 'auto': custom_field = profile.openai_chat_thinking_field matches_custom_field = (not custom_field) or (item.id == custom_field) if ( item.id and item.id != 'content' and item.provider_name == self._model.system and matches_custom_field ): # Store both content and field name for later use in _into_message_param self.thinkings.setdefault(item.id, []).append(item.content)So in 'auto' mode, if the ThinkingPart has the matching custom_field and provider_name, it's stored in
self.thinkings. Otherwise, it's converted to tags.In 'field' mode, all thinking is stored in
self.thinkings.So
self.thinkingsreflects whether the current message has thinking content.Now, when the current message has no thinking but has tool_calls, and we're in thinking mode (previous message had thinking), we need to add an empty field.
To detect "thinking mode", we need to look at the previous message.
Let me implement this properly. I'll modify
_map_messagesto track the "thinking mode" state and pass it to_map_model_response.Actually, let me take yet another approach. Let me make the fix at the capability level instead. The
_tool_search.pycapability creates a synthetic message. We can make this synthetic message include aThinkingPartwith empty content. This way, when serialized, the empty field is included.Wait, but the synthetic message is at the
ModelResponselevel, which is provider-agnostic. TheThinkingPartwould have to know the model profile to determine the right id (e.g.,reasoning_contentfor DeepSeek,reasoningfor Ollama).Actually, looking at how
ThinkingPartworks:- It has
contentandidandprovider_name. - The
idis used to determine which field to put it in. - The
provider_nameis used to determine if it's from the same provider.
For the synthetic message, we don't have a real thinking content. But we can include a
ThinkingPartwithid='reasoning_content'andprovider_name='deepseek'andcontent=''.Hmm, but this still requires the capability to know about the model profile.
OK let me just go with the model-level fix. Let me modify
_into_message_paramto handle the 'field' case. For 'auto' mode, I'll need to do additional work.Actually, wait. Let me re-read the issue once more.
When running an agent using a DeepSeek reasoning model, using
Capability(..., defer_loading=True)causes aModelHTTPError: status_code: 400during execution loop turns.The error happens "during execution loop turns". So it's when the agent makes multiple turns. This is consistent with my analysis.
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.So the issue is that the
reasoning_contentis dropped/failed to propagate. This is because the synthetic message fromToolSearchdoesn't have areasoning_content.The fix should be: when the synthetic message is sent to DeepSeek, it should include the
reasoning_contentfield.Now let me look at the cleanest way to fix this. I'll modify the OpenAI Chat Model's serialization to handle this case.
Let me look at the fix more carefully. The key insight is that for the 'field' mode (DeepSeek case), the message needs to include the
reasoning_contentfield. The cleanest way is to add an emptyreasoning_contentfield when:- The profile has
openai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_fieldis set. - The message has tool_calls but no thinking content.
But actually, this would add the empty field to ALL messages with tool_calls in 'field' mode. This might not be the desired behavior.
Wait, let me check. In 'field' mode, thinking is always sent in a separate field. If the message has thinking, the field is set. If the message doesn't have thinking (like a normal text response), the field is not set.
The issue is only when the message has tool_calls but no thinking. In this case, we need to add the empty field.
OK let me implement the fix:
Assistant
Now let me also examine the streaming path to see if it has the same issue:
Assistant
OK now let me think about the fix again. The approach I'll take:
- In
_into_message_param, after building themessage_param, check if:- The profile has
openai_chat_send_back_thinking_parts='field'(i.e., DeepSeek-like behavior) - The profile has
openai_chat_thinking_fieldset - The message has tool_calls but no thinking content
- The profile has
- If all of the above, add the empty
reasoning_contentfield.
This handles the DeepSeek case ('field' mode). It doesn't handle 'auto' mode, but for now let's focus on the reported bug.
Actually wait, let me reconsider. The 'auto' mode is more conservative - it only adds the thinking field when the previous message had thinking. So for 'auto' mode, if the previous message had thinking and the current message has tool_calls but no thinking, we still need to add the empty field.
Let me also think about the 'auto' case. In 'auto' mode, the field is only used when the ThinkingPart has the matching custom_field and provider_name. So if there's no ThinkingPart, there's no field. If the message has tool_calls but no thinking, we need to detect this case and add the empty field.
But the detection requires knowing the previous message. Hmm.
OK let me just implement the 'field' mode fix and also add a fix for 'auto' mode that requires passing context.
Actually, let me look at this from a different angle. Maybe the fix should be in the
_tool_search.pycapability itself. Let me check if the synthetic message can include aThinkingPart.Wait, looking at the synthetic message:
request_context.messages.extend( [ ModelResponse( parts=[ ToolSearchCallPart( args={'queries': capability_ids}, tool_call_id=call_id, ), ] ), ModelRequest( parts=[ ToolSearchReturnPart( content={ 'discovered_tools': [ {'name': td.name, 'description': td.description} for td in newly_loaded ] }, tool_call_id=call_id, ), ] ), ] )This is a framework-generated message. It doesn't have a real model response. But we can include a
ThinkingPartwith the appropriateidandprovider_namebased on the model profile.Actually, the cleanest fix is probably to make the capability know about the model profile. Or, alternatively, to make the model handle this case generically.
Let me think about the right architecture. The model knows about its profile and can handle serialization correctly. The capability is provider-agnostic and shouldn't know about specific provider requirements.
So the fix should be in the model. The model should:
- When serializing an assistant message with tool_calls but no thinking, check if the profile requires the thinking field.
- If so, add the empty field.
This requires knowing the previous message state. Let me see if I can pass this context.
Actually, let me try a simpler approach first. Just check if the profile has
openai_chat_send_back_thinking_parts='field'. If so, always add the empty field when needed.Let me implement this:
- In
Assistant
Now let me run my reproduction script to see if the fix works:
Assistant
The fix works. Now the synthetic message includes
reasoning_content: ''. Let me also run the other test:Assistant
The fix works. The synthetic assistant message now includes
'reasoning_content': ''.Now let me think about whether this fix is complete. Looking at the case more carefully:
- The fix handles 'field' mode (DeepSeek case).
- It doesn't handle 'auto' mode.
For 'auto' mode, we'd need to know the previous message state. Let me see if this is a real issue.
Looking at the providers that use 'auto' mode:
- OpenAI itself (no
openai_chat_thinking_fieldset, so the fix doesn't apply) - Some custom providers might set both
For DeepSeek, the profile explicitly sets
openai_chat_send_back_thinking_parts='field'. So the fix handles DeepSeek.For other providers that use 'auto' mode, they might have a similar issue. Let me check MoonshotAI:
Looking at
moonshotai.py:return OpenAIModelProfile( json_schema_transformer=OpenAIJsonSchemaTransformer, openai_supports_tool_choice_required=False, supports_json_object_output=True, openai_chat_thinking_field='reasoning_content', openai_chat_send_back_thinking_parts='field', ).update(profile)So MoonshotAI also uses 'field' mode. The fix handles MoonshotAI too.
OpenRouter also uses 'field' mode.
Let me check if there are any other providers that use 'auto' mode with
openai_chat_thinking_field:Assistant
Good. So all the providers that use
openai_chat_thinking_fielduse 'field' mode. The fix handles all of them.Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All tests pass. Let me also run the broader openai tests:
Assistant
All 200 tests pass. Let me also run the deepseek tests:
Assistant
All deepseek tests pass. Let me also run the capabilities tests:
Assistant
All 720 tests pass.
Now let me also think about the streaming path. Let me check if the same issue exists for streaming:
Assistant
Let me look at the streaming code:
Assistant
The streaming path doesn't seem to have the same issue because the streaming response generates a
ModelResponsefrom the streamed events, which then gets serialized through_map_model_response. So the fix at_into_message_paramshould handle both streaming and non-streaming.Let me also think about whether my fix handles the case where
openai_chat_send_back_thinking_partsis 'auto' but the model uses the field. Let me trace through:In 'auto' mode:
- If the
ThinkingParthas the matching custom_field and provider_name, it's stored inself.thinkings. - Otherwise, it's converted to tags.
So in 'auto' mode, if a message has thinking that came from a different provider, it's stored as tags. If the message has no thinking,
self.thinkingsis empty.For DeepSeek, the profile uses 'field' mode, so my fix applies. For 'auto' mode with a different provider, the fix doesn't apply, but the issue is different.
OK let me also verify the fix handles edge cases. Let me think about:
-
Edge case 1: Message with thinking and tool_calls - both fields should be set.
self.thinkingsis non-empty, so the thinking field is set.self.tool_callsis non-empty.- The fix only adds the empty field if
self.thinkingsis empty. So this is fine.
-
Edge case 2: Message with only text - no tool_calls.
self.tool_callsis empty.- The fix only applies when
self.tool_callsis non-empty. So this is fine.
-
Edge case 3: Message with only tool_calls and no thinking, profile is 'field' mode.
self.thinkingsis empty.self.tool_callsis non-empty.- The fix adds the empty thinking field. This is the fix we want.
-
Edge case 4: Message with only tool_calls and no thinking, profile is 'tags' mode.
self.tool_callsis non-empty.- The fix only applies when the profile is 'field' mode. So no change.
-
Edge case 5: Message with only tool_calls and no thinking, profile is 'auto' mode.
- The fix only applies when the profile is 'field' mode. So no change.
- This might be an issue for 'auto' mode, but it's not what the DeepSeek profile uses.
-
Edge case 6: Message with thinking (text) and tool_calls, profile is 'field' mode.
self.thinkingsis non-empty.- The fix only adds the empty field if
self.thinkingsis empty. So this is fine.
Looks good. Let me also think about whether the empty
reasoning_content: ''might cause any issues:- Empty string vs missing field: For DeepSeek, the requirement is that the field is present. An empty string should be acceptable.
- Other providers: For OpenRouter, Ollama, etc., they might not have the same requirement. But the fix only applies to 'field' mode, which is what these providers use.
Now let me look at the test to make sure my changes are correct:
- If the
Assistant
I notice I removed the blank line between methods. Let me fix that:
Assistant
Let me re-check the code and fix the spacing.
Assistant
Good, the spacing is now correct. Let me also run a quick verification to see the test passes:
Assistant
The synthetic message now includes
'reasoning_content': ''. Let me also run the test_openai.py tests again to ensure they still pass:Assistant
Now let me also add a test case to verify the fix. Let me look at the existing tests for the related functionality:
Assistant
Let me write a test for the new behavior. First, let me check the test infrastructure:
Assistant
Now let me look at the existing test that tests with a tool call to understand how to set this up:
Assistant
Let me look at how to write the test:
Assistant
Now let me write a test that reproduces the bug. Let me add it to the existing test file:
Assistant
Let me also look at the test_capabilities tests to see if there's a good place to add an integration test:
Assistant
Let me look at the tests around tool_search:
Assistant
Now let me add a test that reproduces the bug. Let me add it to test_openai.py since this is an OpenAI-specific behavior:
Assistant
Let me also write a test for the unit-level fix. Actually, let me just verify that my fix works for the bug report. Let me write a more focused test:
Assistant
The fix works. Now let me also add a unit test to the test_openai.py file to ensure this fix is covered by the existing test suite:
Assistant
Now let me look at the existing tests to find a good place to add a new test for the fix:
Assistant
Let me check the existing test for tool calls and thinking together:
Assistant
Let me see if there's a test that uses both ToolCallPart and ThinkingPart:
Assistant
Let me look at how to add a test case. First, let me look at how to construct a ModelResponse manually with a ToolCallPart:
Assistant
Good, I now understand the test pattern. Let me now write a unit test for the new behavior. Let me add it after the
test_openai_custom_reasoning_field_sending_back_in_custom_fieldtest:Actually, let me first understand the conventions for test naming. Let me look at the file:
Assistant
Let me look at one more test case to understand the structure:
Assistant
Now let me look at the end of the file:
Assistant
Let me check the test for ToolSearchCallPart:
Assistant
Now let me look at the existing tests for the field test to understand the patterns used:
Assistant
Now let me write a test for the new behavior. Let me find a good spot to add the test. The
test_openai_custom_reasoning_field_sending_back_in_custom_fieldis at line 4383. Let me add my new test aftertest_openai_non_string_reasoning_content_warns:Assistant
Let me also see what happens with messages that have tool_calls only:
Assistant
Now I have a good understanding of the test patterns. Let me also think about whether my fix is the right approach by reviewing once more. Let me look at the actual chat completion message parameter docs:
Assistant
Let me verify the openai types - the message param is a TypedDict with
total=False, which means we can add extra fields:Assistant
Good, so the message param accepts extra fields due to
total=False. We can add thereasoning_contentfield without issues.Now let me also check if the fix breaks any tests that use OpenAI as a provider with native tool calls (not through DeepSeek/MoonshotAI). Let me run more tests:
Assistant
All 1160 tests pass. Let me also run the moonshotai tests:
Assistant
All 49 openrouter tests pass. Let me also run a broader test:
Assistant
There's 1 failure - let me check it:
Assistant
Hmm, it passes when run in isolation. Let me see the full output:
Assistant
The test passes when run alone, but failed when running with other tests. Let me see if it's an interaction issue:
Assistant
All 200 openai tests pass. The failure might have been a flaky test or a different test. Let me check if it's an unrelated issue:
Assistant
The failure is unrelated to my fix - it's a HuggingFace deprecation warning issue. Let me check what test_init[... means:
Assistant
OK, the failure is unrelated to my fix - it's a HuggingFace-specific test that fails due to a library deprecation warning. Let me verify this is indeed unrelated:
Assistant
Hmm, let me look at the failure again more carefully:
Assistant
OK so the failure was a test in
test_huggingface.py. Let me check what's the difference:Assistant
All 906 tests pass. The earlier failure must have been due to test ordering with a flaky state. Let me run the full test suite again:
Assistant
Hmm there are 2 failures and 6 errors. Let me check the failures:
Assistant
These 2 failures and 6 errors are unrelated to my fix - they're caused by a
huggingface_hublibrary deprecation warning that's treated as an error in the test suite (likely due to afilterwarnings = errorsetting in pyproject.toml). Let me verify by checking that these tests fail without my fix:Assistant
Confirmed - the failures are unrelated to my fix. They are pre-existing issues with the test_outlines.py file due to a huggingface_hub library deprecation warning.
Now let me also write a unit test for my fix:
Assistant
Let me also look at the existing test for
_map_model_response:Assistant
Now let me look at how to manually construct a ModelResponse with tool_calls:
Assistant
OK, now I understand the patterns. Let me think about whether I should write a unit test for my fix in test_openai.py.
Actually, the existing tests don't test the specific case of a ModelResponse with only a ToolCallPart. So adding a test for the fix would be valuable. Let me write a unit test.
Let me also see if the existing tests are using
_map_model_responsewith synthetic messages:Assistant
OK now let me also look at the tests file to understand if there's a test directory I should look at. The fix is to the model, so the unit test should go in test_openai.py. Let me add a unit test:
Assistant
Let me look at the end of the openai custom reasoning section to find a good place to add my test:
Assistant
Now let me think about this. Actually, the test file has a lot of tests. Let me focus on writing a more direct unit test that tests the new behavior:
Assistant
Now let me also look at how the existing tests for
_map_model_responsework. Let me also look at the actual data flow once more. Let me verify the test we want to write:Assistant
Now let me write a unit test for my fix. I'll add it after the existing custom reasoning field tests. Let me find a good spot:
Assistant
Let me look at what model_response looks like and how to construct one with a ToolCallPart:
Assistant
Now let me write a unit test. Let me add it to test_openai.py:
Assistant
Now let me also check the OpenAI model for the moonshotai case. Let me see what tests there are for moonshotai:
Assistant
Let me check the original code one more time to make sure the fix is correct:
Assistant
Now let me also check that my fix doesn't break the case where
openai_chat_send_back_thinking_parts='field'and the message has thinking. Let me trace:- Profile has
openai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_field='reasoning_content'. - Message has thinking content.
- Message has tool_calls.
self.thinkingsis non-empty (the thinking content).self.tool_callsis non-empty (the tool calls).- The fix is in the
if not self.thinkings:block, so it's skipped. - The thinking field is added normally via the loop.
So this case works correctly.
Now let me also trace:
- Profile has
openai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_field='reasoning_content'. - Message has thinking content but no tool_calls.
self.thinkingsis non-empty.self.tool_callsis empty.- The
if self.tool_calls:block is skipped. - The thinking field is added normally.
This case works correctly.
Now let me trace:
- Profile has
openai_chat_send_back_thinking_parts='auto'(no field set). - Message has only tool_calls, no thinking.
self.thinkingsis empty.self.tool_callsis non-empty.- The
if not self.thinkings:block is entered. - Profile check:
openai_chat_send_back_thinking_parts == 'field'is False, so the empty field is NOT added.
This case is correct - we don't add the empty field for 'auto' mode.
Now let me trace:
- Profile has
openai_chat_send_back_thinking_parts='tags'(default OpenAI). - Message has only tool_calls, no thinking.
self.thinkingsis empty.self.tool_callsis non-empty.- The
if not self.thinkings:block is entered. - Profile check:
openai_chat_send_back_thinking_parts == 'field'is False, so the empty field is NOT added.
This case is correct.
Now let me trace:
- Profile has
openai_chat_send_back_thinking_parts='field'andopenai_chat_thinking_field='reasoning_content'. - Message has only tool_calls, no thinking (synthetic message from ToolSearch).
self.thinkingsis empty.self.tool_callsis non-empty.- The
if not self.thinkings:block is entered. - Profile check:
openai_chat_send_back_thinking_parts == 'field'andopenai_chat_thinking_fieldis set, so the empty field IS added.
This is the fix case. Good.
Let me also check the streaming case. Let me look at how streaming sends back the messages.
Actually, streaming just generates a ModelResponse from the streamed events. So the fix should apply the same way.
Let me also test this with a simple streaming test.Let me also verify the fix with a streaming test:et call_tool_delta for 1 Let me also verify the fix with a streaming test: Let me also verify the fix doesn't break streaming. Let me look at the streaming code:```<tool_call>
- Profile has
Assistant
The streaming case uses the same
_map_model_responsemethod, so the fix applies to both streaming and non-streaming. Let me run a comprehensive test to make sure everything is still passing:Assistant
All 1363 tests pass. Let me now also verify my reproduction script one more time:
Assistant
The fix works. The synthetic assistant message now includes the
reasoning_contentfield with an empty string, which satisfies DeepSeek's API requirement.Let me also verify the fix in the multi-turn scenario:
Assistant
The fix works for multi-turn scenarios. The synthetic message includes the
reasoning_content: ''field.Let me also clean up my temp files:
Assistant
Good, all temp files are removed.
Now let me verify my final changes:
Assistant
The diff shows the clean and minimal fix. Let me verify one more time by running the relevant tests:
Assistant
All 1076 tests pass.
Let me also do a final check to verify the fix works for the exact scenario from the issue:
Assistant
The fix works perfectly for the exact scenario from the issue:
- The agent now executes successfully with
defer_loading=True. - All assistant messages with tool_calls include the
reasoning_contentfield. - The synthetic message from
ToolSearchcorrectly includes'reasoning_content': ''.
Let me clean up the temp file:
- The agent now executes successfully with
Assistant
Let me now look at my final fix one more time:
Assistant
Now let me also check if the OpenAI Chat Completions API spec allows arbitrary fields like
reasoning_content:The
ChatCompletionAssistantMessageParamis aTypedDictwithtotal=False. This means all fields are optional, but extra fields are accepted (TypedDict doesn't enforce strict matching at runtime). So adding extra fields is safe.Let me also do one final check - run all the tests once more:
Assistant
All 1363 tests pass.
Summary
Let me address the checklist:
-
If an error was reported in the issue description, I have successfully reproduced it. - I created reproduction scripts (
/tmp/repro.py,/tmp/repro2.py,/tmp/issue_repro.py) that demonstrated the bug: when usingCapability(defer_loading=True)with DeepSeek, the synthetic assistant message from theToolSearchcapability didn't include thereasoning_contentfield, which DeepSeek requires. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. - After applying the fix, I verified that the synthetic assistant message now includes
'reasoning_content': '', which satisfies DeepSeek's API requirement. The reproduction now shows all requests include thereasoning_contentfield on tool-call assistant messages. -
I have thought about edge cases and made sure my fix handles them as well. - The fix only adds the empty
reasoning_contentfield when:- The profile has
openai_chat_send_back_thinking_parts='field'(DeepSeek, MoonshotAI, OpenRouter case) - The profile has
openai_chat_thinking_fieldset - The message has tool_calls but no thinking content
This means:
- Messages with thinking content (real model messages) are unaffected.
- Messages without tool_calls are unaffected.
- Messages with 'auto' or 'tags' mode are unaffected.
- Messages with 'field' mode but the field already populated are unaffected.
- The profile has
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. - All 1363 tests in
test_capabilities.py,test_tool_search.py,test_openai.py,test_deepseek.py,test_openrouter.py, andtest_anthropic.pypass.
-
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
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →