Skip to content

# Bug: ${env:VAR} substitution in provider apiKey and baseURL override from models catalog #27853

Closed as not planned

Description

@Ish-oota

Description

Bug Summary

Two related bugs prevent opencode run from working with OpenCode GO models.

Bug 1: ${env:VAR} substitution fails in provider apiKey

Using ${env:OPENCODE_API_KEY} in opencode.json provider options does not resolve
to the environment variable value. The raw string is sent as the API key, causing
authentication failures.

Bug 2: baseURL in opencode.json silently overrides the models catalog

If a user defines baseURL for opencode-go in opencode.json, it silently overrides
the correct URL from the models catalog with no warning.

The correct endpoint from the models catalog (https://opencode.ai/zen/go/models-api.json) is:

"opencode-go": {
  "id": "opencode-go",
  "env": ["OPENCODE_API_KEY"],
  "npm": "@ai-sdk/openai-compatible",
  "api": "https://opencode.ai/zen/go/v1"
}

A common mistake is using https://api.opencode.ai/v1 (OpenAI-compatible style),
which is incorrect and causes auth failures.

Bug 3: opencode run returns tokens=0 with OpenCode GO models

Even after resolving Bugs 1 and 2, opencode run produces no output with OpenCode GO models.

Debug log comparison:

Provider SDK Package message.part.delta Result
Google @ai-sdk/google ✅ Received Works
OpenCode GO @ai-sdk/openai-compatible ❌ Empty Fails

Token result from opencode run:

{
  "tokens": { "input": 0, "output": 0, "reasoning": 0 },
  "finish": "other"
}

Key observation: TUI mode works correctly with the same model (mimo-v2.5-pro).
This indicates opencode run uses a different code path than TUI for OpenCode GO
provider routing, and that code path fails to parse the streaming response via
@ai-sdk/openai-compatible.

Workaround (confirmed working)

Remove the opencode-go provider block from opencode.json entirely:

{
  "provider": {
    // Do NOT define opencode-go here
  }
}

The CLI will automatically use:

  • Authentication from auth.json (registered via opencode providers login)
  • URL from the models catalog (https://opencode.ai/zen/go/v1)

This workaround restores TUI functionality but does not fix opencode run.

Plugins

No response

OpenCode version

1.15.1

Steps to reproduce

Bug 1: ${env:VAR} substitution failure

  1. Authenticate with OpenCode GO:
    opencode providers login
    
  2. Add the following to opencode.json:
    {
      "provider": {
        "opencode-go": {
          "options": {
            "apiKey": "${env:OPENCODE_API_KEY}"
          }
        }
      }
    }
  3. Set the environment variable:
    $env:OPENCODE_API_KEY = "sk-..."
  4. Run:
    opencode run -m opencode-go/mimo-v2.5-pro "test"
    
  5. Expected: Model responds normally
    Actual: Error: Invalid API key — ${env:OPENCODE_API_KEY} is sent as a literal string

Bug 2: baseURL silently overrides models catalog

  1. Add the following to opencode.json:
    {
      "provider": {
        "opencode-go": {
          "options": {
            "baseURL": "https://api.opencode.ai/v1"
          }
        }
      }
    }
  2. Run:
    opencode run -m opencode-go/mimo-v2.5-pro "test"
    
  3. Expected: Warning that baseURL differs from models catalog
    Actual: Incorrect URL silently overrides https://opencode.ai/zen/go/v1,
    causing Error: Invalid API key with no diagnostic message

Bug 3: opencode run returns tokens=0

  1. Remove all opencode-go config from opencode.json (use workaround above)
  2. Confirm TUI mode works:
    opencode   # interactive TUI → mimo-v2.5-pro responds ✅
    
  3. Run headless:
    opencode run -m opencode-go/mimo-v2.5-pro "Say OK"
    
  4. Expected: Model responds with text, non-zero token count
    Actual:
    { "tokens": { "input": 0, "output": 0, "reasoning": 0 }, "finish": "other" }
  5. Enable debug logging and compare providers:
    • @ai-sdk/google → message.part.delta received ✅
    • @ai-sdk/openai-compatible → message.part.delta empty ❌

Screenshot and/or share link

No response

Operating System

Windows 11

Terminal

Windows Terminal PowerShell 7+

Activity

  1. github-actions commented on May 16, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  2. Ish-oota commented on May 17, 2026

    @Ish-oota
    Author

    Thanks for the detailed report and cross-references — very helpful for triaging.

    Bug 1 (${env:VAR} substitution):
    This appears to be a duplicate of #19946. The syntax differs slightly, but the root cause is the same: env-var substitution fails to resolve inside custom provider options. Happy to close this in favour of #19946.

    Bug 2 (baseURL silently overriding catalog URL):
    I couldn't find an existing duplicate for this. Unlike #5674 where options aren't applied at all, here the user-defined baseURL is applied but silently overrides the correct catalog URL, producing a misleading Invalid API key error. I think this is worth tracking as a separate issue.

    Bug 3 (opencode run returning tokens=0):
    This looks like a match for #5210. The debug logs show message.part.delta is empty and the stream ends with finish: "other" rather than "stop" — consistent with text-delta events being dropped in processor.ts when text-start is missing. The fact that TUI works correctly on the same machine confirms this is isolated to the opencode run headless code path.

    I'd suggest keeping this issue open to track Bug 2 and Bug 3 (linked to #5210), while closing Bug 1 as a duplicate of #19946. Happy to provide additional logs if needed.

  3. github-actions commented on Jul 17, 2026

    @github-actions
    Contributor

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

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

Metadata

Metadata

Assignees

No one assigned

    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