Skip to content

Conversation

@howlowck
Copy link
Member

@howlowck howlowck commented Jan 8, 2026

Motivation and Context

Currently, when using AG-UI with a service-managed thread (like, Foundry Agent), every message is seen as a new conversation because the service_thread_id is not being recognized.

There are a few implementation issues with the current AG-UI integration in Agent Framework when it comes to service-managed threads:

  • Issue 1: a new AgentThread is created without a service_thread_id on every request/process, disregarding what's in the ExecutionContext.
  • Issue 2: the ExecutionContext cannot distringuish if the thread id is supplied by the user/system or randomly generated, so it cannot reliably set the service_thread_id value to the AgentThread (setting an id that's auto-generated as the service_thread_id will result in an exception).
  • Issue 3: the first AG-UI event (RunStartEvent) is sent back to the user at the start of the process before any real response is received from the agent backend, so a conversation will get a random GUID and not the real thread/conversation id.
  • Issue 4: the AgentFrameworkEventBridge, the mechanism that translates MAF updates to AG-UI events, is also created at the beginning of the process. The thread_id and run_id is set during instantiation, so the ids cannot (and should not) be changed after the event bridge is created.

Description

Changes:

  • Adds supplied_thread_id and supplied_run_id in ExecutionContext. This helps resolve Issue 2 as it helps distinguish if the id is auto-generated or supplied by an external source (user or system).
  • Adds an optional use_service_thread argument to AG-UI AgentFrameworkAgent. This helps resolve Issue 1. Only if this option is set, is when AgentThread is created with service_thread_id=context.supplied_thread_id.
  • Delays the first AG-UI event to after it gets the first update from the backend. If it gets ids different than what's in the context, it updates the context and recreates the event bridge before sending the AG-UI event back to the user. This change also redistributes "the first AG-UI event creation" to all exit conditions before the first agent backend update. This helps resolve Issue 3 and Issue 4.

See below for a visual representation of the changes (left side is main, right side is this PR):
(Scenario: New conversation)
image

(Scenario: Continuing the conversation)
image

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? No, it's not a breaking change. All new arguments are all optional

@github-actions github-actions bot changed the title (AG-UI) Support service-managed thread on AG-UI Python: (AG-UI) Support service-managed thread on AG-UI Jan 8, 2026
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Jan 8, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/ag-ui/agent_framework_ag_ui
   _agent.py51394%68, 127, 187
   _orchestrators.py4114888%113, 122, 229, 480, 535–537, 546–547, 555, 557–559, 563, 572, 605, 607–609, 611–614, 616, 618–619, 621, 623–630, 730–732, 736–741, 790–792, 795
   _utils.py99792%70, 73, 86, 109, 155, 254, 259
TOTAL17678288883% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2619 154 💤 0 ❌ 0 🔥 1m 2s ⏱️

@howlowck howlowck marked this pull request as ready for review January 8, 2026 09:00
Copilot AI review requested due to automatic review settings January 8, 2026 09:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for service-managed threads in AG-UI to properly handle scenarios where backend services (like Foundry Agent) manage thread IDs. The changes address issues where conversations weren't being properly continued because service thread IDs weren't recognized.

Key changes:

  • Adds supplied_thread_id and supplied_run_id properties to ExecutionContext to distinguish user/system-provided IDs from auto-generated ones
  • Introduces use_service_thread parameter to AgentFrameworkAgent and AgentConfig to enable service-managed thread creation
  • Delays emission of initial AG-UI events until after receiving the first backend update, allowing dynamic ID updates from service responses

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
python/packages/ag-ui/tests/test_service_thread_id.py New test file for service thread ID tracking, verifying that conversation IDs from service responses are properly reflected in events
python/packages/ag-ui/tests/test_orchestrators.py Enhanced existing tests with proper type hints and assertions for event types; updated DummyAgent to return proper conversation/response IDs
python/packages/ag-ui/agent_framework_ag_ui/_utils.py Added utility function to extract conversation IDs from AgentRunResponseUpdate objects
python/packages/ag-ui/agent_framework_ag_ui/_orchestrators.py Core changes: added supplied ID tracking, ID update methods, event bridge recreation logic, and delayed initial event emission
python/packages/ag-ui/agent_framework_ag_ui/_agent.py Added use_service_thread parameter to configuration classes with proper documentation

@howlowck howlowck force-pushed the feature/ag-ui-proper-conversation-id-run-id branch from 84f26bf to 11761fe Compare January 8, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants