Skip to content

Conversation

@thejens
Copy link
Contributor

@thejens thejens commented Dec 16, 2025

Addresses #3734

All AG-UI events now include timestamps to help frontends order events and show when things actually happened.

Changes

  • Added handle_event() override that sets timestamps on all transformed events if not already set
  • Explicitly set timestamps on lifecycle events (RunStarted, RunFinished, RunError)
  • Updated all test assertions to expect timestamp fields
  • Added test_timestamps_are_set() to verify the behavior

Timestamps are in milliseconds (JS format) using now_utc().timestamp() * 1000.

The two-layer approach (explicit + override) ensures no events slip through without timestamps while avoiding double-setting.

@DouweM DouweM self-assigned this Dec 16, 2025
@thejens thejens requested a review from DouweM December 16, 2025 21:40
@DouweM DouweM changed the title Set timestamps on AG-UI events (#3734) Set timestamps on AG-UI events Dec 16, 2025
async def handle_event(self, event: NativeEvent) -> AsyncIterator[BaseEvent]:
"""Override to set timestamps on all AG-UI events."""
async for agui_event in super().handle_event(event):
if agui_event.timestamp is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing coverage to fail because the timestamp is always none :) I think we can drop the if here

Copy link
Contributor

@jens-gilion jens-gilion Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) well it's always none in the current tests -- but I guess I'm not fluent enough in the ag-ui standard to understand if there are scenarios where events come on the stream from sources where the timestamp might be set -- I know it has quite some flexibility with custom events

Your call, I can remove the if (will solve my need) or furnish a test that streams an event with a pre-existing timestamp set

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I suppose since we let the user stream their own AG-UI events from the on_complete handler, it's better to not overwrite that here. If you can add a test that'd be great!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test (sorry if me using two different github accounts (albeit w similar names) cause confusion in the comments)

Adds timestamps to all AG-UI events to fix ordering and timing issues for frontends consuming the event stream. - Override handle_event() to set timestamps on all transformed events - Explicitly set timestamps on lifecycle events (RunStarted, RunFinished, RunError) - Add test_timestamps_are_set() to verify all events have valid timestamps - Update all test assertions to expect timestamp fields
@thejens thejens requested a review from DouweM December 17, 2025 16:04
@DouweM DouweM merged commit 8576d36 into pydantic:main Dec 17, 2025
32 checks passed
@DouweM
Copy link
Collaborator

DouweM commented Dec 17, 2025

@thejens Thanks Jens!

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