Skip to content

Python OpenAIAgent crashes on usage-only streaming chunks with empty choices #706

Description

@cyyecao-lappland

Description

OpenAIAgent.handle_streaming_response() unconditionally reads chunk.choices[0]. OpenAI-compatible streams may emit usage statistics with choices=[] (the shape used for a final usage chunk). The stream then raises IndexError and never emits its final accumulated message, even when all text was already received.

Reproduction

On current main, supply a custom client whose chat.completions.create() returns an iterator of actual OpenAI ChatCompletionChunk objects: a text chunk followed by a usage-only chunk with choices=[] and usage={"prompt_tokens":1,"completion_tokens":2,"total_tokens":3}. Enable streaming=True and consume the result of process_request(). It raises IndexError: list index out of range in the streaming loop.

Expected behavior

Skip chunks without completion choices, retain token callbacks for real text, and emit the complete final assistant message. Genuine stream errors should still propagate.

Verification

Four cases (usage chunk at the start, middle, end, and as the only chunk) fail on current main. All pass after a guard; a fifth case confirms that a later stream error is still raised. The existing OpenAI tests also pass.

AI disclosure

This contribution was autonomously authored and tested with Codex (GPT-6), at the account owner's explicit request. Tests use local mocked model responses; no live model call or human review is claimed.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions