Skip to content

Python: fix(ag-ui): properly handle json serialize with handoff workflows as agent#3275

Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3:3239-fix
Jan 21, 2026
Merged

Python: fix(ag-ui): properly handle json serialize with handoff workflows as agent#3275
moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3:3239-fix

Conversation

@moonbox3
Copy link
Contributor

Motivation and Context

When using AG-UI with a Handoff workflow (as an agent), the server crashes with TypeError: Object of type HandoffAgentUserRequest is not JSON serializable the first time a handoff agent requests user input. This occurs because HandoffAgentUserRequest is a dataclass containing an AgentResponse (which uses SerializationMixin), and the AG-UI event bridge calls json.dumps() directly on these objects without converting them to serializable dictionaries first.

The fix updates make _json_safe() in _utils.py to handle to_dict() methods (used by SerializationMixin) and to recursively process results from as_dict(), model_dump(), to_dict(), and dict() calls to make sure nested non-serializable objects are also converted. The three locations in _events.py that serialize function call arguments now use make_json_safe() to properly handle dataclasses and SerializationMixin objects before JSON encoding.

Description

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? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings January 19, 2026 03:42
@moonbox3 moonbox3 self-assigned this Jan 19, 2026
@moonbox3 moonbox3 added python workflows Related to Workflows in agent-framework ag-ui labels Jan 19, 2026
@moonbox3 moonbox3 moved this to In Review in Agent Framework Jan 19, 2026
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Jan 19, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/ag-ui/agent_framework_ag_ui
   _events.py2641395%105–106, 200, 219, 248, 262, 285, 365–366, 395, 406, 429, 485
   _message_adapters.py4524490%66, 76–77, 86–89, 155, 255, 259, 262, 265, 269, 285, 302, 324, 360, 372, 425, 442–443, 506, 510, 516, 524–525, 527, 531–534, 547, 636–639, 641, 706, 763, 767, 770, 773, 781–782
   _orchestrators.py4104888%111, 120, 227, 477, 532–534, 541–542, 550, 552–554, 558, 567, 600, 602–604, 606–609, 611, 613–614, 616, 618–625, 725–727, 731–736, 785–787, 790
   _utils.py101793%70, 73, 86, 109, 158, 257, 262
packages/ag-ui/agent_framework_ag_ui/_orchestration
   _helpers.py1845371%57, 106, 109, 146, 149, 153, 171, 174, 178, 195, 198, 201, 221, 233, 237, 252, 257, 288–305, 307–310, 314–315, 329, 348–350, 357, 377, 379, 382–386
   _state_manager.py51198%97
TOTAL17429270684% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3180 213 💤 0 ❌ 0 🔥 1m 3s ⏱️

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 fixes a JSON serialization crash in AG-UI when using handoff workflows as agents. The server previously crashed with TypeError: Object of type HandoffAgentUserRequest is not JSON serializable when attempting to serialize dataclass objects containing nested SerializationMixin objects (which use to_dict() methods).

Changes:

  • Enhanced make_json_safe() to support to_dict() method and recursively process all serialization method outputs
  • Updated three locations in _events.py to use make_json_safe() before JSON encoding function call arguments
  • Added comprehensive test coverage for dataclasses with nested objects and SerializationMixin patterns

Reviewed changes

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

File Description
python/packages/ag-ui/agent_framework_ag_ui/_utils.py Added support for to_dict() method in make_json_safe() and made all serialization methods recursive
python/packages/ag-ui/agent_framework_ag_ui/_events.py Applied make_json_safe() to function call arguments in three locations before JSON encoding
python/packages/ag-ui/tests/test_utils.py Added unit tests for to_dict() method handling and nested dataclass serialization
python/packages/ag-ui/tests/test_events_comprehensive.py Added integration tests for dataclass and nested dataclass arguments in function calls

@moonbox3 moonbox3 enabled auto-merge January 19, 2026 04:04
@moonbox3 moonbox3 added this pull request to the merge queue Jan 21, 2026
Merged via the queue into microsoft:main with commit 6d7690e Jan 21, 2026
23 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in Agent Framework Jan 21, 2026
arisng pushed a commit to arisng/agent-framework that referenced this pull request Feb 2, 2026
…lows as agent (microsoft#3275)

* fix(ag-ui): properly handle json serialize with handoff workflows as agent

* Other improvements around handling non-serializable objects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ag-ui python workflows Related to Workflows in agent-framework

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: agent-framework-ag-ui crashes when Handoff user-input requests emit non‑JSON‑serializable arguments

5 participants