KEMBAR78
Fix structured logging serialization data loss with SerializeAsAny annotations by Copilot · Pull Request #6889 · microsoft/autogen · GitHub
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 3, 2025

Structured logging was losing critical data during JSON serialization when BaseModel classes contained fields typed as base classes (like BaseChatMessage) but holding subclass instances (like TextMessage).

Problem

When PydanticJsonMessageSerializer serialized messages, it used model_dump_json() without serialize_as_any=True, which only preserved fields defined in the base class schema. Subclass-specific fields like TextMessage.content and TextMessage.type were being lost:

# Before fix - data loss
text_msg = TextMessage(content="Hello, world!", source="Agent")
group_msg = GroupChatMessage(message=text_msg)
json_data = group_msg.model_dump_json()
# Result: {"message": {"id": "...", "source": "Agent", "created_at": "..."}}
# Missing: content and type fields

Solution

Added SerializeAsAny annotations to BaseModel fields containing BaseChatMessage or BaseAgentEvent types:

  • GroupChatStart.messages: List[SerializeAsAny[BaseChatMessage]]
  • GroupChatMessage.message: SerializeAsAny[BaseAgentEvent | BaseChatMessage]
  • TaskResult.messages: Sequence[SerializeAsAny[BaseAgentEvent | BaseChatMessage]]
  • GroupChatAgentResponse.response: SerializeAsAny[Response]
  • GroupChatTeamResponse.result: SerializeAsAny[TaskResult]

Result

Now all subclass data is preserved during serialization:

# After fix - complete data preservation
json_data = group_msg.model_dump_json()
# Result: {"message": {"id": "...", "source": "Agent", "created_at": "...", "content": "Hello, world!", "type": "TextMessage"}}
# All fields preserved!

This fix ensures structured logging captures complete message information for better observability and debugging, with no breaking changes to existing APIs.

Fixes #6855.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 3, 2025 00:42
Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
…tions

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI changed the title [WIP] Structured logging is missing data from Trace logging (0.6.4) Fix structured logging serialization data loss with SerializeAsAny annotations Aug 3, 2025
Copilot finished work on behalf of ekzhu August 3, 2025 01:01
Copilot AI requested a review from ekzhu August 3, 2025 01:01
@ekzhu ekzhu marked this pull request as ready for review August 3, 2025 04:06
@codecov
Copy link

codecov bot commented Aug 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.93%. Comparing base (9cb067e) to head (a3f1805).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6889   +/-   ##
=======================================
  Coverage   80.93%   80.93%           
=======================================
  Files         237      237           
  Lines       18291    18291           
=======================================
  Hits        14803    14803           
  Misses       3488     3488           
Flag Coverage Δ
unittests 80.93% <100.00%> (ø)

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 ekzhu merged commit ec5da1e into main Aug 5, 2025
73 checks passed
@ekzhu ekzhu deleted the copilot/fix-6855 branch August 5, 2025 16:32
@DKingAlpha
Copy link

DKingAlpha commented Sep 6, 2025

@ekzhu

pydantic (2.12.0a1) does not support ABCModel_1 | ABCModel_2

pydantic 2.11.7 works. I think it's a more like a bug in pydantic.

Minimal code to reproduce:

from autogen_agentchat.messages import ThoughtEvent, TextMessage
from autogen_agentchat.teams._group_chat._events import GroupChatMessage

# BaseAgentEvent
thought = ThoughtEvent(source='2', content='2')

# BaseChatMessage
text = TextMessage(source='1', content='1')


GroupChatMessage(message=thought)
GroupChatMessage(message=text)

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.

Structured logging is missing data from Trace logging (0.6.4)

3 participants