fix(llm): recognize Gemini 429/5xx as retryable via typed APIError path
This commit is contained in:
1 parent
9292731d79
commit
3da8fe8ed3
2 files changed
+32
-4
No files matched your search
+15
-4
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
anthropic "github.com/anthropics/anthropic-sdk-go"
|
anthropic "github.com/anthropics/anthropic-sdk-go"
|
||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
|
"google.golang.org/genai"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsRetryableModelError reports whether err is a transient capacity
|
// IsRetryableModelError reports whether err is a transient capacity
|
||||||
@@ -14,14 +15,17 @@ import (
|
|||||||
//
|
//
|
||||||
// - Anthropic 429 (rate limit) and 529 (overloaded)
|
// - Anthropic 429 (rate limit) and 529 (overloaded)
|
||||||
// - OpenAI 429 (rate limit) and 5xx (server / 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:
|
// Anything else (including 4xx auth/validation errors) returns false:
|
||||||
// retrying with a different model wouldn't fix the underlying problem.
|
// retrying with a different model wouldn't fix the underlying problem.
|
||||||
//
|
//
|
||||||
// Errors from local providers (Ollama, etc.) and Gemini are NOT
|
// Errors from local providers (Ollama, etc.) are NOT classified as
|
||||||
// classified as retryable here; their failure modes are different
|
// retryable here; their failure modes are different and a model swap
|
||||||
// and a model swap rarely helps. Add cases as new providers
|
// rarely helps. Add cases as new providers accumulate retry experience.
|
||||||
// accumulate retry experience.
|
|
||||||
func IsRetryableModelError(err error) bool {
|
func IsRetryableModelError(err error) bool {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return false
|
return false
|
||||||
@@ -47,6 +51,13 @@ func IsRetryableModelError(err error) bool {
|
|||||||
return openaiReqErr.HTTPStatusCode == 429 ||
|
return openaiReqErr.HTTPStatusCode == 429 ||
|
||||||
(openaiReqErr.HTTPStatusCode >= 500 && openaiReqErr.HTTPStatusCode < 600)
|
(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
|
// Last-ditch: substring match on common SDK error wrappers that
|
||||||
// don't unwrap cleanly to typed errors. Cheap belt-and-braces;
|
// don't unwrap cleanly to typed errors. Cheap belt-and-braces;
|
||||||
|
|||||||
@@ -9,8 +9,25 @@ import (
|
|||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"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) {
|
func TestIsRetryableModelErrorAnthropic429(t *testing.T) {
|
||||||
err := &anthropic.Error{StatusCode: 429}
|
err := &anthropic.Error{StatusCode: 429}
|
||||||
assert.True(t, IsRetryableModelError(err))
|
assert.True(t, IsRetryableModelError(err))
|
||||||
|
|||||||
Reference in new issue
Block a user