fix(providers): improve context overflow detection and classification
This commit is contained in:
parent
82c78e853b
commit
97dec16769
3 changed files with 22 additions and 8 deletions
|
|
@ -1949,6 +1949,7 @@ turnLoop:
|
||||||
|
|
||||||
isContextError := !isTimeoutError && (strings.Contains(errMsg, "context_length_exceeded") ||
|
isContextError := !isTimeoutError && (strings.Contains(errMsg, "context_length_exceeded") ||
|
||||||
strings.Contains(errMsg, "context window") ||
|
strings.Contains(errMsg, "context window") ||
|
||||||
|
strings.Contains(errMsg, "context_window") ||
|
||||||
strings.Contains(errMsg, "maximum context length") ||
|
strings.Contains(errMsg, "maximum context length") ||
|
||||||
strings.Contains(errMsg, "token limit") ||
|
strings.Contains(errMsg, "token limit") ||
|
||||||
strings.Contains(errMsg, "too many tokens") ||
|
strings.Contains(errMsg, "too many tokens") ||
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,15 @@ var (
|
||||||
substr("messages.1.content.1.tool_use.id"),
|
substr("messages.1.content.1.tool_use.id"),
|
||||||
substr("invalid request format"),
|
substr("invalid request format"),
|
||||||
}
|
}
|
||||||
|
contextOverflowPatterns = []errorPattern{
|
||||||
|
rxp(`context[_ ]?length[_ ]?exceeded`),
|
||||||
|
rxp(`context[_ ]?window[_ ]?exceeded`),
|
||||||
|
substr("maximum context length"),
|
||||||
|
substr("token limit"),
|
||||||
|
substr("too many tokens"),
|
||||||
|
substr("prompt is too long"),
|
||||||
|
substr("request too large"),
|
||||||
|
}
|
||||||
|
|
||||||
imageDimensionPatterns = []errorPattern{
|
imageDimensionPatterns = []errorPattern{
|
||||||
rxp(`image dimensions exceed max`),
|
rxp(`image dimensions exceed max`),
|
||||||
|
|
@ -201,6 +210,9 @@ func classifyByMessage(msg string) FailoverReason {
|
||||||
if matchesAny(msg, formatPatterns) {
|
if matchesAny(msg, formatPatterns) {
|
||||||
return FailoverFormat
|
return FailoverFormat
|
||||||
}
|
}
|
||||||
|
if matchesAny(msg, contextOverflowPatterns) {
|
||||||
|
return FailoverContextOverflow
|
||||||
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,13 +71,14 @@ type NativeSearchCapable interface {
|
||||||
type FailoverReason string
|
type FailoverReason string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FailoverAuth FailoverReason = "auth"
|
FailoverAuth FailoverReason = "auth"
|
||||||
FailoverRateLimit FailoverReason = "rate_limit"
|
FailoverRateLimit FailoverReason = "rate_limit"
|
||||||
FailoverBilling FailoverReason = "billing"
|
FailoverBilling FailoverReason = "billing"
|
||||||
FailoverTimeout FailoverReason = "timeout"
|
FailoverTimeout FailoverReason = "timeout"
|
||||||
FailoverFormat FailoverReason = "format"
|
FailoverFormat FailoverReason = "format"
|
||||||
FailoverOverloaded FailoverReason = "overloaded"
|
FailoverContextOverflow FailoverReason = "context_overflow"
|
||||||
FailoverUnknown FailoverReason = "unknown"
|
FailoverOverloaded FailoverReason = "overloaded"
|
||||||
|
FailoverUnknown FailoverReason = "unknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FailoverError wraps an LLM provider error with classification metadata.
|
// FailoverError wraps an LLM provider error with classification metadata.
|
||||||
|
|
@ -101,7 +102,7 @@ func (e *FailoverError) Unwrap() error {
|
||||||
// IsRetriable returns true if this error should trigger fallback to next candidate.
|
// IsRetriable returns true if this error should trigger fallback to next candidate.
|
||||||
// Non-retriable: Format errors (bad request structure, image dimension/size).
|
// Non-retriable: Format errors (bad request structure, image dimension/size).
|
||||||
func (e *FailoverError) IsRetriable() bool {
|
func (e *FailoverError) IsRetriable() bool {
|
||||||
return e.Reason != FailoverFormat
|
return e.Reason != FailoverFormat && e.Reason != FailoverContextOverflow
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModelConfig holds primary model and fallback list.
|
// ModelConfig holds primary model and fallback list.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue