[PR #6483] fix(provider): validate baseURL format to prevent ERR_INVALID_URL #11930

Open
opened 2026-02-16 18:16:52 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6483

State: open
Merged: No


Summary

  • Adds URL format validation for provider baseURL/api fields
  • Prevents cryptic ERR_INVALID_URL errors when invalid values like "anthropic" or "" reach the SDK
  • Defense-in-depth with two validation layers

Changes

Phase 1: Runtime Validation (provider.ts)

  • Validates baseURL is a proper URL (http:// or https://) before passing to SDK
  • Invalid values are filtered out, letting SDK use its default endpoint

Phase 2: Config-Time Validation (config.ts)

  • Catches invalid api field values at config load time
  • Hybrid strategy: validates against models.dev cache when available, falls back to URL format check
  • Provides helpful error messages with hints

Root Cause

The @ai-sdk/* SDKs use nullish coalescing (??) for baseURL defaults, which doesn't catch empty strings or non-URL values—only null/undefined.

Test Plan

  • Typecheck passes
  • Invalid config values ("api": "anthropic") caught with clear error message
  • Valid configs load normally
  • SDK uses default URL when invalid baseURL is filtered

Example Error Message

Invalid provider configuration:

  provider.anthropic.api: Invalid value "anthropic"
    Hint: Remove the "api" field to use the default anthropic endpoint

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6483 **State:** open **Merged:** No --- ## Summary - Adds URL format validation for provider `baseURL`/`api` fields - Prevents cryptic `ERR_INVALID_URL` errors when invalid values like `"anthropic"` or `""` reach the SDK - Defense-in-depth with two validation layers ## Changes ### Phase 1: Runtime Validation (`provider.ts`) - Validates `baseURL` is a proper URL (`http://` or `https://`) before passing to SDK - Invalid values are filtered out, letting SDK use its default endpoint ### Phase 2: Config-Time Validation (`config.ts`) - Catches invalid `api` field values at config load time - Hybrid strategy: validates against models.dev cache when available, falls back to URL format check - Provides helpful error messages with hints ## Root Cause The `@ai-sdk/*` SDKs use nullish coalescing (`??`) for baseURL defaults, which doesn't catch empty strings or non-URL values—only `null`/`undefined`. ## Test Plan - [x] Typecheck passes - [x] Invalid config values (`"api": "anthropic"`) caught with clear error message - [x] Valid configs load normally - [x] SDK uses default URL when invalid baseURL is filtered ## Example Error Message ``` Invalid provider configuration: provider.anthropic.api: Invalid value "anthropic" Hint: Remove the "api" field to use the default anthropic endpoint ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code)
yindo added the pull-request label 2026-02-16 18:16:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11930