From 3b3da793daebd6e52e3b3122fe2f018b26b818b4 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 27 May 2026 00:48:23 +0000 Subject: [PATCH] fix(responses): assign monotonic sequence_number to all synthetic events Several Responses API event types now declare sequence_number with a default of 0. The native synthetic streaming path constructed many events without explicitly passing sequence_number, which caused them to serialize as 0 and break the strict monotonic ordering guarantee expected by some clients (e.g. Grok Build CLI). Assign sequence_number to every event after the list is built so the stream is guaranteed monotonic regardless of which helper produced each event. Co-authored-by: Yassin Kortam --- litellm/responses/streaming_iterator.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index c4e72cb7dc5..a7eecf53dfe 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -1212,6 +1212,18 @@ def _build_synthetic_response_events( response=transformed, ) ) + + # Assign monotonic sequence_number to every event. The helpers above + # build events without consistently passing sequence_number, and several + # event types now declare `sequence_number: int = 0` (default), which + # would otherwise serialize as 0 for most events and break the strict + # monotonic ordering guarantee expected by some Responses API clients. + for idx, event in enumerate(events): + try: + event.sequence_number = idx + except (AttributeError, ValueError): + pass + return events