Python: Fix AG-UI EventEncoder crash on non-BaseEvent objects#4957
Open
LEDazzio01 wants to merge 2 commits intomicrosoft:mainfrom
Open
Python: Fix AG-UI EventEncoder crash on non-BaseEvent objects#4957LEDazzio01 wants to merge 2 commits intomicrosoft:mainfrom
LEDazzio01 wants to merge 2 commits intomicrosoft:mainfrom
Conversation
EventEncoder.encode() calls model_dump_json() internally, which fails with AttributeError when non-Pydantic objects (like AgentResponseUpdate) leak through the AG-UI event pipeline. This adds a defensive isinstance(event, BaseEvent) check in the SSE event_generator() so that non-BaseEvent objects are skipped with a warning instead of crashing the stream. Fixes microsoft#4929
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an AG-UI FastAPI SSE streaming failure mode where non-ag_ui.core.BaseEvent objects (e.g., AgentResponseUpdate) reaching the encoder could break/terminate the event stream.
Changes:
- Add a defensive
BaseEventtype guard in the FastAPI endpoint event loop to skip non-BaseEventobjects with a warning. - Add a regression test ensuring mixed valid events + leaked
AgentResponseUpdatedoes not result in aRUN_ERRORevent.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/ag-ui/agent_framework_ag_ui/_endpoint.py |
Skips non-BaseEvent objects before calling EventEncoder.encode() to keep SSE streaming resilient. |
python/packages/ag-ui/tests/ag_ui/test_endpoint.py |
Adds regression test covering a leaked non-BaseEvent object in the stream. |
…arning" Address Copilot review comment — the endpoint implementation logs a warning when skipping non-BaseEvent objects, so the test docstring and inline comments should reflect that behavior accurately.
Contributor
Author
|
Addressed Copilot's feedback in
This now accurately reflects the endpoint behavior (warning is logged when non- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4929 —
EventEncoder._encode_ssecrashes withAttributeErrorwhen a non-Pydantic object (likeAgentResponseUpdate) reaches the SSE encoder.Root Cause
EventEncoder.encode()(fromag_ui.encoder) internally callsmodel_dump_json(), which only works on PydanticBaseModelinstances.AgentResponseUpdateextendsSerializationMixin(not Pydantic) and usesto_json()instead. When anAgentResponseUpdateleaks through the AG-UI event pipeline, the encoder crashes:Fix
Added a defensive
isinstance(event, BaseEvent)guard in_endpoint.py'sevent_generator()before passing events toencoder.encode(). Non-BaseEventobjects are now skipped with a warning instead of crashing the SSE stream.Changes
_endpoint.py: ImportBaseEventfromag_ui.coreand add type check before encodingtest_endpoint.py: Addedtest_endpoint_skips_non_base_event_objectsregression test that yields a mix ofBaseEventandAgentResponseUpdateobjects and verifies the stream completes without errorTesting
The new test creates a workflow that yields both a
RunStartedEvent(valid) and anAgentResponseUpdate(invalid for SSE). It verifies:RUN_STARTEDis present in the outputRUN_ERRORis emitted (the invalid object is silently skipped)