Skip to content

feat(agentchat): add get_thread() method to BaseGroupChat#7320

Open
ayzmkk-rongbi wants to merge 1 commit into
microsoft:mainfrom
ayzmkk-rongbi:feat/get-thread-group-chat
Open

feat(agentchat): add get_thread() method to BaseGroupChat#7320
ayzmkk-rongbi wants to merge 1 commit into
microsoft:mainfrom
ayzmkk-rongbi:feat/get-thread-group-chat

Conversation

@ayzmkk-rongbi

Copy link
Copy Markdown

Why are these changes needed?

Currently, the message thread is stored internally in BaseGroupChatManager._message_thread but is not accessible from the public API. Users need a way to:

  • Inspect conversation progress during or after a group chat run
  • Debug multi-agent interactions by examining message history
  • Display chat history in UIs or monitoring dashboards
  • Access the full message thread without relying on run_stream()

Related Issue(s)

Changes

_events.py: Added GroupChatGetThread RPC request event and GroupChatGetThreadResponse response model containing the serialized message list.

_base_group_chat_manager.py:

  • Imported new event types
  • Added handle_get_thread() RPC handler that returns a copy of self._message_thread

_base_group_chat.py:

  • Imported new event types
  • Added public async def get_thread() method with full documentation and usage example
  • Handles both embedded and external runtime modes (starts/stops embedded runtime when team is not running)
  • Validates team initialization before sending RPC

test_group_chat_get_thread.py: Added three test cases covering:

  • test_get_thread_after_run: Verifies thread contains correct messages after run()
  • test_get_thread_empty_after_reset: Verifies thread is empty after reset()
  • test_get_thread_not_initialized: Verifies RuntimeError when team not initialized

All tests run with both single_threaded and embedded runtime modes (6 tests total, all passing).

Implementation Details

The implementation follows the existing RPC communication pattern used by reset(), pause(), and resume():

  1. BaseGroupChat.get_thread() sends a GroupChatGetThread RPC message to the manager
  2. BaseGroupChatManager.handle_get_thread() returns GroupChatGetThreadResponse with the current _message_thread
  3. The response is deserialized and returned as List[BaseAgentEvent | BaseChatMessage]

For embedded runtime support, get_thread() temporarily starts the runtime if the team is not currently running (same pattern as reset()).

Usage Example

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient

async def main():
    model_client = OpenAIChatCompletionClient(model="gpt-4o")
    agent1 = AssistantAgent("Assistant1", model_client=model_client)
    agent2 = AssistantAgent("Assistant2", model_client=model_client)
    termination = MaxMessageTermination(3)
    team = RoundRobinGroupChat([agent1, agent2], termination_condition=termination)

    result = await team.run(task="Count from 1 to 10, respond one at a time.")

    # Get message history (new feature!)
    thread = await team.get_thread()
    for msg in thread:
        print(f"{msg.source}: {msg.content}")

Add support for retrieving the current message thread from group chat
teams via a new get_thread() public method. This allows users to access
all messages exchanged between participants during or after conversation.

Changes:
- Add GroupChatGetThread RPC event and GroupChatGetThreadResponse in _events.py
- Add handle_get_thread RPC handler in BaseGroupChatManager
- Add get_thread() method in BaseGroupChat with embedded runtime support
- Add comprehensive tests for both single_threaded and embedded runtimes

Closes microsoft#6085

Signed-off-by: ayzmkk-rongbi <73167059+ayzmkk-rongbi@users.noreply.github.com>
@ayzmkk-rongbi

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Microsoft"

@codecov

codecov Bot commented Feb 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.20%. Comparing base (13e144e) to head (c28fd95).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7320      +/-   ##
==========================================
- Coverage   81.22%   81.20%   -0.02%     
==========================================
  Files         244        2     -242     
  Lines       18512      149   -18363     
==========================================
- Hits        15036      121   -14915     
+ Misses       3476       28    -3448     
Flag Coverage Δ
unittests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ayzmkk-rongbi

Copy link
Copy Markdown
Author

Hi @ekzhu, this PR implements the \get_thread()\ method for \BaseGroupChat\ as described in #6085. It follows the same RPC pattern used by
eset(), \pause(), and
esume(). All 6 new tests pass (3 test cases × 2 runtime modes). Would you be able to take a look when you have a chance? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get current message thread from a group chat team.

1 participant