Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TypeError when handling streamed final usage chunk with choices: [] #107

Closed
@stanoswald

Description

@stanoswald

Please read this first

  • Have you read the docs?
  • Have you searched for related issues?

Describe the bug

When using an OpenAI-API compatible provider , the streamed response may include a final chunk with choices: []. The convertChatCompletionsStreamToResponses function in openaiChatCompletionsStreaming.ts assumes chunk.choices[0] exists, and throws:

TypeError: Cannot read properties of undefined (reading 'delta')

This is due to this line:

if (!chunk.choices?.[0]?.delta) continue;

This line assumes chunk.choices is non-empty, but according to the OpenAI Chat Completions Streaming API documentation:

choices: Can also be empty for the last chunk if you set stream_options: {"include_usage": true}.

So choices: [] seems a valid case, and should be handled gracefully by the SDK for compatibility with OpenAI-compatible providers.

Debug information

  • Agents SDK version: (e.g. v0.0.7)
  • Runtime environment (e.g. Node.js 22.11.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions