fix(provider): resolve 401 when fetching models from Anthropic-compatible endpoints#3739
Open
thedavidweng wants to merge 2 commits into
Open
fix(provider): resolve 401 when fetching models from Anthropic-compatible endpoints#3739thedavidweng wants to merge 2 commits into
thedavidweng wants to merge 2 commits into
Conversation
…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
Owner
|
@codex review |
There was a problem hiding this comment.
💡 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".
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When configuring an Anthropic-compatible provider (e.g.,
api.anthropic.comor third-party providers using the Anthropic protocol), clicking "Fetch Models" returns HTTP 401 Unauthorized.The
fetch_modelsfunction only sendsAuthorization: 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
ModelFetchStrategyenum (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-versionGoogleApiKey— Gemini style:x-goog-api-key2. Strategy-aware auth headers
fetch_models()now sends provider-appropriate headers based on the strategy, instead of always sending onlyAuthorization: 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
ClaudeFormFields.tsx) and Claude Desktop form (ClaudeDesktopProviderForm.tsx) now pass"anthropic"strategymodel-fetch.ts) exposes thestrategyparameter"bearer"strategy5. New unit tests
test_anthropic_prefers_v1_models— verifies Anthropic strategy URL orderingtest_bearer_prefers_v1_models— verifies Bearer strategy URL orderingReference
This issue was also fixed in the downstream fork: