Skip to content

fix(models): preserve streamed content part order#3822

Open
felmonon wants to merge 1 commit into
openai:mainfrom
felmonon:fix/chatcmpl-content-part-order
Open

fix(models): preserve streamed content part order#3822
felmonon wants to merge 1 commit into
openai:mainfrom
felmonon:fix/chatcmpl-content-part-order

Conversation

@felmonon

Copy link
Copy Markdown
Contributor

Summary

When a Chat Completions stream emits a refusal before text, the raw stream assigns content_index=0 to the refusal and content_index=1 to the text, but response.completed always assembled text before refusal. Consumers replaying raw events therefore saw a different content layout from the completed response.

This PR assembles the completed assistant message using the content indexes already assigned during streaming. Text-before-refusal behavior stays unchanged, while refusal-before-text now matches the raw event sequence. This is a focused follow-up to the pre-existing mismatch discussed in #3757 and follows the raw-event/final-response alignment required during #3769.

Test plan

  • The updated regression test fails on origin/main and passes with this change.
  • uv run pytest -q tests/models/test_openai_chatcompletions_stream.py — 57 passed.
  • bash .agents/skills/code-change-verification/scripts/run.sh — format, lint, and the full test suite passed.
  • Targeted mypy and pyright checks pass for both changed files.
  • The repository-wide local typecheck still reports six errors in unchanged current-main files (archive_ops.py, test_any_llm_model.py, and test_run_step_execution.py).

Issue number

Follow-up to #3757; related maintainer direction in #3769.

Checks

  • I added regression coverage.
  • I ran the full impacted test slice.
  • I ran the repository verification script and documented its unrelated typecheck limitation.

@felmonon felmonon marked this pull request as ready for review July 13, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants