OpenCode Zen Gemini Integration - 500 Internal Server Error #5346

Open
opened 2026-02-16 17:51:39 -05:00 by yindo · 0 comments
Owner

Originally created by @nitech on GitHub (Jan 13, 2026).

Originally assigned to: @fwang on GitHub.

Description

Summary

OpenCode Zen's Gemini endpoints (gemini-3-pro and gemini-3-flash) consistently return a 500 Internal Server Error with the message: "Cannot read properties of undefined (reading 'promptTokenCount')". This appears to be a server-side bug in the response handling logic.

Environment

  • Package: @ai-sdk/google (configured to use OpenCode Zen endpoints)
  • OpenCode Zen API: https://opencode.ai/zen/v1
  • Tested Models: gemini-3-pro, gemini-3-flash
  • Authentication: Bearer token (OPENCODE_API_KEY)

Expected Behavior

According to the OpenCode Zen documentation, Gemini models should be accessible via:

  • Endpoint: https://opencode.ai/zen/v1/models/gemini-3-pro
  • SDK: @ai-sdk/google

The endpoint should accept requests and return valid Gemini responses.

Actual Behavior

All requests to Gemini endpoints return:

{
  "type": "error",
  "error": {
    "type": "error",
    "message": "Cannot read properties of undefined (reading 'promptTokenCount')"
  }
}

HTTP Status: 500 Internal Server Error

Plugins

No response

OpenCode version

No response

Steps to reproduce

1. Using @ai-sdk/google (Correct SDK per documentation)

import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { generateText } from 'ai';

const google = createGoogleGenerativeAI({
  apiKey: process.env.OPENCODE_API_KEY,
  baseURL: 'https://opencode.ai/zen/v1',
});

const model = google('models/gemini-3-pro');

// This crashes with 500 error
const result = await generateText({
  model,
  prompt: 'Hello',
  maxTokens: 10,
});

Actual Request Generated:

  • URL: https://opencode.ai/zen/v1/models/gemini-3-pro:generateContent
  • Method: POST
  • Headers: x-goog-api-key: <OPENCODE_API_KEY>
  • Body: {"generationConfig":{},"contents":[{"role":"user","parts":[{"text":"Hello"}]}]}

Response: 500 Internal Server Error

2. Using Direct HTTP Requests (Multiple Attempts)

Attempt A: Google JSON Format + x-goog-api-key

curl -X POST https://opencode.ai/zen/v1/models/gemini-3-pro \
  -H "x-goog-api-key: <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{"contents":[{"parts":[{"text":"Hello"}]}]}'

Result: 500 Internal Server Error - "Cannot read properties of undefined (reading 'promptTokenCount')"

Attempt B: With :generateContent suffix

curl -X POST https://opencode.ai/zen/v1/models/gemini-3-pro:generateContent \
  -H "x-goog-api-key: <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{"contents":[{"parts":[{"text":"Hello"}]}]}'

Result: 500 Internal Server Error - Same error message

Attempt C: Via /chat/completions endpoint with model ID

curl -X POST https://opencode.ai/zen/v1/chat/completions \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{"model":"gemini-3-pro","messages":[{"role":"user","content":"Hello"}]}'

Result: 500 Internal Server Error - Same error message

Attempt D: Authorization Bearer header (instead of x-goog-api-key)

curl -X POST https://opencode.ai/zen/v1/models/gemini-3-pro \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{"contents":[{"parts":[{"text":"Hello"}]}]}'

Result: 401 Unauthorized - "Missing API key"

Analysis

The error message "Cannot read properties of undefined (reading 'promptTokenCount')" suggests that OpenCode Zen's backend is:

  1. Successfully receiving and processing the request
  2. Likely making a successful call to Google's Gemini API (or upstream provider)
  3. Attempting to parse usage statistics from the response
  4. Failing because the usage object is undefined or malformed

This appears to be a bug in OpenCode Zen's response transformation/wrapping logic, specifically when handling Gemini's usage metadata.

Impact

  • Severity: High
  • Workaround: None available through OpenCode Zen
  • Users cannot access Gemini models despite them being listed as supported in the documentation
  • Affects both gemini-3-pro and gemini-3-flash

Additional Context

  • Other model families (GPT-5, Claude) work correctly through OpenCode Zen
  • The issue is isolated to Gemini models
  • The error is consistent across all request formats and authentication methods (when using x-goog-api-key)
  • The models are correctly listed in the /models endpoint response

Suggested Fix

The OpenCode Zen backend should:

  1. Handle cases where usage/token count metadata might be undefined or in a different format
  2. Add proper error handling to avoid 500 errors when parsing response metadata
  3. Consider returning partial responses if token counting fails, rather than failing the entire request

Model List Verification

The models are correctly advertised:

curl https://opencode.ai/zen/v1/models

Returns gemini-3-pro and gemini-3-flash in the list with "owned_by": "opencode".

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Originally created by @nitech on GitHub (Jan 13, 2026). Originally assigned to: @fwang on GitHub. ### Description ## Summary OpenCode Zen's Gemini endpoints (`gemini-3-pro` and `gemini-3-flash`) consistently return a 500 Internal Server Error with the message: `"Cannot read properties of undefined (reading 'promptTokenCount')"`. This appears to be a server-side bug in the response handling logic. ## Environment - **Package:** `@ai-sdk/google` (configured to use OpenCode Zen endpoints) - **OpenCode Zen API:** `https://opencode.ai/zen/v1` - **Tested Models:** `gemini-3-pro`, `gemini-3-flash` - **Authentication:** Bearer token (`OPENCODE_API_KEY`) ## Expected Behavior According to the [OpenCode Zen documentation](https://opencode.ai/docs/zen/#endpoints), Gemini models should be accessible via: - **Endpoint:** `https://opencode.ai/zen/v1/models/gemini-3-pro` - **SDK:** `@ai-sdk/google` The endpoint should accept requests and return valid Gemini responses. ## Actual Behavior All requests to Gemini endpoints return: ```json { "type": "error", "error": { "type": "error", "message": "Cannot read properties of undefined (reading 'promptTokenCount')" } } ``` HTTP Status: **500 Internal Server Error** ### Plugins _No response_ ### OpenCode version _No response_ ### Steps to reproduce ### 1. Using @ai-sdk/google (Correct SDK per documentation) ```typescript import { createGoogleGenerativeAI } from '@ai-sdk/google'; import { generateText } from 'ai'; const google = createGoogleGenerativeAI({ apiKey: process.env.OPENCODE_API_KEY, baseURL: 'https://opencode.ai/zen/v1', }); const model = google('models/gemini-3-pro'); // This crashes with 500 error const result = await generateText({ model, prompt: 'Hello', maxTokens: 10, }); ``` **Actual Request Generated:** - URL: `https://opencode.ai/zen/v1/models/gemini-3-pro:generateContent` - Method: `POST` - Headers: `x-goog-api-key: <OPENCODE_API_KEY>` - Body: `{"generationConfig":{},"contents":[{"role":"user","parts":[{"text":"Hello"}]}]}` **Response:** 500 Internal Server Error ### 2. Using Direct HTTP Requests (Multiple Attempts) #### Attempt A: Google JSON Format + x-goog-api-key ```bash curl -X POST https://opencode.ai/zen/v1/models/gemini-3-pro \ -H "x-goog-api-key: <API_KEY>" \ -H "Content-Type: application/json" \ -d '{"contents":[{"parts":[{"text":"Hello"}]}]}' ``` **Result:** 500 Internal Server Error - `"Cannot read properties of undefined (reading 'promptTokenCount')"` #### Attempt B: With :generateContent suffix ```bash curl -X POST https://opencode.ai/zen/v1/models/gemini-3-pro:generateContent \ -H "x-goog-api-key: <API_KEY>" \ -H "Content-Type: application/json" \ -d '{"contents":[{"parts":[{"text":"Hello"}]}]}' ``` **Result:** 500 Internal Server Error - Same error message #### Attempt C: Via /chat/completions endpoint with model ID ```bash curl -X POST https://opencode.ai/zen/v1/chat/completions \ -H "Authorization: Bearer <API_KEY>" \ -H "Content-Type: application/json" \ -d '{"model":"gemini-3-pro","messages":[{"role":"user","content":"Hello"}]}' ``` **Result:** 500 Internal Server Error - Same error message #### Attempt D: Authorization Bearer header (instead of x-goog-api-key) ```bash curl -X POST https://opencode.ai/zen/v1/models/gemini-3-pro \ -H "Authorization: Bearer <API_KEY>" \ -H "Content-Type: application/json" \ -d '{"contents":[{"parts":[{"text":"Hello"}]}]}' ``` **Result:** 401 Unauthorized - `"Missing API key"` ## Analysis The error message `"Cannot read properties of undefined (reading 'promptTokenCount')"` suggests that OpenCode Zen's backend is: 1. Successfully receiving and processing the request 2. Likely making a successful call to Google's Gemini API (or upstream provider) 3. Attempting to parse usage statistics from the response 4. **Failing because the usage object is undefined or malformed** This appears to be a bug in OpenCode Zen's response transformation/wrapping logic, specifically when handling Gemini's usage metadata. ## Impact - **Severity:** High - **Workaround:** None available through OpenCode Zen - Users cannot access Gemini models despite them being listed as supported in the documentation - Affects both `gemini-3-pro` and `gemini-3-flash` ## Additional Context - Other model families (GPT-5, Claude) work correctly through OpenCode Zen - The issue is isolated to Gemini models - The error is consistent across all request formats and authentication methods (when using `x-goog-api-key`) - The models are correctly listed in the `/models` endpoint response ## Suggested Fix The OpenCode Zen backend should: 1. Handle cases where usage/token count metadata might be undefined or in a different format 2. Add proper error handling to avoid 500 errors when parsing response metadata 3. Consider returning partial responses if token counting fails, rather than failing the entire request ## Model List Verification The models are correctly advertised: ```bash curl https://opencode.ai/zen/v1/models ``` Returns `gemini-3-pro` and `gemini-3-flash` in the list with `"owned_by": "opencode"`. ### Screenshot and/or share link _No response_ ### Operating System _No response_ ### Terminal _No response_
yindo added the bugzen labels 2026-02-16 17:51:39 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#5346