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.
Description
OpenAIAgent.handle_streaming_response()unconditionally readschunk.choices[0]. OpenAI-compatible streams may emit usage statistics withchoices=[](the shape used for a final usage chunk). The stream then raisesIndexErrorand 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 OpenAIChatCompletionChunkobjects: a text chunk followed by a usage-only chunk withchoices=[]andusage={"prompt_tokens":1,"completion_tokens":2,"total_tokens":3}. Enablestreaming=Trueand consume the result ofprocess_request(). It raisesIndexError: list index out of rangein 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.