From 55336fe06c6fd64102621a81ef5c72daae7fb3e8 Mon Sep 17 00:00:00 2001 From: aabhathanki Date: Mon, 26 Jan 2026 00:44:31 +0530 Subject: [PATCH] Fix HttpTool serialization in StaticStreamWorkbench --- .../src/autogen_ext/tools/http/_http_tool.py | 2 +- .../tools/test_http_tool_serialization.py | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 python/packages/autogen-ext/tests/tools/test_http_tool_serialization.py diff --git a/python/packages/autogen-ext/src/autogen_ext/tools/http/_http_tool.py b/python/packages/autogen-ext/src/autogen_ext/tools/http/_http_tool.py index c519143be4c5..13e0d247dcd0 100644 --- a/python/packages/autogen-ext/src/autogen_ext/tools/http/_http_tool.py +++ b/python/packages/autogen-ext/src/autogen_ext/tools/http/_http_tool.py @@ -42,7 +42,7 @@ class HttpToolConfig(BaseModel): """ The HTTP method to use, will default to POST if not provided. """ - headers: Optional[dict[str, Any]] + headers: Optional[dict[str, Any]] = None """ A dictionary of headers to send with the request. """ diff --git a/python/packages/autogen-ext/tests/tools/test_http_tool_serialization.py b/python/packages/autogen-ext/tests/tools/test_http_tool_serialization.py new file mode 100644 index 000000000000..e0c093ff931e --- /dev/null +++ b/python/packages/autogen-ext/tests/tools/test_http_tool_serialization.py @@ -0,0 +1,91 @@ +"""Tests for HttpTool serialization and deserialization (Issue #7172)""" + +import pytest +from autogen_core.tools import StaticStreamWorkbench +from autogen_ext.tools.http import HttpTool + + +@pytest.mark.asyncio +async def test_httptool_config_serialization(): + """Test HttpTool can be serialized and deserialized directly""" + tool = HttpTool( + name="test_tool", + description="test description", + scheme="https", + host="httpbin.org", + port=443, + path="/get", + method="GET", + json_schema={"type": "object", "properties": {}}, + headers={"Authorization": "Bearer test", "Custom-Header": "value"}, + ) + + # Serialize + config = tool._to_config() + + # Deserialize + restored_tool = HttpTool._from_config(config) + + # Verify all fields are preserved + assert restored_tool.name == tool.name + assert restored_tool.server_params.headers == tool.server_params.headers + assert restored_tool.server_params.host == tool.server_params.host + assert restored_tool.server_params.port == tool.server_params.port + + +@pytest.mark.asyncio +async def test_httptool_serialization_without_headers(): + """Test HttpTool serialization works when headers is None""" + tool = HttpTool( + name="test_tool_no_headers", + description="test without headers", + scheme="https", + host="httpbin.org", + port=443, + path="/get", + method="GET", + json_schema={"type": "object", "properties": {}}, + # headers not provided (defaults to None) + ) + + # Serialize and deserialize + config = tool._to_config() + restored_tool = HttpTool._from_config(config) + + # Verify headers is None + assert restored_tool.server_params.headers is None + + +@pytest.mark.asyncio +async def test_static_workbench_httptool_serialization(): + """Test StaticStreamWorkbench can save/load with HttpTool (Issue #7172)""" + tool = HttpTool( + name="base64_decode", + description="base64 decode a value", + scheme="https", + host="httpbin.org", + port=443, + path="/base64/{value}", + method="GET", + json_schema={ + "type": "object", + "properties": { + "value": {"type": "string", "description": "The base64 value to decode"}, + }, + "required": ["value"], + }, + ) + + # Create workbench + workbench = StaticStreamWorkbench(tools=[tool]) + + # Serialize config + config = workbench._to_config() + + # This should not raise validation error (was failing before fix) + new_workbench = StaticStreamWorkbench._from_config(config) + + # Verify workbench works + tools = await new_workbench.list_tools() + assert len(tools) == 1 + assert tools[0]["name"] == "base64_decode" \ No newline at end of file