-
Notifications
You must be signed in to change notification settings - Fork 20k
feat(deepseek): support interleaved reasoning in multi-turn tool call loop #34177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat(deepseek): support interleaved reasoning in multi-turn tool call loop #34177
Conversation
Signed-off-by: Alan Chen <[email protected]>
Signed-off-by: Alan Chen <[email protected]>
476dacb to
2a29e45
Compare
|
|
||
| @staticmethod | ||
| def _set_reasoning(message: BaseMessage, reasoning: str) -> None: | ||
| """Set reasoning content on a message with both key names for compatibility. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compatibility with what? prefer not to make this change if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was meant for compatibility with langchain-openai's convention to store reasoning text in the reasoning.
and in case OpenRouter model expects reasoning in the result.
Below are code 297-309 from master branch
if choices and hasattr(choices[0].message, "reasoning_content"):
rtn.generations[0].message.additional_kwargs["reasoning_content"] = choices[
0
].message.reasoning_content
# Handle use via OpenRouter
elif choices and hasattr(choices[0].message, "model_extra"):
model_extra = choices[0].message.model_extra
if isinstance(model_extra, dict) and (
reasoning := model_extra.get("reasoning")
):
rtn.generations[0].message.additional_kwargs["reasoning_content"] = (
reasoning
)
I can remove the message.additional_kwargs["reasoning"] = reasoning and only keep it in message.additional_kwargs["reasoning_content"]
|
After several test I noticed that some provider like MiniMax and OpenRouter actually put the reasoning content in In fact, |
…dundant reasoning field Signed-off-by: Alan Chen <[email protected]>
…compatibility Signed-off-by: Alan Chen <[email protected]>
Signed-off-by: Alan Chen <[email protected]>
|
This PR is ready for a review. The reasoning content may come from three different place:
They will all be stored into Some providers (via OpenRouter) return reasoning with special metadata or encrypted reasoning that require special handling and are currently bypassed:
To get the complete support, consider request through Provider directly instead of OpenRouter For rest of provider via OpenRouter, the The only exception that reasoning text will be duplicated is where the |
|
我也遇到了类似问题,请问这个问题已经修复了吗,我该如何升级来支持 deepseek-reasoner 模型来绑定 tool |
This PR should have resolve the issue for |
Description
This PR add support for preserving
reasoning_contentfrom previous AI messages and re-injecting it into the request payload for subsequent API calls forChatDeepSeek. This add support for interleaved thinking feature where the model can access its earlier reasoning content in multi-turn tool call.Previously,
ChatDeepSeekwas dropping this field when converting messages to the API format. However, their official API now specifically requires to include thereasoning_contentfield in multi-turn tool call loop (see #34166)Background
DeepSeek's new model DeepSeek V3.2 support interleaved thinking in their thinking mode, which means the
reasoning_contentis also appear for tool calls and expect to send back to keep complete CoT.This changes will also benefit some of other models like GLM 4.6, Minimax M2 and Kimi-k2-thinking, which previously can only use
langcahin_anthropicfor interleaved thinking supportNo breaking changes.
Standalone test to verify interleaved thinking
In addition to unit test added, I have created standalone test script to verify the interleaved thinking in actual agent set up
Test Setup:
System instruction:
Tools provided:
choose_numberchoose_colormystery_tasksubmit_answersHow it verifies interleaved reasoning:
choose_number- reasoning happens inreasoning_contentonlychoose_color- reasoning happens inreasoning_contentonlymystery_task- It will asks model "WHY did you choose that number/color?"Before the fix (click to expand)
The DeepSeek v3.2 would raise error as we did not pass back the
reasoning_contentThe GLM 4.6 model would have no idea of why it choose the number and color in first place
After the fix (click to expand)