fix:Get Claude Models - #1491
Conversation
WalkthroughThis change adds support for the Anthropic channel type in the model fetching logic. It introduces new structs to represent Anthropic's model response format, updates request header handling for Anthropic, and extends the response parsing logic to convert Anthropic models into the existing internal format. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Controller
participant AnthropicAPI
Client->>Controller: FetchUpstreamModels(channelType=Anthropic)
Controller->>AnthropicAPI: GET /models (with x-api-key, anthropic-version headers)
AnthropicAPI-->>Controller: AnthropicModelsResponse
Controller->>Controller: Unmarshal response to AnthropicModelsResponse
Controller->>Controller: Convert to OpenAIModel format
Controller-->>Client: Return models list
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
controller/channel.go(3 hunks)
🔇 Additional comments (3)
controller/channel.go (3)
63-69: LGTM! Well-structured Anthropic model representation.The
AnthropicModelstruct properly defines the expected fields from Anthropic's API response with appropriate JSON tags.
71-77: LGTM! Proper pagination response structure.The
AnthropicModelsResponsestruct correctly handles paginated responses with appropriate fields for data and pagination metadata.
255-265: LGTM! Proper response parsing and format conversion.The Anthropic response parsing correctly:
- Unmarshals the API response into the appropriate struct
- Converts to the standardized OpenAI format used internally
- Follows the established pattern for model ID extraction
- Maintains consistency with other provider implementations
| case constant.ChannelTypeAnthropic: | ||
| // Authropic模型获取Header | ||
| header := http.Header{} | ||
| header.Add("x-api-key", channel.Key) | ||
| header.Add("anthropic-version", "2023-06-01") | ||
| body, err = GetResponseBody("GET", url, channel, header) |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Verify the Anthropic API version and consider making it configurable.
The implementation correctly sets the required headers for Anthropic API calls. However, the hardcoded version "2023-06-01" should be verified to ensure it's current and supported.
🌐 Web query:
What is the current recommended API version for Anthropic Claude models API?
💡 Result:
As of August 2, 2025, the recommended API version for Anthropic's Claude models is 2025-05-22. This version corresponds to the latest releases of Claude Sonnet 4 and Claude Opus 4, which were introduced on May 22, 2025. (docs.anthropic.com)
To ensure compatibility and access to the latest features, it's advisable to include the anthropic-version header in your API requests, specifying this version. For example:
anthropic-version: 2025-05-22
This practice helps maintain consistent behavior and leverages the most recent advancements in the Claude models.
Citations:
Update Anthropic API version and make it configurable
The hardcoded header value "2023-06-01" is now outdated. According to Anthropic’s docs, the current recommended version is 2025-05-22. To prevent this drifting out of sync in the future, pull the version string from configuration (or define it as a shared constant) rather than embedding it in the call site.
Locations to update:
- controller/channel.go (around line 225)
Suggested diff:
--- a/controller/channel.go
+++ b/controller/channel.go
@@ case constant.ChannelTypeAnthropic:
- header.Add("anthropic-version", "2023-06-01")
+ // Use configured Anthropic API version (defaults to latest: 2025-05-22)
+ header.Add("anthropic-version", config.AnthropicAPIVersion)• Define AnthropicAPIVersion in your config (e.g., via env var or a constants file) with default "2025-05-22".
• This ensures you can bump the version globally without touching each call site.
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In controller/channel.go around lines 223 to 228, the Anthropic API version
header is hardcoded as "2023-06-01", which is outdated. To fix this, define a
shared constant or configuration variable named AnthropicAPIVersion with the
value "2025-05-22" (e.g., in a config or constants file). Then replace the
hardcoded version string in the header.Add call with this constant to make the
version configurable and maintainable.
Thx to #1448, but I want to streamline it, only fix the issue with getting Claude models
Summary by CodeRabbit
New Features
Bug Fixes