Skip to content

Conversation

ihower
Copy link
Contributor

@ihower ihower commented Oct 7, 2025

Issues

This PR fixes incorrect streaming order for ReasoningItem and RawResponsesStreamEvent:

  1. RawResponsesStreamEvent should be emitted ASAP.

    This behavior was accidentally broken in Fix: Emit tool_called events immediately in streaming runs #1300, which changed the original correct ordering.

  2. ReasoningItem should be emitted right after its response.output_item.done event, rather than being delayed until after response.completed. This issue was reported in ReasoningItem of RunStreamEvents are getting emitted out of order #1767 (comment)

Solution

  1. Move raw event emission earlier

    Restored the correct ordering by moving RawResponsesStreamEvent to the top of the handler.

  2. Emit ReasoningItem earlier

    Applied the exact same approach as in PR Fix: Emit tool_called events immediately in streaming runs #1300 for early emission of ToolCallItem, also applying it to ensure that ReasoningItem is emitted right after its ResponseOutputItemDoneEvent.

    Code intentionally not refactored for DRYness to keep the diff explicit and review-friendly. Refactoring can be done later after test code are merged.

  3. Add comprehensive unit tests

    Extended FakeModel to fully simulate detailed streaming sequence and added tests verifying the exact event order, ensuring future changes won’t reintroduce regressions.

    For reference, here is a real example script that calls the OpenAI API, along with the corresponding 27 test events: https://gist.github.com/ihower/b6d9fe9b72b02c5131e210e2202b31b7

Related

@seratch seratch added bug Something isn't working feature:core labels Oct 8, 2025
@seratch seratch merged commit 9078e29 into openai:main Oct 8, 2025
5 checks passed
@waziers
Copy link

waziers commented Oct 8, 2025

Thanks for fixing @seratch!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReasoningItem of RunStreamEvents are getting emitted out of order Ordering of events in Runner.run_streamed is incorrect
3 participants