diff --git a/llm/retry.go b/llm/retry.go index c2149e3..3e524e2 100644 --- a/llm/retry.go +++ b/llm/retry.go @@ -6,6 +6,7 @@ import ( anthropic "github.com/anthropics/anthropic-sdk-go" openai "github.com/sashabaranov/go-openai" + "google.golang.org/genai" ) // IsRetryableModelError reports whether err is a transient capacity @@ -14,14 +15,17 @@ import ( // // - Anthropic 429 (rate limit) and 529 (overloaded) // - OpenAI 429 (rate limit) and 5xx (server / overloaded) +// - Gemini 429 (quota/RESOURCE_EXHAUSTED) and 5xx (overloaded/unavailable), +// recognized via a typed genai.APIError path (Gemini's error string +// "Error 429, ... Status: RESOURCE_EXHAUSTED" does not match the +// bounded substring forms, so the typed path is required). // // Anything else (including 4xx auth/validation errors) returns false: // retrying with a different model wouldn't fix the underlying problem. // -// Errors from local providers (Ollama, etc.) and Gemini are NOT -// classified as retryable here; their failure modes are different -// and a model swap rarely helps. Add cases as new providers -// accumulate retry experience. +// Errors from local providers (Ollama, etc.) are NOT classified as +// retryable here; their failure modes are different and a model swap +// rarely helps. Add cases as new providers accumulate retry experience. func IsRetryableModelError(err error) bool { if err == nil { return false @@ -47,6 +51,13 @@ func IsRetryableModelError(err error) bool { return openaiReqErr.HTTPStatusCode == 429 || (openaiReqErr.HTTPStatusCode >= 500 && openaiReqErr.HTTPStatusCode < 600) } + // genai.APIError.Error() is a value receiver, so the error interface is + // satisfied by the value; errors.As targets the value type. + var genaiErr genai.APIError + if errors.As(err, &genaiErr) { + return genaiErr.Code == 429 || + (genaiErr.Code >= 500 && genaiErr.Code < 600) + } // Last-ditch: substring match on common SDK error wrappers that // don't unwrap cleanly to typed errors. Cheap belt-and-braces; diff --git a/llm/retry_test.go b/llm/retry_test.go index 1c26e81..d22ee17 100644 --- a/llm/retry_test.go +++ b/llm/retry_test.go @@ -9,8 +9,25 @@ import ( openai "github.com/sashabaranov/go-openai" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "google.golang.org/genai" ) +func TestIsRetryable_Gemini429(t *testing.T) { + // Mirror genai.APIError's real shape. + err := genai.APIError{Code: 429, Message: "Resource has been exhausted (e.g. check quota).", Status: "RESOURCE_EXHAUSTED"} + require.True(t, IsRetryableModelError(err)) +} + +func TestIsRetryable_Gemini503(t *testing.T) { + err := genai.APIError{Code: 503, Message: "The model is overloaded.", Status: "UNAVAILABLE"} + require.True(t, IsRetryableModelError(err)) +} + +func TestIsRetryable_Gemini400NotRetryable(t *testing.T) { + err := genai.APIError{Code: 400, Message: "Invalid argument", Status: "INVALID_ARGUMENT"} + require.False(t, IsRetryableModelError(err)) +} + func TestIsRetryableModelErrorAnthropic429(t *testing.T) { err := &anthropic.Error{StatusCode: 429} assert.True(t, IsRetryableModelError(err))