fix: use Anthropic SDK provider for anthropic protocol with API key#867
fix: use Anthropic SDK provider for anthropic protocol with API key#867echowxsy wants to merge 2 commits intosipeed:mainfrom
Conversation
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>
nikolasdehor
left a comment
There was a problem hiding this comment.
Good fix for the model_list path — this correctly routes Anthropic API key configs through NewClaudeProviderWithBaseURL instead of the OpenAI-compatible HTTP provider.
Notes:
-
Complementary to PR #444: This PR fixes
factory_provider.go(CreateProviderFromConfig, formodel_listconfigs), while PR #444 fixesfactory.go(resolveProviderSelection/CreateProvider, for single-provider configs). Both should be merged for complete coverage. -
Dropped parameters: The old code passed
cfg.Proxy,cfg.MaxTokensField, andcfg.RequestTimeoutto the HTTP provider.NewClaudeProviderWithBaseURLdoes not accept these — consider whether this is a regression for users with proxy settings configured. -
Empty apiBase: When
cfg.APIBaseis empty, the old code set a defaulthttps://api.anthropic.com/v1. Verify thatNewClaudeProviderWithBaseURLhandles emptyapiBasecorrectly (likely via Anthropic SDK defaults). -
Tests: Please add at least one test case for
CreateProviderFromConfigwith 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>
|
Thanks for the thorough review! Addressing each point: Complementary to PR #444: Agreed — this PR fixes Dropped parameters (Proxy, MaxTokensField, RequestTimeout): This is intentional and consistent with the existing OAuth/token auth path (lines 115-121), which also uses Empty apiBase: Verified — Tests: Added in
All provider tests pass: |
Summary
anthropicprotocol inmodel_listto use the real Anthropic SDK provider (NewClaudeProviderWithBaseURL) instead of the OpenAI-compatible HTTP providerProblem
When using
model_listwithanthropic/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" }CreateProviderFromConfigfell into thecase "anthropic"branch (line 114) but created anHTTPProvider(OpenAI-compatible), which sent/v1/chat/completionsformat requests to an Anthropic Messages API endpoint → 404.Fix
Replace
NewHTTPProviderWithMaxTokensFieldAndRequestTimeoutwithNewClaudeProviderWithBaseURLfor the API key path, so it uses the official Anthropic SDK which sends proper/v1/messagesformat requests.Testing
Tested on a MaixCAM (RISC-V) device with Zhipu GLM coding plan endpoint (
https://api.z.ai/api/anthropic/v1):🤖 Generated with Claude Code