A programming framework for agentic AI
Feature/agentchat message id field 6317 (#6645)
Tejas Dharanida20f7c6c72b
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.pyindex 905df4f9d..6ecf6cf73 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py@@ -1,20 +1,11 @@+from __future__ import annotations+import asyncioimport jsonimport logging+import uuidimport warnings-from typing import (- Any,- AsyncGenerator,- Awaitable,- Callable,- Dict,- List,- Mapping,- Optional,- Sequence,- Tuple,- Union,-)+from typing import Any, AsyncGenerator, Awaitable, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, Unionfrom autogen_core import CancellationToken, Component, ComponentModel, FunctionCallfrom autogen_core.memory import Memory@@ -140,12 +131,12 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):.. tip::By default, the tool call results are returned as the response when tool- calls are made, so pay close attention to how the tools’ return values+ calls are made, so pay close attention to how the tools' return valuesare formatted—especially if another agent expects a specific schema.* Use **`tool_call_summary_format`** for a simple static template.* Use **`tool_call_summary_formatter`** for full programmatic control- (e.g., “hide large success payloads, show full details on error”).+ (e.g., "hide large success payloads, show full details on error").*Note*: `tool_call_summary_formatter` is **not serializable** and willbe ignored when an agent is loaded from, or exported to, YAML/JSON@@ -555,7 +546,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):- Start with a pizza crust (store-bought or homemade).- Spread a layer of marinara or tomato sauce evenly over the crust.- Top with your favorite vegetables like bell peppers, mushrooms, onions, olives, and spinach.- - Add some protein if you’d like, such as grilled chicken or pepperoni (ensure it's cheese-free).+ - Add some protein if you'd like, such as grilled chicken or pepperoni (ensure it's cheese-free).- Sprinkle with herbs like oregano and basil, and maybe a drizzle of olive oil.- Bake according to the crust instructions until the edges are golden and the veggies are cooked.@@ -847,7 +838,10 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):inner_messages.append(event_msg)yield event_msg- # STEP 3: Run the first inference+ # STEP 3: Generate a message ID for correlation between streaming chunks and final message+ message_id = str(uuid.uuid4())++ # STEP 4: Run the first inferencemodel_result = Noneasync for inference_output in self._call_llm(model_client=model_client,@@ -859,6 +853,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):agent_name=agent_name,cancellation_token=cancellation_token,output_content_type=output_content_type,+ message_id=message_id,):if isinstance(inference_output, CreateResult):model_result = inference_output@@ -883,7 +878,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):))- # STEP 4: Process the model output+ # STEP 5: Process the model outputasync for output_event in self._process_model_result(model_result=model_result,inner_messages=inner_messages,@@ -900,6 +895,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):tool_call_summary_format=tool_call_summary_format,tool_call_summary_formatter=tool_call_summary_formatter,output_content_type=output_content_type,+ message_id=message_id,format_string=format_string,):yield output_event@@ -951,6 +947,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):agent_name: str,cancellation_token: CancellationToken,output_content_type: type[BaseModel] | None,+ message_id: str,) -> AsyncGenerator[Union[CreateResult, ModelClientStreamingChunkEvent], None]:"""Perform a model inference and yield either streaming chunk events or the final CreateResult.@@ -962,6 +959,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):if model_client_stream:model_result: Optional[CreateResult] = None+async for chunk in model_client.create_stream(llm_messages,tools=tools,@@ -971,7 +969,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):if isinstance(chunk, CreateResult):model_result = chunkelif isinstance(chunk, str):- yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name)+ yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name, full_message_id=message_id)else:raise RuntimeError(f"Invalid chunk type: {type(chunk)}")if model_result is None:@@ -1004,6 +1002,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):tool_call_summary_format: str,tool_call_summary_formatter: Callable[[FunctionCall, FunctionExecutionResult], str] | None,output_content_type: type[BaseModel] | None,+ message_id: str,format_string: str | None = None,) -> AsyncGenerator[BaseAgentEvent | BaseChatMessage | Response, None]:"""@@ -1013,6 +1012,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):# If direct text response (string)if isinstance(model_result.content, str):+ # Use the passed message ID for the final messageif output_content_type:content = output_content_type.model_validate_json(model_result.content)yield Response(@@ -1021,6 +1021,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):source=agent_name,models_usage=model_result.usage,format_string=format_string,+ id=message_id,),inner_messages=inner_messages,)@@ -1030,6 +1031,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):content=model_result.content,source=agent_name,models_usage=model_result.usage,+ id=message_id,),inner_messages=inner_messages,)@@ -1202,6 +1204,9 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):reflection_result: Optional[CreateResult] = None+ # Generate a message ID for correlation between chunks and final message in reflection flow+ reflection_message_id = str(uuid.uuid4())+if model_client_stream:async for chunk in model_client.create_stream(llm_messages,@@ -1210,7 +1215,9 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):if isinstance(chunk, CreateResult):reflection_result = chunkelif isinstance(chunk, str):- yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name)+ yield ModelClientStreamingChunkEvent(+ content=chunk, source=agent_name, full_message_id=reflection_message_id+ )else:raise RuntimeError(f"Invalid chunk type: {type(chunk)}")else:@@ -1241,6 +1248,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):content=content,source=agent_name,models_usage=reflection_result.usage,+ id=reflection_message_id,),inner_messages=inner_messages,)@@ -1250,6 +1258,7 @@ class AssistantAgent(BaseChatAgent, Component[AssistantAgentConfig]):content=reflection_result.content,source=agent_name,models_usage=reflection_result.usage,+ id=reflection_message_id,),inner_messages=inner_messages,)diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py b/python/packages/autogen-agentchat/src/autogen_agentchat/messages.pyindex 715e59ad4..c67062c75 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py@@ -4,6 +4,7 @@ Each message type inherits either from the BaseChatMessage class or BaseAgentEveclass and includes specific fields relevant to the type of message being sent."""+import uuidfrom abc import ABC, abstractmethodfrom datetime import datetime, timezonefrom typing import Any, Dict, Generic, List, Literal, Mapping, Optional, Type, TypeVar@@ -77,6 +78,9 @@ class BaseChatMessage(BaseMessage, ABC):message using models and return a response as another :class:`BaseChatMessage`."""+ id: str = Field(default_factory=lambda: str(uuid.uuid4()))+ """Unique identifier for this message."""+source: str"""The name of the agent that sent this message."""@@ -149,6 +153,9 @@ class BaseAgentEvent(BaseMessage, ABC):a custom rendering of the content."""+ id: str = Field(default_factory=lambda: str(uuid.uuid4()))+ """Unique identifier for this event."""+source: str"""The name of the agent that sent this message."""@@ -523,6 +530,10 @@ class ModelClientStreamingChunkEvent(BaseAgentEvent):content: str"""A string chunk from the model client."""+ full_message_id: str | None = None+ """Optional reference to the complete message that may come after the chunks.+ This allows consumers of the stream to correlate chunks with the eventual completed message."""+type: Literal["ModelClientStreamingChunkEvent"] = "ModelClientStreamingChunkEvent"def to_text(self) -> str:diff --git a/python/packages/autogen-agentchat/tests/test_assistant_agent.py b/python/packages/autogen-agentchat/tests/test_assistant_agent.pyindex 219443a21..5ffb731c1 100644--- a/python/packages/autogen-agentchat/tests/test_assistant_agent.py+++ b/python/packages/autogen-agentchat/tests/test_assistant_agent.py@@ -1419,8 +1419,8 @@ async def test_structured_message_factory_serialization() -> None:result = await restored_agent.run()assert isinstance(result.messages[0], StructuredMessage)- assert result.messages[0].content.result == "All good" # type: ignore[reportUnknownMemberType]- assert result.messages[0].content.status == "ok" # type: ignore[reportUnknownMemberType]+ assert result.messages[0].content.result == "All good" # type: ignore+ assert result.messages[0].content.status == "ok" # type: ignore@pytest.mark.asynciodiff --git a/python/packages/autogen-agentchat/tests/test_messages.py b/python/packages/autogen-agentchat/tests/test_messages.pyindex 17f53f27e..0e5ff78dd 100644--- a/python/packages/autogen-agentchat/tests/test_messages.py+++ b/python/packages/autogen-agentchat/tests/test_messages.py@@ -1,4 +1,5 @@import json+import uuidfrom typing import Listimport pytest@@ -54,7 +55,7 @@ def test_structured_message() -> None:def test_structured_message_component() -> None:- # Create a structured message with the test contentformat_string="this is a string {field1} and this is an int {field2}"+ # Create a structured message with the test contentformat_string = "this is a string {field1} and this is an int {field2}"s_m = StructuredMessageFactory(input_model=TestContent, format_string=format_string)config = s_m.dump_component()@@ -187,3 +188,114 @@ def test_union_types() -> None:loaded_container = TestContainer.model_validate(data)assert loaded_container.chat_messages == chat_messagesassert loaded_container.agent_events == agent_events+++def test_message_id_field() -> None:+ """Test that messages have unique ID fields automatically generated."""+ # Test BaseChatMessage subclass (TextMessage)+ message1 = TextMessage(source="test_agent", content="Hello, world!")+ message2 = TextMessage(source="test_agent", content="Hello, world!")++ # Check that IDs are present and unique+ assert hasattr(message1, "id")+ assert hasattr(message2, "id")+ assert message1.id != message2.id+ assert isinstance(message1.id, str)+ assert isinstance(message2.id, str)++ # Check that IDs are valid UUIDs+ try:+ uuid.UUID(message1.id)+ uuid.UUID(message2.id)+ except ValueError:+ pytest.fail("Generated IDs are not valid UUIDs")++ # Test BaseAgentEvent subclass (ModelClientStreamingChunkEvent)+ event1 = ModelClientStreamingChunkEvent(source="test_agent", content="chunk1")+ event2 = ModelClientStreamingChunkEvent(source="test_agent", content="chunk2")++ # Check that IDs are present and unique+ assert hasattr(event1, "id")+ assert hasattr(event2, "id")+ assert event1.id != event2.id+ assert isinstance(event1.id, str)+ assert isinstance(event2.id, str)++ # Check that IDs are valid UUIDs+ try:+ uuid.UUID(event1.id)+ uuid.UUID(event2.id)+ except ValueError:+ pytest.fail("Generated IDs are not valid UUIDs")+++def test_custom_message_id() -> None:+ """Test that custom IDs can be provided."""+ custom_id = "custom-message-id-123"+ message = TextMessage(id=custom_id, source="test_agent", content="Hello, world!")++ assert message.id == custom_id++ custom_event_id = "custom-event-id-456"+ event = ModelClientStreamingChunkEvent(id=custom_event_id, source="test_agent", content="chunk")++ assert event.id == custom_event_id+++def test_streaming_chunk_full_message_id() -> None:+ """Test the full_message_id field in ModelClientStreamingChunkEvent."""+ # Test without full_message_id+ chunk1 = ModelClientStreamingChunkEvent(source="test_agent", content="chunk1")+ assert chunk1.full_message_id is None++ # Test with full_message_id+ full_msg_id = "full-message-123"+ chunk2 = ModelClientStreamingChunkEvent(source="test_agent", content="chunk2", full_message_id=full_msg_id)+ assert chunk2.full_message_id == full_msg_id++ # Test that chunk has its own ID separate from full_message_id+ assert chunk2.id != chunk2.full_message_id+ assert isinstance(chunk2.id, str)++ # Verify chunk ID is a valid UUID+ try:+ uuid.UUID(chunk2.id)+ except ValueError:+ pytest.fail("Chunk ID is not a valid UUID")+++def test_message_serialization_with_id() -> None:+ """Test that messages with IDs serialize and deserialize correctly."""+ # Create a message with auto-generated ID+ original_message = TextMessage(source="test_agent", content="Hello, world!")+ original_id = original_message.id++ # Serialize to dict+ message_data = original_message.model_dump()+ assert "id" in message_data+ assert message_data["id"] == original_id++ # Deserialize from dict+ restored_message = TextMessage.model_validate(message_data)+ assert restored_message.id == original_id+ assert restored_message.source == "test_agent"+ assert restored_message.content == "Hello, world!"++ # Test with streaming chunk event+ original_chunk = ModelClientStreamingChunkEvent(+ source="test_agent", content="chunk", full_message_id="full-msg-123"+ )+ original_chunk_id = original_chunk.id++ # Serialize to dict+ chunk_data = original_chunk.model_dump()+ assert "id" in chunk_data+ assert "full_message_id" in chunk_data+ assert chunk_data["id"] == original_chunk_id+ assert chunk_data["full_message_id"] == "full-msg-123"++ # Deserialize from dict+ restored_chunk = ModelClientStreamingChunkEvent.model_validate(chunk_data)+ assert restored_chunk.id == original_chunk_id+ assert restored_chunk.full_message_id == "full-msg-123"+ assert restored_chunk.content == "chunk"diff --git a/python/packages/autogen-agentchat/tests/test_streaming_message_id_correlation.py b/python/packages/autogen-agentchat/tests/test_streaming_message_id_correlation.pynew file mode 100644index 000000000..9031c532a--- /dev/null+++ b/python/packages/autogen-agentchat/tests/test_streaming_message_id_correlation.py@@ -0,0 +1,99 @@+from typing import List, Optional++import pytest+from autogen_agentchat.agents import AssistantAgent+from autogen_agentchat.base import TaskResult+from autogen_agentchat.messages import ModelClientStreamingChunkEvent, TextMessage+from autogen_core import FunctionCall+from autogen_core.models import CreateResult, ModelFamily, RequestUsage+from autogen_ext.models.replay import ReplayChatCompletionClient+++async def _echo_function(input: str) -> str:+ return input+++async def test_streaming_message_id_correlation() -> None:+ """Test that streaming chunks have full_message_id that matches final message ID."""+ mock_client = ReplayChatCompletionClient(+ [+ "Response to message",+ ]+ )+ agent = AssistantAgent(+ "test_agent",+ model_client=mock_client,+ model_client_stream=True,+ )++ # Track all chunks and the final message+ chunks: List[ModelClientStreamingChunkEvent] = []+ final_message: Optional[TextMessage] = None++ async for message in agent.run_stream(task="task"):+ if isinstance(message, TaskResult):+ assert len(message.messages) == 2+ assert isinstance(message.messages[0], TextMessage)+ assert isinstance(message.messages[1], TextMessage)+ final_message = message.messages[1]+ elif isinstance(message, ModelClientStreamingChunkEvent):+ chunks.append(message)++ # Verify we got chunks and a final message+ assert len(chunks) > 0+ assert final_message is not None++ # Every chunk should have the same full_message_id as the final message's id+ for chunk in chunks:+ assert chunk.full_message_id == final_message.id++ # Test the reflect_on_tool_use streaming case+ mock_client = ReplayChatCompletionClient(+ [+ CreateResult(+ content=[+ FunctionCall(id="1", name="_echo_function", arguments=r'{"input": "task"}'),+ ],+ finish_reason="function_calls",+ usage=RequestUsage(prompt_tokens=10, completion_tokens=5),+ cached=False,+ ),+ "Example reflection response",+ ],+ model_info={+ "function_calling": True,+ "vision": False,+ "json_output": False,+ "family": ModelFamily.GPT_4,+ "structured_output": False,+ },+ )++ agent = AssistantAgent(+ "test_agent",+ model_client=mock_client,+ model_client_stream=True,+ reflect_on_tool_use=True,+ tools=[_echo_function],+ )++ # Track reflection chunks and final message+ reflection_chunks: List[ModelClientStreamingChunkEvent] = []+ final_reflection_message: Optional[TextMessage] = None++ async for message in agent.run_stream(task="task"):+ if isinstance(message, TaskResult):+ # The last message should be the reflection result+ if isinstance(message.messages[-1], TextMessage):+ final_reflection_message = message.messages[-1]+ elif isinstance(message, ModelClientStreamingChunkEvent):+ reflection_chunks.append(message)++ # Verify we got reflection chunks and a final message+ assert len(reflection_chunks) > 0+ assert final_reflection_message is not None++ # Every reflection chunk should have the same full_message_id as the final message's id+ for chunk in reflection_chunks:+ assert chunk.full_message_id == final_reflection_message.id # type: ignore