diff --git a/python/packages/core/agent_framework/_agents.py b/python/packages/core/agent_framework/_agents.py index aadd1be40a..1d03dcfa36 100644 --- a/python/packages/core/agent_framework/_agents.py +++ b/python/packages/core/agent_framework/_agents.py @@ -906,6 +906,7 @@ async def run( ) return AgentRunResponse( messages=response.messages, + conversation_id=response.conversation_id, response_id=response.response_id, created_at=response.created_at, usage_details=response.usage_details, @@ -1045,6 +1046,7 @@ async def run_stream( role=update.role, author_name=update.author_name, response_id=update.response_id, + conversation_id=update.conversation_id, message_id=update.message_id, created_at=update.created_at, additional_properties=update.additional_properties, diff --git a/python/packages/core/agent_framework/_types.py b/python/packages/core/agent_framework/_types.py index 637794b1ca..84afb39026 100644 --- a/python/packages/core/agent_framework/_types.py +++ b/python/packages/core/agent_framework/_types.py @@ -2795,6 +2795,7 @@ def __init__( | MutableMapping[str, Any] | list[MutableMapping[str, Any]] | None = None, + conversation_id: str | None = None, response_id: str | None = None, created_at: CreatedAtT | None = None, usage_details: UsageDetails | MutableMapping[str, Any] | None = None, @@ -2807,6 +2808,7 @@ def __init__( Keyword Args: messages: The list of chat messages in the response. + conversation_id: The ID of the current conversation. response_id: The ID of the chat response. created_at: A timestamp for the chat response. usage_details: The usage details for the chat response. @@ -2835,6 +2837,7 @@ def __init__( usage_details = UsageDetails.from_dict(usage_details) self.messages = processed_messages + self.conversation_id = conversation_id self.response_id = response_id self.created_at = created_at self.usage_details = usage_details @@ -2961,6 +2964,7 @@ def __init__( text: TextContent | str | None = None, role: Role | MutableMapping[str, Any] | str | None = None, author_name: str | None = None, + conversation_id: str | None = None, response_id: str | None = None, message_id: str | None = None, created_at: CreatedAtT | None = None, @@ -2975,6 +2979,7 @@ def __init__( text: Optional text content of the update. role: The role of the author of the response update (Role, string, or dict author_name: Optional name of the author of the response update. + conversation_id: Optional identifier for the conversation of which this update is a part. response_id: Optional ID of the response of which this update is a part. message_id: Optional ID of the message of which this update is a part. created_at: Optional timestamp for the chat response update. @@ -2999,6 +3004,7 @@ def __init__( self.contents = parsed_contents self.role = role self.author_name = author_name + self.conversation_id = conversation_id self.response_id = response_id self.message_id = message_id self.created_at = created_at diff --git a/python/packages/core/tests/core/test_agents.py b/python/packages/core/tests/core/test_agents.py index a6df07cbbe..419a2e1749 100644 --- a/python/packages/core/tests/core/test_agents.py +++ b/python/packages/core/tests/core/test_agents.py @@ -19,6 +19,7 @@ ChatMessage, ChatMessageStore, ChatResponse, + ChatResponseUpdate, Context, ContextProvider, FunctionCallContent, @@ -343,6 +344,37 @@ async def test_chat_agent_run_stream_context_providers(chat_client: ChatClientPr assert mock_provider.invoked_called +async def test_chat_agent_run_stream_context_providers_with_conversation_id(chat_client: ChatClientProtocol) -> None: + """Test that conversation_id from streaming responses is propagated to the thread's service_thread_id.""" + mock_provider = MockContextProvider(messages=[ChatMessage(role=Role.SYSTEM, text="Stream context instructions")]) + mock_response_updates = [ + ChatResponseUpdate( + messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent("test response")])], + conversation_id="conv-123", + ), + ChatResponseUpdate( + messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent(" another update")])], + conversation_id="conv-123", + ), + ChatResponseUpdate( + messages=[ChatMessage(role=Role.ASSISTANT, contents=[TextContent(" final update")])], + conversation_id="conv-123", + ), + ] + chat_client.streaming_responses = [mock_response_updates] + agent = ChatAgent(chat_client=chat_client, context_providers=mock_provider) + + # Collect all stream updates + updates: list[AgentRunResponseUpdate] = [] + thread = agent.get_new_thread() + async for update in agent.run_stream("Hello", thread=thread): + updates.append(update) + + # Verify conversation_id is propagated to thread + assert all(update.conversation_id == "conv-123" for update in updates) + assert thread.service_thread_id == "conv-123" + + async def test_chat_agent_multiple_context_providers(chat_client: ChatClientProtocol) -> None: """Test that multiple context providers work together.""" provider1 = MockContextProvider(messages=[ChatMessage(role=Role.SYSTEM, text="First provider instructions")]) diff --git a/python/packages/core/tests/core/test_types.py b/python/packages/core/tests/core/test_types.py index 81242147d2..c3bbd0297b 100644 --- a/python/packages/core/tests/core/test_types.py +++ b/python/packages/core/tests/core/test_types.py @@ -1814,6 +1814,7 @@ def test_agent_run_response_update_all_content_types(): "contents": [{"type": "text", "text": "Answer"}], }, ], + "conversation_id": "conv-123", "response_id": "run-123", "usage_details": { "type": "usage_details", @@ -1833,6 +1834,7 @@ def test_agent_run_response_update_all_content_types(): ], "role": {"type": "role", "value": "assistant"}, "message_id": "msg-123", + "conversation_id": "conv-123", "response_id": "run-123", "author_name": "Agent", },