Skip to content

Conversation

@brettheap
Copy link
Contributor

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

@rekram1-node
Copy link
Collaborator

if you want this to get merged it'd be nice if it was cleaned up, this should be a very very simple change like maybe 3 lines max

@brettheap brettheap force-pushed the fix/anthropic-baseurl-error branch from 75961c6 to 64a8ce4 Compare January 1, 2026 13:25
Prevents ERR_INVALID_URL by validating baseURL values before passing
to AI SDK. The SDK's nullish coalescing (??) only catches null/undefined,
not empty strings or invalid values like 'anthropic'.

When an invalid baseURL is provided, it's filtered out and the SDK
uses its default endpoint instead.
@brettheap brettheap force-pushed the fix/anthropic-baseurl-error branch from 64a8ce4 to 1620bb6 Compare January 1, 2026 13:27
@brettheap
Copy link
Contributor Author

Updated PR to address feedback:

  • ✅ Simplified to 1 focused commit (down from 37)
  • Only the minimal runtime validation (~9 lines)
  • ✅ Rebased on latest upstream/dev
  • ✅ Removed config-time validation (Phase 2) - saved for future PR
  • ✅ Removed all documentation/openspec files

The fix now validates baseURL format before passing to the SDK, preventing ERR_INVALID_URL when users misconfigure provider settings. Just as you requested - "a very very simple change" 👍

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.

2 participants