feat(chain): fail over on empty/degenerate responses
A failover chain previously treated a successful-but-empty completion (no content parts and no tool calls — a "stop with nothing") as a valid result and returned it. The agent loop then ended the run with empty output, and the configured backup models were never tried because no error was raised. This let a single flaky model silently terminate an agent/skill run with no answer (observed in the wild with ollama-cloud/glm-5.2 returning empty completions right after a large tool/think turn). - Add llm.ErrEmptyResponse (classified transient) and Response.IsEmpty(): true only when there are no tool calls and no meaningful content (no parts, or whitespace-only text). A media/image part counts as content, so image-only responses are NOT empty. - chain.Generate converts an empty completion into ErrEmptyResponse so the chain fails over to the next target. Unlike an ordinary transient it is NOT retried on the same target (the model just produced it; these calls are expensive) — the chain penalizes health (so a persistently-empty target benches) and advances immediately. - When every target returns empty the call fails with ErrChainExhausted joined to ErrEmptyResponse — a visible error instead of a hollow success. Single-element chains therefore also surface empties as errors. Stream path is unchanged (can't inspect content before the consumer reads it). Tests: Response.IsEmpty table; chain fails over past an empty head; all-empty chain returns ErrChainExhausted/ErrEmptyResponse; repeated empties bench the target across requests. Full suite green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -33,6 +33,15 @@ var ErrModelNotFound = errors.New("model not found")
|
||||
// well be able to serve the request.
|
||||
var ErrUnsupported = errors.New("request unsupported by target")
|
||||
|
||||
// ErrEmptyResponse marks a provider call that returned, without error, no
|
||||
// usable output — no content parts and no tool calls (a "stop with
|
||||
// nothing"). It is never a valid completion: an agent step needs either a
|
||||
// final answer or a tool call, and a one-shot Generate needs content. A
|
||||
// chain treats it as a per-target failure and fails over to the next
|
||||
// element (benching the empty target) so a single flaky model cannot
|
||||
// silently end a run with nothing. See chain.Generate / Response.IsEmpty.
|
||||
var ErrEmptyResponse = errors.New("model returned an empty response")
|
||||
|
||||
// APIError is a structured provider error carrying enough context to
|
||||
// classify it and to debug it.
|
||||
type APIError struct {
|
||||
@@ -106,6 +115,11 @@ func Classify(err error) ErrorClass {
|
||||
if errors.Is(err, ErrModelNotFound) || errors.Is(err, ErrUnsupported) {
|
||||
return ClassPermanent
|
||||
}
|
||||
if errors.Is(err, ErrEmptyResponse) {
|
||||
// An empty completion may be a one-off provider hiccup; another
|
||||
// target (or, rarely, a retry) can produce real output.
|
||||
return ClassTransient
|
||||
}
|
||||
if errors.Is(err, syscall.ECONNREFUSED) || errors.Is(err, syscall.ECONNRESET) {
|
||||
return ClassTransient
|
||||
}
|
||||
|
||||
@@ -86,3 +86,29 @@ func (r *Response) Text() string {
|
||||
func (r *Response) Message() Message {
|
||||
return Message{Role: RoleAssistant, Parts: r.Parts, ToolCalls: r.ToolCalls}
|
||||
}
|
||||
|
||||
// IsEmpty reports whether the response carries no usable output: no tool
|
||||
// calls and no meaningful content (no parts at all, or only whitespace
|
||||
// text). A media/image part — or any non-text part — counts as content, so
|
||||
// an image-only response is NOT empty. A "stop with nothing" like this is
|
||||
// never a valid completion for an agent step or a Generate call; failover
|
||||
// chains treat it as a per-target failure (see ErrEmptyResponse).
|
||||
func (r *Response) IsEmpty() bool {
|
||||
if r == nil {
|
||||
return true
|
||||
}
|
||||
if len(r.ToolCalls) > 0 {
|
||||
return false
|
||||
}
|
||||
for _, p := range r.Parts {
|
||||
if t, ok := p.(TextPart); ok {
|
||||
if strings.TrimSpace(t.Text) != "" {
|
||||
return false
|
||||
}
|
||||
continue
|
||||
}
|
||||
// Any non-text part (image/media) is meaningful output.
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package llm
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestResponseIsEmpty(t *testing.T) {
|
||||
img := ImagePart{MIME: "image/png", Data: []byte{0x89, 0x50, 0x4e, 0x47}}
|
||||
tests := []struct {
|
||||
name string
|
||||
resp *Response
|
||||
want bool
|
||||
}{
|
||||
{"nil response", nil, true},
|
||||
{"no parts, no tool calls", &Response{FinishReason: FinishStop}, true},
|
||||
{"single empty text part", &Response{Parts: []Part{Text("")}}, true},
|
||||
{"whitespace-only text", &Response{Parts: []Part{Text(" \n\t ")}}, true},
|
||||
{"real text", &Response{Parts: []Part{Text("hello")}}, false},
|
||||
{"tool call, no text", &Response{ToolCalls: []ToolCall{{ID: "1", Name: "x"}}}, false},
|
||||
{"image only", &Response{Parts: []Part{img}}, false},
|
||||
{"empty text but a tool call", &Response{Parts: []Part{Text("")}, ToolCalls: []ToolCall{{ID: "1", Name: "x"}}}, false},
|
||||
{"whitespace text plus an image", &Response{Parts: []Part{Text(" "), img}}, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.resp.IsEmpty(); got != tt.want {
|
||||
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user