diff --git a/python/packages/github_copilot/agent_framework_github_copilot/_agent.py b/python/packages/github_copilot/agent_framework_github_copilot/_agent.py index 92c47f970b1..7e3a8e428aa 100644 --- a/python/packages/github_copilot/agent_framework_github_copilot/_agent.py +++ b/python/packages/github_copilot/agent_framework_github_copilot/_agent.py @@ -1601,7 +1601,7 @@ async def handler(invocation: ToolInvocation) -> ToolResult: ) text = "\n".join(c.text for c in result if c.type == "text" and c.text) return ToolResult( - text_result_for_llm=text or str(result), + text_result_for_llm=text, result_type="success", ) except Exception as e: diff --git a/python/packages/github_copilot/tests/test_github_copilot_agent.py b/python/packages/github_copilot/tests/test_github_copilot_agent.py index 277d558f301..25cae12a2fd 100644 --- a/python/packages/github_copilot/tests/test_github_copilot_agent.py +++ b/python/packages/github_copilot/tests/test_github_copilot_agent.py @@ -2926,6 +2926,38 @@ def no_args_tool() -> str: assert result.result_type == "success" assert result.text_result_for_llm == "no args result" + @pytest.mark.parametrize( + "tool_return", + [None, "", Content.from_data(data=b"\x89PNG\r\n\x1a\n", media_type="image/png")], + ids=["none", "empty_string", "image_only"], + ) + async def test_tool_handler_returns_empty_text_when_tool_returns_no_text( + self, + mock_client: MagicMock, + mock_session: MagicMock, + tool_return: Any, + ) -> None: + """Test that a tool result without text reaches the model as an empty string, not as a Python repr.""" + + def no_text_tool() -> Any: + """A tool that returns no text.""" + return tool_return + + agent = GitHubCopilotAgent(client=mock_client, tools=[no_text_tool]) + await agent.start() + + await agent._get_or_create_session(AgentSession()) # type: ignore + + call_args = mock_client.create_session.call_args + config = call_args.kwargs + copilot_tool = config["tools"][0] + + result = await copilot_tool.handler(ToolInvocation(arguments={})) + + assert isinstance(result, ToolResult) + assert result.result_type == "success" + assert result.text_result_for_llm == "" + async def test_run_forwards_function_invocation_kwargs_to_tools( self, mock_client: MagicMock,