@@ -121,20 +121,6 @@ func (provider *AnthropicProvider) GetProviderKey() schemas.ModelProvider {
121121 return getProviderName (schemas .Anthropic , provider .customProviderConfig )
122122}
123123
124- // parseStreamAnthropicError parses Anthropic streaming error responses.
125- func parseStreamAnthropicError (resp * http.Response , providerType schemas.ModelProvider ) * schemas.BifrostError {
126- statusCode := resp .StatusCode
127- body , _ := io .ReadAll (resp .Body )
128- resp .Body .Close ()
129-
130- var errorResp anthropic.AnthropicError
131- if err := sonic .Unmarshal (body , & errorResp ); err != nil {
132- return newBifrostOperationError (schemas .ErrProviderResponseUnmarshal , err , providerType )
133- }
134-
135- return newProviderAPIError (errorResp .Error .Message , nil , statusCode , providerType , & errorResp .Error .Type , nil )
136- }
137-
138124// completeRequest sends a request to Anthropic's API and handles the response.
139125// It constructs the API URL, sets up authentication, and processes the response.
140126// Returns the response body or an error if the request fails.
@@ -188,14 +174,9 @@ func (provider *AnthropicProvider) completeRequest(ctx context.Context, requestB
188174 return bodyCopy , latency , nil
189175}
190176
191- // ListModels performs a list models request to Anthropic's API.
192- func (provider * AnthropicProvider ) ListModels (ctx context.Context , key schemas.Key , request * schemas.BifrostListModelsRequest ) (* schemas.BifrostListModelsResponse , * schemas.BifrostError ) {
193- if err := checkOperationAllowed (schemas .Anthropic , provider .customProviderConfig , schemas .ListModelsRequest ); err != nil {
194- return nil , err
195- }
196-
197- providerName := provider .GetProviderKey ()
198-
177+ // listModelsByKey performs a list models request for a single key.
178+ // Returns the response and latency, or an error if the request fails.
179+ func (provider * AnthropicProvider ) listModelsByKey (ctx context.Context , key schemas.Key , request * schemas.BifrostListModelsRequest ) (* schemas.BifrostListModelsResponse , * schemas.BifrostError ) {
199180 // Create request
200181 req := fasthttp .AcquireRequest ()
201182 resp := fasthttp .AcquireResponse ()
@@ -206,8 +187,7 @@ func (provider *AnthropicProvider) ListModels(ctx context.Context, key schemas.K
206187 setExtraHeaders (req , provider .networkConfig .ExtraHeaders , nil )
207188
208189 // Build URL using centralized URL construction
209- requestURL := anthropic .ToAnthropicListModelsURL (request , provider .networkConfig .BaseURL + "/v1/models" )
210- req .SetRequestURI (requestURL )
190+ req .SetRequestURI (fmt .Sprintf ("%s/v1/models?limit=%d" , provider .networkConfig .BaseURL , schemas .DefaultPageSize ))
211191 req .Header .SetMethod (http .MethodGet )
212192 req .Header .SetContentType ("application/json" )
213193 req .Header .Set ("x-api-key" , key .Value )
@@ -221,14 +201,10 @@ func (provider *AnthropicProvider) ListModels(ctx context.Context, key schemas.K
221201
222202 // Handle error response
223203 if resp .StatusCode () != fasthttp .StatusOK {
224- provider .logger .Debug (fmt .Sprintf ("error from %s provider: %s" , provider .GetProviderKey (), string (resp .Body ())))
225-
226204 var errorResp anthropic.AnthropicError
227-
228205 bifrostErr := handleProviderAPIError (resp , & errorResp )
229206 bifrostErr .Error .Type = & errorResp .Error .Type
230207 bifrostErr .Error .Message = errorResp .Error .Message
231-
232208 return nil , bifrostErr
233209 }
234210
@@ -240,11 +216,7 @@ func (provider *AnthropicProvider) ListModels(ctx context.Context, key schemas.K
240216 }
241217
242218 // Create final response
243- response := anthropicResponse .ToBifrostListModelsResponse (providerName )
244-
245- // Set ExtraFields
246- response .ExtraFields .Provider = providerName
247- response .ExtraFields .RequestType = schemas .ListModelsRequest
219+ response := anthropicResponse .ToBifrostListModelsResponse (provider .GetProviderKey ())
248220 response .ExtraFields .Latency = latency .Milliseconds ()
249221
250222 // Set raw response if enabled
@@ -255,6 +227,23 @@ func (provider *AnthropicProvider) ListModels(ctx context.Context, key schemas.K
255227 return response , nil
256228}
257229
230+ // ListModels performs a list models request to Anthropic's API.
231+ // It fetches models using all provided keys and aggregates the results.
232+ // Uses a best-effort approach: continues with remaining keys even if some fail.
233+ // Requests are made concurrently for improved performance.
234+ func (provider * AnthropicProvider ) ListModels (ctx context.Context , keys []schemas.Key , request * schemas.BifrostListModelsRequest ) (* schemas.BifrostListModelsResponse , * schemas.BifrostError ) {
235+ if err := checkOperationAllowed (schemas .Anthropic , provider .customProviderConfig , schemas .ListModelsRequest ); err != nil {
236+ return nil , err
237+ }
238+ return handleMultipleListModelsRequests (
239+ ctx ,
240+ keys ,
241+ request ,
242+ provider .listModelsByKey ,
243+ provider .logger ,
244+ )
245+ }
246+
258247// TextCompletion performs a text completion request to Anthropic's API.
259248// It formats the request, sends it to Anthropic, and processes the response.
260249// Returns a BifrostResponse containing the completion results or an error if the request fails.
@@ -852,3 +841,17 @@ func (provider *AnthropicProvider) Transcription(ctx context.Context, key schema
852841func (provider * AnthropicProvider ) TranscriptionStream (ctx context.Context , postHookRunner schemas.PostHookRunner , key schemas.Key , request * schemas.BifrostTranscriptionRequest ) (chan * schemas.BifrostStream , * schemas.BifrostError ) {
853842 return nil , newUnsupportedOperationError ("transcription stream" , "anthropic" )
854843}
844+
845+ // parseStreamAnthropicError parses Anthropic streaming error responses.
846+ func parseStreamAnthropicError (resp * http.Response , providerType schemas.ModelProvider ) * schemas.BifrostError {
847+ statusCode := resp .StatusCode
848+ body , _ := io .ReadAll (resp .Body )
849+ resp .Body .Close ()
850+
851+ var errorResp anthropic.AnthropicError
852+ if err := sonic .Unmarshal (body , & errorResp ); err != nil {
853+ return newBifrostOperationError (schemas .ErrProviderResponseUnmarshal , err , providerType )
854+ }
855+
856+ return newProviderAPIError (errorResp .Error .Message , nil , statusCode , providerType , & errorResp .Error .Type , nil )
857+ }
0 commit comments