Skip to content

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

@thedavidweng

Description

@thedavidweng

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.

Root Cause

The fetch_models function in src-tauri/src/services/model_fetch.rs only sends the Authorization: Bearer <key> header for all providers:

.header("Authorization", format!("Bearer {api_key}"))

However, Anthropic's API requires:

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

The Authorization: Bearer header alone is not accepted by Anthropic endpoints, causing 401 errors.

Additionally: URL candidate ordering

The build_models_url_candidates function currently prefers /v1/models first for non-versioned URLs, which happens to be correct for Anthropic. However, without the proper auth headers, the correct URL still returns 401.

Reference

This issue was also fixed in the downstream fork:

The fix introduces a ModelFetchStrategy enum that sends provider-appropriate headers:

  • BearerAuthorization: Bearer <key> (OpenAI style)
  • AnthropicAuthorization: Bearer <key> + x-api-key + anthropic-version
  • GoogleApiKeyx-goog-api-key (Gemini style)

Expected Behavior

Model fetching should work correctly for Anthropic-compatible endpoints by sending the appropriate authentication headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions