Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/core/langchain_core/output_parsers/openai_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ def parse_result(self, result: list[Generation], *, partial: bool = False) -> An
msg = "This output parser can only be used with a chat generation."
raise OutputParserException(msg)
message = generation.message

# Preserve DeepSeek reasoning during OpenAI-tool parsing
if (
hasattr(message, "additional_kwargs")
and "reasoning_content" in message.additional_kwargs
and isinstance(message, AIMessage)
and message.tool_calls
):
message.additional_kwargs["_deepseek_reasoning"] = (
message.additional_kwargs["reasoning_content"]
)

if isinstance(message, AIMessage) and message.tool_calls:
parsed_tool_calls = [dict(tc) for tc in message.tool_calls]
for tool_call in parsed_tool_calls:
Expand Down
47 changes: 29 additions & 18 deletions libs/partners/deepseek/langchain_deepseek/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,25 +288,36 @@ def _create_chat_result(
if not isinstance(response, openai.BaseModel):
return rtn

for generation in rtn.generations:
if generation.message.response_metadata is None:
generation.message.response_metadata = {}
generation.message.response_metadata["model_provider"] = "deepseek"

choices = getattr(response, "choices", None)
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
)

for generation in rtn.generations:
msg = generation.message

if msg.response_metadata is None:
msg.response_metadata = {}
msg.response_metadata["model_provider"] = "deepseek"

# native reasoning_content
if choices and hasattr(choices[0].message, "reasoning_content"):
msg.additional_kwargs["reasoning_content"] = choices[
0
].message.reasoning_content
continue

# recovered reasoning from parser patch
if "_deepseek_reasoning" in msg.additional_kwargs:
msg.additional_kwargs["reasoning_content"] = msg.additional_kwargs[
"_deepseek_reasoning"
]
continue

# Handle use via OpenRouter
if choices and hasattr(choices[0].message, "model_extra"):
model_extra = choices[0].message.model_extra
if isinstance(model_extra, dict):
reasoning = model_extra.get("reasoning")
if reasoning:
msg.additional_kwargs["reasoning_content"] = reasoning

return rtn

Expand Down
Loading