Skip to content

fix: use Anthropic SDK provider for anthropic protocol with API key#867

Open
echowxsy wants to merge 2 commits intosipeed:mainfrom
echowxsy:fix/anthropic-protocol-use-sdk-provider
Open

fix: use Anthropic SDK provider for anthropic protocol with API key#867
echowxsy wants to merge 2 commits intosipeed:mainfrom
echowxsy:fix/anthropic-protocol-use-sdk-provider

Conversation

@echowxsy
Copy link

Summary

  • Fix anthropic protocol in model_list to use the real Anthropic SDK provider (NewClaudeProviderWithBaseURL) instead of the OpenAI-compatible HTTP provider
  • The previous code sent OpenAI-format requests to Anthropic-compatible endpoints, causing 404 errors

Problem

When using model_list with anthropic/ protocol and an API key:

{
  "model_name": "glm-4.7",
  "model": "anthropic/glm-4.7",
  "api_key": "your-key",
  "api_base": "https://api.z.ai/api/anthropic/v1"
}

CreateProviderFromConfig fell into the case "anthropic" branch (line 114) but created an HTTPProvider (OpenAI-compatible), which sent /v1/chat/completions format requests to an Anthropic Messages API endpoint → 404.

Fix

Replace NewHTTPProviderWithMaxTokensFieldAndRequestTimeout with NewClaudeProviderWithBaseURL for the API key path, so it uses the official Anthropic SDK which sends proper /v1/messages format requests.

Testing

Tested on a MaixCAM (RISC-V) device with Zhipu GLM coding plan endpoint (https://api.z.ai/api/anthropic/v1):

Before: 404 Not Found
After:  🦞 2 + 2 = 4 ✅

🤖 Generated with Claude Code

The anthropic protocol case in CreateProviderFromConfig incorrectly used
NewHTTPProviderWithMaxTokensFieldAndRequestTimeout (OpenAI-compatible
HTTP provider) when an API key was provided. This sent OpenAI-format
requests to Anthropic-compatible endpoints, causing 404 errors.

Switch to NewClaudeProviderWithBaseURL which uses the official Anthropic
SDK and sends proper Anthropic Messages API format requests. This fixes
model_list entries like:

  {"model": "anthropic/glm-4.7", "api_base": "https://api.z.ai/api/anthropic/v1"}

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

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

Good fix for the model_list path — this correctly routes Anthropic API key configs through NewClaudeProviderWithBaseURL instead of the OpenAI-compatible HTTP provider.

Notes:

  1. Complementary to PR #444: This PR fixes factory_provider.go (CreateProviderFromConfig, for model_list configs), while PR #444 fixes factory.go (resolveProviderSelection / CreateProvider, for single-provider configs). Both should be merged for complete coverage.

  2. Dropped parameters: The old code passed cfg.Proxy, cfg.MaxTokensField, and cfg.RequestTimeout to the HTTP provider. NewClaudeProviderWithBaseURL does not accept these — consider whether this is a regression for users with proxy settings configured.

  3. Empty apiBase: When cfg.APIBase is empty, the old code set a default https://api.anthropic.com/v1. Verify that NewClaudeProviderWithBaseURL handles empty apiBase correctly (likely via Anthropic SDK defaults).

  4. Tests: Please add at least one test case for CreateProviderFromConfig with anthropic protocol and API key.

- Verify anthropic protocol returns *ClaudeProvider (not *HTTPProvider)
- Test custom api_base for Anthropic-compatible endpoints
- Test missing API key error message

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@echowxsy
Copy link
Author

Thanks for the thorough review! Addressing each point:

Complementary to PR #444: Agreed — this PR fixes CreateProviderFromConfig (model_list path) while #444 fixes CreateProvider (single-provider path). Both should be merged for complete coverage.

Dropped parameters (Proxy, MaxTokensField, RequestTimeout): This is intentional and consistent with the existing OAuth/token auth path (lines 115-121), which also uses ClaudeProvider without these parameters. The Anthropic SDK manages HTTP internally via anthropic-sdk-go and doesn't expose proxy/timeout at the constructor level. If proxy support for Anthropic protocol becomes needed, it would require passing option.WithHTTPClient() with a custom transport — that could be a separate enhancement PR.

Empty apiBase: Verified — normalizeBaseURL("") returns defaultBaseURL ("https://api.anthropic.com"), so empty api_base correctly falls back to the official Anthropic API endpoint.

Tests: Added in 3e1e2b7:

  • TestCreateProviderFromConfig_Anthropic — updated to verify *ClaudeProvider type (not *HTTPProvider)
  • TestCreateProviderFromConfig_AnthropicWithCustomAPIBase — tests custom api_base (e.g., Zhipu coding plan endpoint)
  • TestCreateProviderFromConfig_AnthropicMissingAPIKey — verifies proper error message

All provider tests pass: go test -tags stdjson ./pkg/providers/

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