Skip to content

fix(provider): resolve 401 when fetching models from Anthropic-compatible endpoints#3739

Open
thedavidweng wants to merge 2 commits into
farion1231:mainfrom
thedavidweng:fix/anthropic-model-fetch-401
Open

fix(provider): resolve 401 when fetching models from Anthropic-compatible endpoints#3739
thedavidweng wants to merge 2 commits into
farion1231:mainfrom
thedavidweng:fix/anthropic-model-fetch-401

Conversation

@thedavidweng
Copy link
Copy Markdown

@thedavidweng thedavidweng commented Jun 4, 2026

Problem

When configuring an Anthropic-compatible provider (e.g., api.anthropic.com or third-party providers using the Anthropic protocol), clicking "Fetch Models" returns HTTP 401 Unauthorized.

The fetch_models function only sends Authorization: Bearer <key>, but Anthropic's API requires:

  • x-api-key: <key> (primary authentication)
  • anthropic-version: 2023-06-01 (required version header)

Fixes #3738

Changes

1. Introduce ModelFetchStrategy enum (src-tauri/src/services/model_fetch.rs)

Three variants controlling auth headers and URL candidate ordering:

  • Bearer (default) — OpenAI style: Authorization: Bearer <key>
  • Anthropic — Anthropic style: Authorization + x-api-key + anthropic-version
  • GoogleApiKey — Gemini style: x-goog-api-key

2. Strategy-aware auth headers

fetch_models() now sends provider-appropriate headers based on the strategy, instead of always sending only Authorization: Bearer.

3. Strategy-aware URL candidate ordering

build_models_url_candidates() now uses the strategy to determine URL ordering when the base URL doesn't contain a version segment.

4. Frontend integration

  • Claude form (ClaudeFormFields.tsx) and Claude Desktop form (ClaudeDesktopProviderForm.tsx) now pass "anthropic" strategy
  • TypeScript API wrapper (model-fetch.ts) exposes the strategy parameter
  • Other forms (Codex, Gemini, OpenCode, etc.) continue using the default "bearer" strategy

5. New unit tests

  • test_anthropic_prefers_v1_models — verifies Anthropic strategy URL ordering
  • test_bearer_prefers_v1_models — verifies Bearer strategy URL ordering

Reference

This issue was also fixed in the downstream fork:

…ible endpoints

When a user configures an Anthropic-compatible provider (e.g. api.anthropic.com
or third-party providers using the Anthropic protocol), clicking 'Fetch Models'
returns HTTP 401 Unauthorized because the code only sends Authorization: Bearer,
while Anthropic requires x-api-key + anthropic-version headers.

Changes:
- Introduce ModelFetchStrategy enum (Bearer, Anthropic, GoogleApiKey) to control
  auth headers and URL candidate ordering per provider type
- fetch_models() now sends provider-appropriate headers based on strategy:
  - Bearer: Authorization: Bearer (OpenAI style)
  - Anthropic: Authorization + x-api-key + anthropic-version
  - GoogleApiKey: x-goog-api-key
- build_models_url_candidates() is now strategy-aware for URL ordering
- Claude form callers pass 'anthropic' strategy by default
- Add unit tests for Anthropic and Bearer URL ordering

Fixes farion1231#3738
Ref: SaladDay/cc-switch-cli#227
@farion1231
Copy link
Copy Markdown
Owner

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a72df559ab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src-tauri/src/services/model_fetch.rs
Comment thread src-tauri/src/services/model_fetch.rs Outdated
- Remove Authorization: Bearer header from Anthropic strategy (only send
  x-api-key + anthropic-version, not Bearer which Anthropic rejects)
- Preserve /v1/models fallback candidate for versioned URLs like /v4
  (regression: the fallback was dropped when refactoring to strategy-aware
  URL ordering)
- Run cargo fmt and prettier to fix CI formatting checks
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.

fetch_models returns 401 for Anthropic-compatible endpoints due to missing auth headers

2 participants