diff --git a/backend/app/services/llm/utils.py b/backend/app/services/llm/utils.py index 27f2b5b47..3d8bbd128 100644 --- a/backend/app/services/llm/utils.py +++ b/backend/app/services/llm/utils.py @@ -136,8 +136,8 @@ def convert_chat_messages_to_llm_format(messages) -> list[dict]: continue # Skip malformed tool_call records else: entry: dict = {"role": msg.role, "content": msg.content} - if hasattr(msg, "thinking") and msg.thinking: - entry["thinking"] = msg.thinking + if msg.role == "assistant" and hasattr(msg, "thinking") and msg.thinking: + entry["reasoning_content"] = msg.thinking result.append(entry) return result diff --git a/backend/tests/test_llm_utils.py b/backend/tests/test_llm_utils.py new file mode 100644 index 000000000..527f80c4f --- /dev/null +++ b/backend/tests/test_llm_utils.py @@ -0,0 +1,20 @@ +from types import SimpleNamespace +from app.services.llm.utils import convert_chat_messages_to_llm_format + +def test_convert_chat_messages_preserves_reasoning_content(): + message = SimpleNamespace( + id="message-1", + role="assistant", + content="I found the answer.", + thinking="I should inspect the evidence first.", + ) + + result = convert_chat_messages_to_llm_format([message]) + + assert result == [ + { + "role": "assistant", + "content": "I found the answer.", + "reasoning_content": "I should inspect the evidence first.", + } + ] \ No newline at end of file