Skip to content

SingleThreadedAgentRuntime to use subclass check for factory_wrapper instead of equality #6731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2025

Conversation

ZenWayne
Copy link
Contributor

@ZenWayne ZenWayne commented Jun 29, 2025

The agent factory in SingleThreadedAgentRuntime does not support customized class that inherited from ChatAgentContainer
Now change the judge expression to

if expected_class is not None and not isinstance(agent_instance, expected_class):
raise ValueError("Factory registered using the wrong type.")

instead of exactly match

Why are these changes needed?

To support Customized ChatAgentContainer that can ajust the logic after an event or a message published by ChatAgentContainer, which achieved by inheriting ChatAgentContainer

Related issue number

Resolves #6730

Checks

@ekzhu ekzhu requested a review from Copilot June 29, 2025 15:12
Copy link
Contributor

@Copilot 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 updates the agent factory in SingleThreadedAgentRuntime to accept subclasses of ChatAgentContainer by replacing a strict type equality check with an isinstance check.

  • Switched type_func_alias(agent_instance) != expected_class to not isinstance(agent_instance, expected_class)
  • Continues to raise a ValueError only when the instance is not an instance or subclass of the expected class
Comments suppressed due to low confidence (1)

python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py:906

  • Add a unit test that verifies a subclass of the expected class is accepted by this check and that non-matching types still trigger the error.
            if expected_class is not None and not isinstance(agent_instance, expected_class):

@ZenWayne
Copy link
Contributor Author

this is because participant_factory is hard coded in BaseGroupChat._create_participant_factory(_base_group_chat.py#L146) so that when it called by singlethread runtime, the agent factory requiement does not satified because it has to be match exactly as ChatAgentContainer
Related function call

  1. BaseGroupChat._create_participant_factory(_base_group_chat.py#L146)
  2. BaseGroupChat._init(_base_group_chat.py#L167)
  3. factory called(_single_threaded_agent_runtime.py#L906)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@ekzhu
Copy link
Collaborator

ekzhu commented Jul 7, 2025

@ZenWayne I made some changes please review.

@ekzhu ekzhu enabled auto-merge (squash) July 7, 2025 23:00
@ekzhu ekzhu disabled auto-merge July 7, 2025 23:00
@ekzhu ekzhu changed the title add customized ChatAgentContainer support SingleThreadedAgentRuntime to use subclass check for factory_wrapper instead of equality Jul 7, 2025
@ekzhu ekzhu merged commit 4ccac43 into microsoft:main Jul 7, 2025
63 checks passed
Copy link

codecov bot commented Jul 7, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 80.44%. Comparing base (02e6574) to head (caee8e1).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...src/autogen_core/_single_threaded_agent_runtime.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6731      +/-   ##
==========================================
+ Coverage   76.42%   80.44%   +4.01%     
==========================================
  Files         343      237     -106     
  Lines       20982    17864    -3118     
  Branches      406        0     -406     
==========================================
- Hits        16035    14370    -1665     
+ Misses       4674     3494    -1180     
+ Partials      273        0     -273     
Flag Coverage Δ
unittests 80.44% <50.00%> (+4.01%) ⬆️

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.

@ZenWayne
Copy link
Contributor Author

ZenWayne commented Jul 8, 2025

LGTM, Thanks for the merge!

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.

Customized ChatAgentContainer not support
2 participants