Skip to content

Python: Fix AG-UI EventEncoder crash on non-BaseEvent objects#4957

Open
LEDazzio01 wants to merge 2 commits intomicrosoft:mainfrom
LEDazzio01:fix/agui-event-encoder-serialization
Open

Python: Fix AG-UI EventEncoder crash on non-BaseEvent objects#4957
LEDazzio01 wants to merge 2 commits intomicrosoft:mainfrom
LEDazzio01:fix/agui-event-encoder-serialization

Conversation

@LEDazzio01
Copy link
Copy Markdown
Contributor

Summary

Fixes #4929EventEncoder._encode_sse crashes with AttributeError when a non-Pydantic object (like AgentResponseUpdate) reaches the SSE encoder.

Root Cause

EventEncoder.encode() (from ag_ui.encoder) internally calls model_dump_json(), which only works on Pydantic BaseModel instances. AgentResponseUpdate extends SerializationMixin (not Pydantic) and uses to_json() instead. When an AgentResponseUpdate leaks through the AG-UI event pipeline, the encoder crashes:

AttributeError: 'AgentResponseUpdate' object has no attribute 'model_dump_json'

Fix

Added a defensive isinstance(event, BaseEvent) guard in _endpoint.py's event_generator() before passing events to encoder.encode(). Non-BaseEvent objects are now skipped with a warning instead of crashing the SSE stream.

Changes

  • _endpoint.py: Import BaseEvent from ag_ui.core and add type check before encoding
  • test_endpoint.py: Added test_endpoint_skips_non_base_event_objects regression test that yields a mix of BaseEvent and AgentResponseUpdate objects and verifies the stream completes without error

Testing

The new test creates a workflow that yields both a RunStartedEvent (valid) and an AgentResponseUpdate (invalid for SSE). It verifies:

  • The stream returns 200
  • RUN_STARTED is present in the output
  • No RUN_ERROR is emitted (the invalid object is silently skipped)

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
Copilot AI review requested due to automatic review settings March 28, 2026 01:25
Copy link
Copy Markdown
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

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 BaseEvent type guard in the FastAPI endpoint event loop to skip non-BaseEvent objects with a warning.
  • Add a regression test ensuring mixed valid events + leaked AgentResponseUpdate does not result in a RUN_ERROR event.

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.
@LEDazzio01
Copy link
Copy Markdown
Contributor Author

Addressed Copilot's feedback in e79c175:

  • Updated the test docstring from "are skipped gracefully" to "are skipped with a warning"
  • Updated the inline comment from "this should be skipped, not crash" to "this should be skipped with a warning, not crash"
  • Updated the trailing assertion comment from "silently skipped" to "skipped with a warning"

This now accurately reflects the endpoint behavior (warning is logged when non-BaseEvent objects are encountered).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: AG-UI EventEncoder._encode_sse cannot handle AgentResponseUpdate

3 participants