From 50fa524a6dba2f19c7351d85cf4738d0ce804bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=B0=8F=E9=B8=AD?= Date: Tue, 6 May 2025 06:31:58 +0800 Subject: [PATCH] partners: (langchain-deepseek) fix deepseek-r1 always returns an empty `reasoning_content` when reasoning (#31065) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description deepseek-r1 always returns an empty string `reasoning_content` to the first chunk when thinking, and sets `reasoning_content` to None when thinking is over, to determine when to switch to normal output. Therefore, whether the reasoning_content field exists should be judged as None. ## Demo deepseek-r1 reasoning output: ``` {'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': 'assistant', 'tool_calls': None, 'reasoning_content': ''}, 'finish_reason': None, 'index': 0, 'logprobs': None} {'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': '好的'}, 'finish_reason': None, 'index': 0, 'logprobs': None} {'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': ','}, 'finish_reason': None, 'index': 0, 'logprobs': None} {'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': '用户'}, 'finish_reason': None, 'index': 0, 'logprobs': None} ... ``` deepseek-r1 first normal output ``` ... {'delta': {'content': ' main', 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': None}, 'finish_reason': None, 'index': 0, 'logprobs': None} {'delta': {'content': '\n\nimport', 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': None}, 'finish_reason': None, 'index': 0, 'logprobs': None} ... ``` --------- Co-authored-by: ccurme --- libs/partners/deepseek/langchain_deepseek/chat_models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/partners/deepseek/langchain_deepseek/chat_models.py b/libs/partners/deepseek/langchain_deepseek/chat_models.py index 9166e61cfa0..155b192c19a 100644 --- a/libs/partners/deepseek/langchain_deepseek/chat_models.py +++ b/libs/partners/deepseek/langchain_deepseek/chat_models.py @@ -255,12 +255,14 @@ class ChatDeepSeek(BaseChatOpenAI): if (choices := chunk.get("choices")) and generation_chunk: top = choices[0] if isinstance(generation_chunk.message, AIMessageChunk): - if reasoning_content := top.get("delta", {}).get("reasoning_content"): + if ( + reasoning_content := top.get("delta", {}).get("reasoning_content") + ) is not None: generation_chunk.message.additional_kwargs["reasoning_content"] = ( reasoning_content ) # Handle use via OpenRouter - elif reasoning := top.get("delta", {}).get("reasoning"): + elif (reasoning := top.get("delta", {}).get("reasoning")) is not None: generation_chunk.message.additional_kwargs["reasoning_content"] = ( reasoning )