Skip to content

Fix crash when OpenRouter sends chunks with empty choices array#65

Open
rmichelena wants to merge 1 commit intomeltylabs:mainfrom
rmichelena:rmichelena/fix-openrouter-stream-parsing
Open

Fix crash when OpenRouter sends chunks with empty choices array#65
rmichelena wants to merge 1 commit intomeltylabs:mainfrom
rmichelena:rmichelena/fix-openrouter-stream-parsing

Conversation

@rmichelena
Copy link
Copy Markdown

Summary

  • OpenRouter (and OpenAI-compatible providers) can send SSE chunks where choices is an empty array (e.g. final usage/close chunks, as documented in OpenRouter streaming + OpenAI stream_options.include_usage).
  • OpenAICompletionsAPIUtils.ts was unconditionally accessing chunk.choices[0].delta.tool_calls, crashing with undefined is not an object on every OpenRouter streaming call that includes those chunks.
  • Fix: use optional chaining chunk.choices?.[0]?.delta?.tool_calls || [] so empty-choices chunks are silently skipped.

Test plan

  • Start a chat with an OpenRouter model that uses streaming — verify it completes without crashing
  • Start a chat that involves tool calls via OpenRouter — verify tool calls complete correctly
  • Verify non-OpenRouter providers (Anthropic, OpenAI direct) still work normally

by-claude

Use optional chaining on choices[0].delta.tool_calls to guard against
chunks (e.g. usage/close chunks) that have no choices, which caused
"undefined is not an object" errors for all OpenRouter streaming calls.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant