Skip to content

Commit 150d777

Browse files
authored
fix: empty a2a message (#2163)
* fix: build NewTask manually to allow empty messages * lint: fix ruff warn * refactor: add explicit AG2 prefix
1 parent 2606a72 commit 150d777

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

autogen/a2a/agent_executor.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from datetime import datetime, timezone
6+
from uuid import uuid4
67

78
from a2a.server.agent_execution import AgentExecutor, RequestContext
89
from a2a.server.events import EventQueue
9-
from a2a.types import TaskArtifactUpdateEvent, TaskState, TaskStatus, TaskStatusUpdateEvent
10-
from a2a.utils import new_task
10+
from a2a.types import Task, TaskArtifactUpdateEvent, TaskState, TaskStatus, TaskStatusUpdateEvent
1111
from a2a.utils.message import new_agent_text_message
1212

1313
from autogen import ConversableAgent
@@ -33,8 +33,17 @@ async def execute(self, context: RequestContext, event_queue: EventQueue) -> Non
3333

3434
task = context.current_task
3535
if not task:
36-
task = new_task(context.message)
37-
task.status.timestamp = datetime.now(timezone.utc).isoformat()
36+
request = context.message
37+
# build task object manually to allow empty messages
38+
task = Task(
39+
status=TaskStatus(
40+
state=TaskState.submitted,
41+
timestamp=datetime.now(timezone.utc).isoformat(),
42+
),
43+
id=request.task_id or str(uuid4()),
44+
context_id=request.context_id or str(uuid4()),
45+
history=[request],
46+
)
3847
# publish the task status submitted event
3948
await event_queue.enqueue_event(task)
4049

autogen/a2a/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
from autogen.remote.protocol import RequestMessage, ResponseMessage
1212

13-
CLIENT_TOOLS_KEY = "ag2_client_tools"
14-
CONTEXT_KEY = "ag2_context_update"
13+
AG2_METADATA_KEY_PREFIX = "ag2_"
14+
CLIENT_TOOLS_KEY = f"{AG2_METADATA_KEY_PREFIX}client_tools"
15+
CONTEXT_KEY = f"{AG2_METADATA_KEY_PREFIX}context_update"
1516

1617

1718
def request_message_to_a2a(

autogen/oai/client_utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,10 @@ def merge_config_with_tools(config: dict[str, Any], client_config: dict[str, Any
129129
full_config = {**config, **client_config}
130130

131131
# Add tools if tools contains something AND are not using deprecated functions
132-
config_tools = config.get("tools", [])
133-
client_tools = client_config.get("tools", [])
134-
135-
if config_tools or client_tools:
132+
tools = config.get("tools", []) + client_config.get("tools", [])
133+
if tools and "functions" not in full_config:
136134
# Don't add tools if functions parameter is present (deprecated API)
137-
if "functions" not in full_config:
138-
full_config["tools"] = config_tools + client_tools
135+
full_config["tools"] = tools
139136

140137
return full_config
141138

test/a2a/chats/test_chat.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ async def test_simple_messaging(remote_agent: ConversableAgent, a2a_client: Http
4444
}
4545

4646

47+
@pytest.mark.asyncio()
48+
async def test_empty_message_send(remote_agent: ConversableAgent, a2a_client: HttpxClientFactory) -> None:
49+
# arrange
50+
remote_agent_mirror = A2aRemoteAgent(url="http://memory", name="remote-mirror", client=a2a_client)
51+
52+
with TestAgent(remote_agent, ["Hi, I am remote agent!"]):
53+
# act
54+
_, message = await remote_agent_mirror.a_generate_remote_reply([
55+
{"content": ""},
56+
])
57+
58+
# assert
59+
assert message == {
60+
"content": "Hi, I am remote agent!",
61+
"name": "remote",
62+
"role": "assistant",
63+
}
64+
65+
4766
@pytest.mark.asyncio()
4867
async def test_conversation(remote_agent: ConversableAgent, a2a_client: HttpxClientFactory) -> None:
4968
# arrange

0 commit comments

Comments
 (0)