Skip to content

Commit 50fa524

Browse files
reatangccurme
andauthored
partners: (langchain-deepseek) fix deepseek-r1 always returns an empty reasoning_content when reasoning (#31065)
## 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 <chester.curme@gmail.com>
1 parent c0b6980 commit 50fa524

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

libs/partners/deepseek/langchain_deepseek/chat_models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,14 @@ def _convert_chunk_to_generation_chunk(
255255
if (choices := chunk.get("choices")) and generation_chunk:
256256
top = choices[0]
257257
if isinstance(generation_chunk.message, AIMessageChunk):
258-
if reasoning_content := top.get("delta", {}).get("reasoning_content"):
258+
if (
259+
reasoning_content := top.get("delta", {}).get("reasoning_content")
260+
) is not None:
259261
generation_chunk.message.additional_kwargs["reasoning_content"] = (
260262
reasoning_content
261263
)
262264
# Handle use via OpenRouter
263-
elif reasoning := top.get("delta", {}).get("reasoning"):
265+
elif (reasoning := top.get("delta", {}).get("reasoning")) is not None:
264266
generation_chunk.message.additional_kwargs["reasoning_content"] = (
265267
reasoning
266268
)

0 commit comments

Comments
 (0)