From 33a6def76295f341fc499a53428b75efca1982af Mon Sep 17 00:00:00 2001 From: Mohammad Mohtashim <45242107+keenborder786@users.noreply.github.com> Date: Mon, 29 Sep 2025 18:17:05 +0500 Subject: [PATCH] fix(core): Support of 'reasoning' type in 'convert_to_openai_messages' (#33050) --- libs/core/langchain_core/messages/utils.py | 2 +- .../tests/unit_tests/messages/test_utils.py | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.py index ea72ae06bad..d479d41e8bc 100644 --- a/libs/core/langchain_core/messages/utils.py +++ b/libs/core/langchain_core/messages/utils.py @@ -1385,7 +1385,7 @@ def convert_to_openai_messages( }, } ) - elif block.get("type") == "thinking": + elif block.get("type") in ["thinking", "reasoning"]: content.append(block) else: err = ( diff --git a/libs/core/tests/unit_tests/messages/test_utils.py b/libs/core/tests/unit_tests/messages/test_utils.py index bedd518589e..5d324401de4 100644 --- a/libs/core/tests/unit_tests/messages/test_utils.py +++ b/libs/core/tests/unit_tests/messages/test_utils.py @@ -1484,3 +1484,64 @@ def test_get_buffer_string_with_empty_content() -> None: expected = "Human: \nAI: \nSystem: " actual = get_buffer_string(messages) assert actual == expected + + +def test_convert_to_openai_messages_reasoning_content() -> None: + """Test convert_to_openai_messages with reasoning content blocks.""" + # Test reasoning block with empty summary + msg = AIMessage(content=[{"type": "reasoning", "summary": []}]) + result = convert_to_openai_messages(msg, text_format="block") + expected = {"role": "assistant", "content": [{"type": "reasoning", "summary": []}]} + assert result == expected + + # Test reasoning block with summary content + msg_with_summary = AIMessage( + content=[ + { + "type": "reasoning", + "summary": [ + {"type": "text", "text": "First thought"}, + {"type": "text", "text": "Second thought"}, + ], + } + ] + ) + result_with_summary = convert_to_openai_messages( + msg_with_summary, text_format="block" + ) + expected_with_summary = { + "role": "assistant", + "content": [ + { + "type": "reasoning", + "summary": [ + {"type": "text", "text": "First thought"}, + {"type": "text", "text": "Second thought"}, + ], + } + ], + } + assert result_with_summary == expected_with_summary + + # Test mixed content with reasoning and text + mixed_msg = AIMessage( + content=[ + {"type": "text", "text": "Regular response"}, + { + "type": "reasoning", + "summary": [{"type": "text", "text": "My reasoning process"}], + }, + ] + ) + mixed_result = convert_to_openai_messages(mixed_msg, text_format="block") + expected_mixed = { + "role": "assistant", + "content": [ + {"type": "text", "text": "Regular response"}, + { + "type": "reasoning", + "summary": [{"type": "text", "text": "My reasoning process"}], + }, + ], + } + assert mixed_result == expected_mixed