Skip to content

Use class hierarchy to organize AgentChat message types and introduce StructuredMessage type #5998

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 32 commits into from
Mar 26, 2025

Conversation

ekzhu
Copy link
Collaborator

@ekzhu ekzhu commented Mar 19, 2025

This PR refactored AgentEvent and ChatMessage union types to abstract base classes. This allows for user-defined message types that subclass one of the base classes to be used in AgentChat.

To support a unified interface for working with the messages, the base classes added abstract methods for:

  • Convert content to string
  • Convert content to a UserMessage for model client
  • Convert content for rendering in console.
  • Dump into a dictionary
  • Load and create a new instance from a dictionary

This way, all agents such as AssistantAgent and SocietyOfMindAgent can utilize the unified interface to work with any built-in and user-defined message type.

This PR also introduces a new message type, StructuredMessage for AgentChat (Resolves #5131), which is a generic type that requires a user-specified content type.

You can create a StructuredMessage as follow:

class MessageType(BaseModel):
  data: str
  references: List[str]

message = StructuredMessage[MessageType](content=MessageType(data="data", references=["a", "b"]), source="user")

# message.content is of type `MessageType`. 

This PR addresses the receving side of this message type. To produce this message type from AssistantAgent, the work continue in #5934.

Added unit tests to verify this message type works with agents and teams.

Copy link

codecov bot commented Mar 19, 2025

Codecov Report

Attention: Patch coverage is 84.23237% with 38 lines in your changes missing coverage. Please review.

Project coverage is 76.89%. Comparing base (8a5ee3d) to head (6a1d51c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...utogen-agentchat/src/autogen_agentchat/messages.py 86.13% 19 Missing ⚠️
...togen_ext/agents/openai/_openai_assistant_agent.py 63.15% 7 Missing ⚠️
...gen-agentchat/src/autogen_agentchat/ui/_console.py 33.33% 6 Missing ⚠️
...entchat/teams/_group_chat/_chat_agent_container.py 91.30% 2 Missing ⚠️
...en_agentchat/teams/_group_chat/_base_group_chat.py 92.30% 1 Missing ⚠️
...en-agentchat/src/autogen_agentchat/utils/_utils.py 80.00% 1 Missing ⚠️
...perimental/task_centric_memory/utils/apprentice.py 50.00% 1 Missing ⚠️
...es/autogen-ext/src/autogen_ext/ui/_rich_console.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5998      +/-   ##
==========================================
+ Coverage   76.78%   76.89%   +0.11%     
==========================================
  Files         191      191              
  Lines       13252    13358     +106     
==========================================
+ Hits        10175    10272      +97     
- Misses       3077     3086       +9     
Flag Coverage Δ
unittests 76.89% <84.23%> (+0.11%) ⬆️

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.

@ekzhu
Copy link
Collaborator Author

ekzhu commented Mar 22, 2025

Pausing the work here. Let's wait for more clarity from #4623 and consider structured message as part of that later.

First let's just focus on #5934 and use TextMessage with JSON string as the content.

@ekzhu ekzhu marked this pull request as draft March 22, 2025 22:48
@ekzhu ekzhu changed the title Introduce StructuredMessage type into ChatMessage Use class hierarchy to organize AgentChat message types and introduce StructuredMessage type Mar 24, 2025
@ekzhu ekzhu marked this pull request as ready for review March 24, 2025 15:59
@ekzhu ekzhu linked an issue Mar 24, 2025 that may be closed by this pull request
@afourney
Copy link
Member

In generally I like this PR.

I worry a little about the really long method names like content_to_model_message... but at least it's clear.

I'm also not sure about content_to_render. Doesn't this really depend on the media type or shell environment? E.g., I got hit on markitdown when people's shells were set to the charset gbk. Is there a better name for this?

@ekzhu
Copy link
Collaborator Author

ekzhu commented Mar 25, 2025

I'm also not sure about content_to_render. Doesn't this really depend on the media type or shell environment? E.g., I got hit on markitdown when people's shells were set to the charset gbk. Is there a better name for this?

I changed it to content_to_text.... perhaps there can be some decoding parameters?

@ekzhu
Copy link
Collaborator Author

ekzhu commented Mar 25, 2025

@jackgerrits @victordibia @afourney I made the change we have discussed so the type hints won't change, just replaced union with base class.

@jackgerrits jackgerrits dismissed their stale review March 25, 2025 20:02

misclick

@ekzhu ekzhu enabled auto-merge (squash) March 26, 2025 23:02
@ekzhu ekzhu merged commit 025490a into main Mar 26, 2025
57 checks passed
@ekzhu ekzhu deleted the ekzhu-structured-message branch March 26, 2025 23:19
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.

When Call MultimodalWebSurfer after using tool, error occurred Structured message type in AgentChat
5 participants