GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Missing array bounds checking in LLM provider response handling · Issue #11700 · anomalyco/opencode

Missing array bounds checking in LLM provider response handling #11700

New issue
Copy link
New issue
Copy link
Closed
Closed
Missing array bounds checking in LLM provider response handling#11700
Copy link
Assignees
rekram1-node

Description

@ryan-mt
ryan-mt
opened on Feb 2, 2026
Issue body actions

Summary

The LLM provider SDK code accesses array elements without checking if the array is non-empty, which can cause runtime crashes when the API returns unexpected responses.

Locations

  • packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-chat-language-model.ts:216
  • packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-chat-language-model.ts:435
  • packages/opencode/src/provider/sdk/copilot/responses/openai-responses-language-model.ts:435

Issue

// Line 216 - doGenerate()
const choice = responseBody.choices[0]  // No check if choices array is empty
const content: Array<LanguageModelV2Content> = []
const text = choice.message.content  // Will throw if choice is undefined

// Line 435 - doStream()
const choice = value.choices[0]  // Same issue in streaming
if (choice?.finish_reason != null) {
  // ...
}

If the API returns an empty choices array (which can happen on errors or edge cases), accessing choices[0] returns undefined, and subsequent property access throws "Cannot read property 'message' of undefined".

Impact

  • Severity: Medium
  • Type: Runtime Error
  • Effect: Unhandled exception when API returns empty choices

Suggested Fix

Add bounds checking:

const choice = responseBody.choices[0]
if (!choice) {
  throw new InvalidResponseDataError({
    data: responseBody,
    message: "Response contained no choices"
  })
}

Or use optional chaining with fallback:

const choice = responseBody.choices?.[0]
if (!choice?.message) {
  // Handle empty response case
}

Activity

  1. github-actions
    assigned
    rekram1-node
    on Feb 2, 2026
  2. mugnimaestra commented on Feb 7, 2026

    @mugnimaestra
    mugnimaestra
    on Feb 7, 2026
    Contributor
    More actions

    Tangentially related: PR #12567 includes a fix in the same file (openai-compatible-chat-language-model.ts) for a different error path — the Copilot API can send multiple reasoning_opaque deltas in a single response (e.g., one with reasoning text, another with tool calls), and the previous code threw InvalidResponseDataError on the second one. The fix now accepts the latest value instead. Not a bounds-check fix, but addresses another crash scenario in this provider.

  3. github-actions commented on Apr 9, 2026

    @github-actions
    github-actionsBot
    on Apr 9, 2026 – with GitHub Actions
    Contributor
    More actions

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

  4. github-actions
    closed this as completedon Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @rekram1-node
    rekram1-node

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.