Skip to content

fix(core): guard against empty choices in convertOpenAIResponseToGemini#2364

Open
netbrah wants to merge 1 commit intoQwenLM:mainfrom
netbrah:fix/empty-choices-crash
Open

fix(core): guard against empty choices in convertOpenAIResponseToGemini#2364
netbrah wants to merge 1 commit intoQwenLM:mainfrom
netbrah:fix/empty-choices-crash

Conversation

@netbrah
Copy link
Contributor

@netbrah netbrah commented Mar 13, 2026

Summary

convertOpenAIResponseToGemini accesses choices[0] without a guard, crashing with TypeError when a provider returns an empty choices array. The streaming counterpart (convertOpenAIChunkToGemini) already uses choices?.[0] with an if (choice) guard — this fix applies the same pattern to the non-streaming path.

Fix

  • Added optional chaining: choices?.[0]
  • Added early return with empty candidates when choice is falsy

Test

Added test: empty choices array returns a response with candidates: [] instead of crashing.

Made with Cursor

The streaming path (convertOpenAIChunkToGemini) already uses optional
chaining on choices and guards with `if (choice)`, but the
non-streaming path accesses choices[0] directly. Providers that return
an empty choices array cause a TypeError crash.

Made-with: Cursor
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