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

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stanoswald opened this issue Jun 15, 2025 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@stanoswald
Copy link

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)
@stanoswald stanoswald added the bug Something isn't working label Jun 15, 2025
@stanoswald stanoswald changed the title TypeError when handling streamed final usage chunk with choices: [ ] TypeError when handling streamed final usage chunk with choices: [] Jun 15, 2025
@actuallyzefe
Copy link

actuallyzefe commented Jun 15, 2025

Hi @stanoswald ! I took a look at the code, and I believe this case is actually handled safely in the current implementation:

  1. The code uses optional chaining to handle empty choices:
if (!chunk.choices?.[0]?.delta) continue;

This means when choices is an empty array ([]), no TypeError should occur - it will just continue to the next iteration.

Are you still experiencing the TypeError in your implementation? If so, could you share a minimal reproduction of the error? That would help identify if there might be a different issue at play.

@stanoswald
Copy link
Author

Hi @stanoswald ! I took a look at the code, and I believe this case is actually handled safely in the current implementation:

  1. The code uses optional chaining to handle empty choices:

if (!chunk.choices?.[0]?.delta) continue;
This means when choices is an empty array ([]), no TypeError should occur - it will just continue to the next iteration.

Are you still experiencing the TypeError in your implementation? If so, could you share a minimal reproduction of the error? That would help identify if there might be a different issue at play.

@actuallyzefe Hey there! Thanks for the quick reply! You're absolutely right - I took another look at the code and I totally missed the [0]? part. That optional chaining should handle the usage chunk parsing safely.
But yeah, in my scenario, this issue only seems to pop up in streaming mode. I'm gonna close this issue for now since both the description and title are kinda off.
I'll dig deeper into my use case to figure out what's actually going on, and if I find other issues, I'll open separate ones.

Thanks again for helping me spot that! 🙏

@stanoswald
Copy link
Author

I was checking the compiled .mjs output locally and noticed it didn’t quite match what I was seeing in the codebase. After a bit of digging with git blame, turns out this has already been addressed in #92 #93 and should be fixed in the next release. All good now!

Sorry for the extra noise—I totally missed the closed issue and PR. @actuallyzefe

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

No branches or pull requests

2 participants