mirror of
https://github.com/hwchase17/langchain.git
synced 2026-03-18 11:07:36 +00:00
fix(core): Support of 'reasoning' type in 'convert_to_openai_messages' (#33050)
This commit is contained in:
committed by
GitHub
parent
c456c8ae51
commit
33a6def762
@@ -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 = (
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user