From a692827f7835ec5ebb1b7669df1577e9935f8f77 Mon Sep 17 00:00:00 2001 From: Revinand Date: Wed, 4 Nov 2026 12:56:29 +0100 Subject: [PATCH] Python: Separate message texts with a newline in AgentResponse.text --- python/packages/core/agent_framework/_types.py | 4 ++-- python/packages/core/tests/core/test_types.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/python/packages/core/agent_framework/_types.py b/python/packages/core/agent_framework/_types.py index efb20f258d7..ac0ca12ae92 100644 --- a/python/packages/core/agent_framework/_types.py +++ b/python/packages/core/agent_framework/_types.py @@ -3785,8 +3785,8 @@ def __init__( @property def text(self) -> str: - """Get the concatenated text of all messages.""" - return "".join(msg.text for msg in self.messages) if self.messages else "" + """Get the concatenated text of all messages, with a newline between non-empty message texts.""" + return "\n".join(text for msg in self.messages if (text := msg.text)) @property def value(self) -> ResponseModelT | None: diff --git a/python/packages/core/tests/core/test_types.py b/python/packages/core/tests/core/test_types.py index f8547c1b7e5..aceceee7735 100644 --- a/python/packages/core/tests/core/test_types.py +++ b/python/packages/core/tests/core/test_types.py @@ -1545,7 +1545,7 @@ def test_agent_response_value_parses_final_message_with_response_format() -> Non response_format=OutputModel, ) - assert response.text == '{"skill_name": "building-permit-compliance"}{"response": "Hello"}' + assert response.text == '{"skill_name": "building-permit-compliance"}\n{"response": "Hello"}' assert response.value is not None assert response.value.response == "Hello" @@ -2254,7 +2254,19 @@ def test_agent_run_response_init_none_messages() -> None: def test_agent_run_response_text_property(chat_message: Message) -> None: response = AgentResponse(messages=[chat_message, chat_message]) - assert response.text == "HelloHello" + assert response.text == "Hello\nHello" + + +def test_agent_run_response_text_separates_messages_around_tool_calls() -> None: + # A preamble before a tool call and the final answer are separate messages and must not run together + response = AgentResponse( + messages=[ + Message("assistant", [Content.from_text("Let me check."), Content.from_function_call("c1", "get_weather")]), + Message("tool", [Content.from_function_result("c1", result="sunny")]), + Message("assistant", ["It is sunny."]), + ] + ) + assert response.text == "Let me check.\nIt is sunny." def test_agent_run_response_text_property_empty() -> None: