fix(providers): add fallback support at provider creation time

Previously, CreateProvider would fail immediately if the primary model
couldn't create a provider (e.g., antigravity model with no OAuth
credentials), even when model_fallbacks were configured in
agents.defaults.

This change makes CreateProvider try each model in the fallback list
when the primary fails, allowing graceful degradation when OAuth
credentials expire or are missing.

Fixes the issue where users with gemini-flash as default model but
no antigravity credentials would get an error instead of falling back
to other configured models.

The fallback chain during Chat() calls remains unchanged and continues
to work as before. This fix ensures provider creation itself respects
the configured fallbacks.
This commit is contained in:
Vishnuvardhan Reddy 2026-02-26 12:03:35 +00:00
parent 8a1fb03974
commit baa62885eb

View file

@ -28,10 +28,20 @@ func CreateProvider(cfg *config.Config) (LLMProvider, string, error) {
return nil, "", fmt.Errorf("no providers configured. Please add entries to model_list in your config") return nil, "", fmt.Errorf("no providers configured. Please add entries to model_list in your config")
} }
// Collect models to try: primary first, then fallbacks
modelsToTry := []string{model}
modelsToTry = append(modelsToTry, cfg.Agents.Defaults.ModelFallbacks...)
var lastErr error
for i, modelName := range modelsToTry {
// Get model config from model_list // Get model config from model_list
modelCfg, err := cfg.GetModelConfig(model) modelCfg, err := cfg.GetModelConfig(modelName)
if err != nil { if err != nil {
return nil, "", fmt.Errorf("model %q not found in model_list: %w", model, err) lastErr = fmt.Errorf("model %q not found in model_list: %w", modelName, err)
if i == len(modelsToTry)-1 {
return nil, "", lastErr
}
continue
} }
// Inject global workspace if not set in model config // Inject global workspace if not set in model config
@ -42,8 +52,22 @@ func CreateProvider(cfg *config.Config) (LLMProvider, string, error) {
// Use factory to create provider // Use factory to create provider
provider, modelID, err := CreateProviderFromConfig(modelCfg) provider, modelID, err := CreateProviderFromConfig(modelCfg)
if err != nil { if err != nil {
return nil, "", fmt.Errorf("failed to create provider for model %q: %w", model, err) lastErr = fmt.Errorf("failed to create provider for model %q: %w", modelName, err)
// If this is the last model, return the error
if i == len(modelsToTry)-1 {
return nil, "", fmt.Errorf("all provider creation attempts failed. Last error: %w", lastErr)
}
// Otherwise, try the next fallback model
continue
} }
// Success! Return the provider
if modelName != model {
// Log that we're using a fallback model
return provider, modelID, nil return provider, modelID, nil
}
return provider, modelID, nil
}
return nil, "", lastErr
} }