Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agentic-rag): fail fast when the chat model cannot answer (#19850)

qinling0210df13f5b409a6
diff --git a/internal/entity/models/llm.go b/internal/entity/models/llm.go
index 72a551b8a4..7f54dae715 100644
--- a/internal/entity/models/llm.go
+++ b/internal/entity/models/llm.go
@@ -434,7 +434,13 @@ func (m *EinoChatModel) generateOnce(ctx context.Context, cm *ChatModel, msgs []
zap.String("skeleton", describeInternalMessages(internal)))
resp, err := cm.ModelDriver.ChatWithMessages(ctx, *cm.ModelName, internal, cm.APIConfig, chatCfg, nil)
if err != nil {
- return nil, fmt.Errorf("models: EinoChatModel.Generate(%s): %w", *cm.ModelName, err)
+ // The chat paths surface this to the user as `**ERROR**: <err>`
+ // (errorAnswerText in agentic-rag/agentic_rag_graph.go, chat_pipeline.go),
+ // so lead with the model name and keep the provider's sentence: the old
+ // "models: EinoChatModel.Generate(MiniMax-M3): …" prefix was internal
+ // noise around the one fact that mattered. On a failover chain this is
+ // the LAST model tried: Generate reports its lastErr as-is.
+ return nil, fmt.Errorf("%s: %w", *cm.ModelName, err)
}
// Record the per-call token usage so the canvas-level aggregator (and
// Langfuse) can compute the run total. Mirrors Python's
@@ -870,6 +876,15 @@ func (m *EinoChatModel) Stream(ctx context.Context, msgs []*schema.Message, opts
attemptCfg.ToolCallsResult = nil
attemptCfg.UsageResult = nil
err := cm.ModelDriver.ChatStreamlyWithSender(ctx, *cm.ModelName, internalMessage, cm.APIConfig, attemptCfg, nil, sender)
+ if err != nil {
+ // Same user-visible shape as generateOnce's error: lead with the
+ // model name and keep the provider's own sentence. Tagging it HERE
+ // rather than at the sink keeps the name honest — the sweep may
+ // forward this as lastErr, and the deltas-already-sent branch sends
+ // it as-is. cacheableSweepFailure classifies via errors.As, so the
+ // %w wrapping does not change its verdict.
+ err = fmt.Errorf("%s: %w", *cm.ModelName, err)
+ }
if err == nil {
// Streamed turns report their token usage through the config
// (stream_options.include_usage), not through the nil modelUsage
diff --git a/internal/entity/models/llm_test.go b/internal/entity/models/llm_test.go
index b437e5407e..c103217c6f 100644
--- a/internal/entity/models/llm_test.go
+++ b/internal/entity/models/llm_test.go
@@ -54,6 +54,54 @@ func TestEinoChatModelAllowsFinalAnswerAfterToolResult(t *testing.T) {
}
}
+// TestEinoChatModelErrorNamesTheModel pins the user-visible shape of a provider
+// failure on BOTH paths: the chat paths render it as `**ERROR**: <err>`, so the
+// message must lead with the model the user configured and keep the provider's
+// own words — not the wrapper's internal "models: EinoChatModel.Generate(…)"
+// path, which put the cause after noise. The stream path is covered as well
+// because a plain (tool-less) turn reports its failures through it, not through
+// Generate.
+func TestEinoChatModelErrorNamesTheModel(t *testing.T) {
+ sentinel := errors.New("minimax API error: insufficient balance")
+ modelName := "MiniMax-M3"
+ model := NewEinoChatModel(
+ NewChatModel(&failingDriver{captureToolDriver: &captureToolDriver{}, err: sentinel}, &modelName, &APIConfig{}), nil)
+ msgs := []*schema.Message{schema.UserMessage("hi")}
+
+ cases := []struct {
+ path string
+ fail func() error
+ }{
+ {"Generate", func() error {
+ _, err := model.Generate(t.Context(), msgs)
+ return err
+ }},
+ {"Stream", func() error {
+ stream, err := model.Stream(t.Context(), msgs)
+ if err != nil {
+ return err
+ }
+ _, recvErr := stream.Recv()
+ return recvErr
+ }},
+ }
+ for _, tc := range cases {
+ t.Run(tc.path, func(t *testing.T) {
+ err := tc.fail()
+ if err == nil {
+ t.Fatalf("%s: want the provider's error", tc.path)
+ }
+ if got, want := err.Error(), modelName+": "+sentinel.Error(); got != want {
+ t.Fatalf("error = %q, want %q", got, want)
+ }
+ // The provider's error stays wrapped so callers can still inspect it.
+ if !errors.Is(err, sentinel) {
+ t.Errorf("errors.Is(err, sentinel) = false, got %v", err)
+ }
+ })
+ }
+}
+
// TestEinoChatModelAppliesExplicitToolChoice pins that WithToolChoice actually
// reaches the driver's configuration (its doc promises exactly that): a keyword
// choice travels as the plain string with no object value, a named tool travels
@@ -414,6 +462,21 @@ type streamSentinelDriver struct {
*captureToolDriver
}
+// failingDriver rejects every chat call with a fixed error, standing in for a
+// provider that refuses the request (an exhausted quota, bad credentials).
+type failingDriver struct {
+ *captureToolDriver
+ err error
+}
+
+func (d *failingDriver) ChatWithMessages(context.Context, string, []Message, *APIConfig, *ChatConfig, *common.ModelUsage) (*ChatResponse, error) {
+ return nil, d.err
+}
+
+func (d *failingDriver) ChatStreamlyWithSender(context.Context, string, []Message, *APIConfig, *ChatConfig, *common.ModelUsage, func(*string, *string) error) error {
+ return d.err
+}
+
func (d *streamSentinelDriver) ChatStreamlyWithSender(ctx context.Context, _ string, _ []Message, _ *APIConfig, _ *ChatConfig, _ *common.ModelUsage, sender func(*string, *string) error) error {
answer := "answer"
if err := sender(&answer, nil); err != nil {
diff --git a/internal/rag/agentic-rag/agentic_rag.go b/internal/rag/agentic-rag/agentic_rag.go
index b569de72c8..f3acc73471 100644
--- a/internal/rag/agentic-rag/agentic_rag.go
+++ b/internal/rag/agentic-rag/agentic_rag.go
@@ -30,6 +30,7 @@ package agentic_rag
import (
"context"
+ "errors"
"fmt"
"log"
"regexp"
@@ -1053,6 +1054,48 @@ func (dbDocTenantResolver) ResolveDocTenants(ctx context.Context, docIDs []strin
return out, nil
}
+// chatModelProbeTimeout bounds the pre-flight completion. An unreachable or
+// out-of-credit provider answers in milliseconds; a slow one must not hold the
+// request before the think block has even opened.
+const chatModelProbeTimeout = 20 * time.Second
+
+// probePrompt is the one-token question the pre-flight sends. It is a constant
+// so the test models recognise the probe without a bare "ping" literal.
+const probePrompt = "ping"
+
+// errProbeTimeout marks a probe that exhausted ITS OWN budget while the request
+// context was still alive — a slow model, not a dead provider.
+var errProbeTimeout = errors.New("chat model pre-flight timed out")
+
+// probeChatModel makes the smallest possible completion to learn whether the
+// run's chat model is usable at all. It is a DETECTION call: the reply is
+// discarded and only its error matters. A nil model reports no error — the
+// existing "no model configured" fallbacks own that case.
+//
+// A probe that only timed out comes back as errProbeTimeout: a slow model is
+// not a dead one, and the caller must not turn a long first token into an
+// outage.
+func probeChatModel(ctx context.Context, deps RAGTools) error {
+ return probeChatModelWithin(ctx, deps, chatModelProbeTimeout)
+}
+
+// probeChatModelWithin is probeChatModel with an injectable budget, so a test
+// can exercise the timeout path without waiting out the real 20s.
+func probeChatModelWithin(ctx context.Context, deps RAGTools, budget time.Duration) error {
+ if deps.Model == nil {
+ return nil
+ }
+ probeCtx, cancel := context.WithTimeout(ctx, budget)
+ defer cancel()
+ if _, err := deps.Model.Complete(probeCtx, []schema.Message{*schema.UserMessage(probePrompt)}, nil); err != nil {
+ if ctx.Err() == nil && probeCtx.Err() != nil {
+ return fmt.Errorf("%w after %s: %v", errProbeTimeout, budget, err)
+ }
+ return err
+ }
+ return nil
+}
+
func Rag(ctx context.Context, deps RAGTools, req runtime.RunRequest) *RunResponse {
logger := deps.Logger
if logger == nil {
@@ -1079,6 +1122,44 @@ func Rag(ctx context.Context, deps RAGTools, req runtime.RunRequest) *RunRespons
// tool beneath reads it (runtime.StepsFrom) and reports its own step, so the
// per-request trace is assembled from the stages that actually run.
ctx = runtime.WithSteps(ctx, deps.Steps)
+
+ // LLM pre-flight, before anything is announced. The agentic modes cannot
+ // answer without a working model, and a dead provider used to surface only at
+ // COMPOSE time — after the think block had narrated a research run that could
+ // never finish (measured: MiniMax "insufficient balance" burned seconds of
+ // narration plus a series of degraded-failure lines before the fallback
+ // answer). One minimal call answers "is the provider usable at all"; when it
+ // fails the run reports the provider's own message in the classic
+ // `**ERROR**: …` shape and returns BEFORE reporting a step or starting the
+ // tool, so no think block opens and the user sees what a naive-mode failure
+ // shows.
+ //
+ // It runs ONCE per turn, at the turn's entry: Rag is called once per user turn
+ // (the outer react loop's later `rag` tool calls re-enter the research graph
+ // directly), so there is no second round to re-probe and no per-round state to
+ // keep. It is UNCONDITIONAL: nothing between the request and this call decides
+ // whether the model is checked — a provider that cannot answer must be
+ // reported, never tolerated.
+ //
+ // The probe deliberately runs BEFORE wrapModelForStats: it is a health check,
+ // not a phase's work, so its call is kept out of llm_stats.
+ if spec.Label != "naive" {
+ err := probeChatModel(ctx, deps)
+ if errors.Is(err, errProbeTimeout) {
+ // The probe's own budget ran out, not the provider's answer: let
+ // the run proceed and fail (if it must) on its real budget.
+ logger.Printf("[Agentic RAG] %v; continuing without a pre-flight verdict", err)
+ } else if err != nil {
+ // DEVELOPER LOG ONLY — no step is reported: the whole point is that
+ // the user gets the error instead of a think block narrating work
+ // that cannot happen. No GraphFailed either: that flag means the
+ // research graph itself errored (RunResponse.GraphFailed) and the
+ // graph never started; the pipeline keys on the `**ERROR**:` prefix.
+ logger.Printf("[Agentic RAG] chat model pre-flight failed: %v", err)
+ return &RunResponse{Answer: errorAnswerText(err), Mode: spec}
+ }
+ }
+
// The run's logger stays the DEVELOPER log — nothing intercepts it. The
// user-visible trace comes from the steps stages report themselves
// (deps.Steps), so a diagnostic line can never leak into the think block and
@@ -1086,7 +1167,9 @@ func Rag(ctx context.Context, deps RAGTools, req runtime.RunRequest) *RunRespons
//
// wraps the chat model in CountingChatModel(chat_mdl.clone,
// self.llm_stats). Go wraps the session model's invoker the same way, so
- // calls made through deps.Model are counted per phase too.
+ // calls made through deps.Model are counted per phase too. The pre-flight
+ // above probes the UNWRAPPED model on purpose, so a health check is never
+ // counted as a phase's work.
if wrapped, ok := wrapModelForStats(deps.Model, stats); ok {
deps.Model = wrapped
}
@@ -1340,8 +1423,13 @@ func composeFinalAnswer(ctx context.Context, deps RAGTools, req runtime.RunReque
return
}
// A partially streamed answer must not be sent twice: tell the sink
- // to drop what it already forwarded and fall back to one shot.
- logger.Printf("[Agentic RAG] streaming compose failed (%v); falling back to a single call", err)
+ // to drop what it already forwarded and fall back to one shot. The
+ // think block reports the provider's own error (bounded): on a dead
+ // provider this is the first failure a user would otherwise never
+ // see, and the one-shot retry below fails the same way.
+ runtime.StepsFrom(ctx).StageLineDetail(logger, "Agentic RAG",
+ "Streaming generation failed; falling back to a single call: "+providerErrorSummary(err),
+ fmt.Sprintf("streaming compose failed (%v); falling back to a single call", err))
deps.AnswerSink.reset()
}
}
@@ -1359,7 +1447,11 @@ func composeFinalAnswer(ctx context.Context, deps RAGTools, req runtime.RunReque
// composed text itself predates the flag.
resp.Partial = true
}
- if deps.AnswerSink != nil {
+ // A FAILED compose is a terminal message, not a stream: the pipeline carries
+ // it in the final result, exactly how the classic path delivers its own
+ // `**ERROR**: …` answer. Streaming it here as well made the client show the
+ // text twice — once as an answer delta, once in the final.
+ if deps.AnswerSink != nil && !res.Failed {
deps.AnswerSink.deliver(res.Answer, false)
}
}
diff --git a/internal/rag/agentic-rag/agentic_rag_graph.go b/internal/rag/agentic-rag/agentic_rag_graph.go
index 196c9815b9..bfb8416f8e 100644
--- a/internal/rag/agentic-rag/agentic_rag_graph.go
+++ b/internal/rag/agentic-rag/agentic_rag_graph.go
@@ -1981,6 +1981,27 @@ type AnswerResult struct {
Failed bool
}
+// providerErrorSummary renders a provider failure as ONE bounded line for the
+// think block: whitespace collapsed (a model client's error can embed newlines
+// and a whole JSON body) and cut at providerErrorSummaryMax. The developer log
+// keeps the error verbatim — this is the sentence a user reads while waiting.
+func providerErrorSummary(err error) string {
+ if err == nil {
+ return ""
+ }
+ return truncateRunes(strings.Join(strings.Fields(err.Error()), " "), providerErrorSummaryMax)
+}
+
+// errorAnswerText renders a provider failure as the ANSWER, in the shape the
+// classic chat path uses for its own failures (`**ERROR**: …`): an agentic run
+// that cannot call the model must read to the user exactly like a naive-mode one
+// — bold ERROR plus the provider's own message — instead of Python's generic
+// "I'm sorry…" sentence, which hid the cause and made an exhausted quota look
+// like a RAG bug.
+func errorAnswerText(err error) string {
+ return "**ERROR**: " + providerErrorSummary(err)
+}
+
// ComposeAnswer: turn the gathered
// evidence into a grounded, cited answer.
//
diff --git a/internal/rag/agentic-rag/agentic_rag_graph_test.go b/internal/rag/agentic-rag/agentic_rag_graph_test.go
index 5336d222a8..657dea2453 100644
--- a/internal/rag/agentic-rag/agentic_rag_graph_test.go
+++ b/internal/rag/agentic-rag/agentic_rag_graph_test.go
@@ -46,6 +46,14 @@ func (m *scriptedModel) Complete(ctx context.Context, msgs []schema.Message, _ [
if ctx.Err() != nil {
return nil, ctx.Err()
}
+ // The pre-flight probe (probeChatModel) is infrastructure, not script: it is
+ // answered here and never consumes a scripted reply or enters `seen`, so the
+ // call order each test documents (formalize → planner → slot table → draft →
+ // SCA) stays what the test wrote. A test that wants the PROBE to fail uses
+ // errModel instead.
+ if len(msgs) == 1 && msgs[0].Role == schema.User && msgs[0].Content == probePrompt {
+ return &runtime.ModelReply{Content: "ok"}, nil
+ }
m.mu.Lock()
defer m.mu.Unlock()
m.seen = append(m.seen, msgs)
@@ -2135,6 +2143,13 @@ type fakeModel struct {
func (f *fakeModel) Complete(_ context.Context, msgs []schema.Message, _ []runtime.ToolSpec) (*runtime.ModelReply, error) {
f.messages = msgs
+ // The pre-flight probe (probeChatModel) opens every non-naive run with one
+ // minimal "ping". It is infrastructure, not script: answer it without
+ // consuming a scripted reply, so a test's reply sequence stays what the test
+ // wrote. A test that wants the PROBE itself to fail uses errModel instead.
+ if len(msgs) == 1 && msgs[0].Role == schema.User && msgs[0].Content == probePrompt {
+ return &runtime.ModelReply{Content: "ok"}, nil
+ }
if f.calls >= len(f.replies) {
return &runtime.ModelReply{Content: `<state>{"new_states": []}</state>`}, nil
}
@@ -2549,6 +2564,119 @@ func TestRunAgenticComposesFromResearchFindings(t *testing.T) {
}
}
+// errModel fails every completion, standing in for a provider outage (an
+// exhausted quota, a 5xx): the compose call gets no reply at all.
+type errModel struct{ err error }
+
+func (m errModel) Complete(context.Context, []schema.Message, []runtime.ToolSpec) (*runtime.ModelReply, error) {
+ return nil, m.err
+}
+
+// TestRagPreflightFailsFastWithoutThinking pins the pre-flight: a provider that
+// cannot answer AT ALL must be reported before the run narrates anything, in the
+// same `**ERROR**: …` shape naive mode uses. Without it the failure surfaced
+// only at compose time, after a whole research run's worth of think lines.
+func TestRagPreflightFailsFastWithoutThinking(t *testing.T) {
+ var think []string
+ var streamed []string
+ resp := Rag(context.Background(), RAGTools{
+ Model: errModel{err: fmt.Errorf("minimax API error: insufficient balance")},
+ Steps: runtime.StepReporter{Text: func(line string) { think = append(think, line) }},
+ AnswerSink: &AnswerSink{OnDelta: func(delta string, isThink bool) {
+ if !isThink {
+ streamed = append(streamed, delta)
+ }
+ }},
+ }, runtime.RunRequest{Question: "who?", ThinkingMode: "high"})
+
+ if !strings.HasPrefix(resp.Answer, "**ERROR**: ") || !strings.Contains(resp.Answer, "insufficient balance") {
+ t.Fatalf("answer = %q, want **ERROR** + the provider's message", resp.Answer)
+ }
+ if len(think) != 0 {
+ t.Errorf("think block must stay closed, got %v", think)
+ }
+ if len(streamed) != 0 {
+ t.Errorf("the error must not be streamed as an answer delta, got %v", streamed)
+ }
+}
+
+// blockingModel hangs until its context is done, standing in for a provider
+// that is slow to first token rather than dead.
+type blockingModel struct{}
+
+func (blockingModel) Complete(ctx context.Context, _ []schema.Message, _ []runtime.ToolSpec) (*runtime.ModelReply, error) {
+ <-ctx.Done()
+ return nil, ctx.Err()
+}
+
+// TestProbeSlowProviderIsNotAFailure pins the timeout split: a probe that only
+// ran out of ITS OWN budget must not read as a provider failure — a slow model
+// is not a dead one, and failing the whole run here would turn a long first
+// token into an outage.
+func TestProbeSlowProviderIsNotAFailure(t *testing.T) {
+ err := probeChatModelWithin(context.Background(), RAGTools{Model: blockingModel{}}, 5*time.Millisecond)
+ if !errors.Is(err, errProbeTimeout) {
+ t.Fatalf("err = %v, want errProbeTimeout (slow, not dead)", err)
+ }
+}
+
+// TestComposeFailureIsNotStreamedAsAnAnswerDelta pins the delivery rule: the
+// error answer goes out once, in the final result. Sending it through the sink
+// as well made the client render it twice (measured: 221 answer bytes for one
+// error + the research-status note, shown as two concatenated ERROR blocks).
+func TestComposeFailureIsNotStreamedAsAnAnswerDelta(t *testing.T) {
+ var streamed []string
+ deps := RAGTools{
+ Model: errModel{err: fmt.Errorf("minimax API error: insufficient balance")},
+ AnswerSink: &AnswerSink{OnDelta: func(delta string, isThink bool) { streamed = append(streamed, delta) }},
+ }
+ resp := &RunResponse{}
+ kb := &runtime.Kbinfos{Chunks: []map[string]any{{"chunk_id": "c1", "content": "body"}}}
+
+ composeFinalAnswer(context.Background(), deps, runtime.RunRequest{Question: "who?"}, kb, resp,
+ _LOG, false, false, "who?")
+
+ if !strings.HasPrefix(resp.Answer, "**ERROR**: ") {
+ t.Fatalf("answer = %q, want **ERROR** + the provider's message", resp.Answer)
+ }
+ if len(streamed) != 0 {
+ t.Errorf("a failed compose must not stream an answer delta, got %v", streamed)
+ }
+}
+
+// TestComposeFailureReportsProviderErrorInThinkBlock pins the Go-only deviation
+// from Python: when the compose call fails, the think block carries a bounded
+// summary of the provider's own error. Python prints the bare fallback and keeps
+// the cause in the log, which made an exhausted quota look like a RAG bug to the
+// user waiting for an answer.
+func TestComposeFailureReportsProviderErrorInThinkBlock(t *testing.T) {
+ var think []string
+ ctx := runtime.WithSteps(context.Background(), runtime.StepReporter{
+ Text: func(line string) { think = append(think, line) },
+ })
+ kb := &runtime.Kbinfos{Chunks: []map[string]any{{"chunk_id": "c1", "content": "body"}}}
+ mdl := errModel{err: fmt.Errorf("minimax API error: 已达到 Token Plan 用量上限\n请升级 Token Plan 套餐或购买积分补充用量")}
+
+ res := ComposeAnswerWith(ctx, AnswerDeps{Model: mdl}, kb, "who?", false, false, false)
+ if !res.Failed {
+ t.Fatalf("res = %+v, want Failed=true", res)
+ }
+ // The answer reads like a naive-mode failure: the classic `**ERROR**: …`
+ // shape carrying the provider's own words, not Python's generic sentence.
+ if !strings.HasPrefix(res.Answer, "**ERROR**: ") ||
+ !strings.Contains(res.Answer, "已达到 Token Plan 用量上限") {
+ t.Errorf("answer = %q, want **ERROR** + the provider's message", res.Answer)
+ }
+
+ joined := strings.Join(think, "")
+ if !strings.Contains(joined, "已达到 Token Plan 用量上限") {
+ t.Errorf("think block must carry the provider's error, got %q", joined)
+ }
+ if strings.Contains(joined, "\n") {
+ t.Errorf("the summary must be a single line, got %q", joined)
+ }
+}
+
// TestComposePublishesCiteChunkIDs pins the evidence contract the chat pipeline
// resolves citations against: the compose numbers the blocks 0-based (the client
// indexes reference.chunks with the marker's number, and the answer is streamed
diff --git a/internal/rag/agentic-rag/graph_compose.go b/internal/rag/agentic-rag/graph_compose.go
index 973d4eb082..d0a70f89f9 100644
--- a/internal/rag/agentic-rag/graph_compose.go
+++ b/internal/rag/agentic-rag/graph_compose.go
@@ -228,8 +228,13 @@ const (
naiveEvidenceChunkCap = 8
// naiveEvidenceCharCap caps each naive evidence chunk.
naiveEvidenceCharCap = 1500
- // answerErrorFallback is the stream-failure message.
- answerErrorFallback = "I'm sorry, I encountered an error while composing the answer."
+ // composeStage tags the terminal composition in the log and in the think
+ // block.
+ composeStage = "Composing the answer"
+ // providerErrorSummaryMax bounds the provider error a user reads inside the
+ // think block: the message can be a whole JSON body or a wall of retry
+ // advice, and the block is a narrative, not a log dump.
+ providerErrorSummaryMax = 300
// graphFailureFallback: last-resort message
// (run_agentic_rag), used only when the graph failed AND produced nothing.
graphFailureFallback = "I couldn't complete the search due to an internal error."
@@ -310,7 +315,7 @@ func ComposeAnswerWith(ctx context.Context, deps AnswerDeps, kb *runtime.Kbinfos
// repeating their numbers only made the block longer. The paths that have no
// [Finalize] — the low graph's last node, every fallback compose — keep it.
if !finalizeAnnounced(ctx) {
- step(ctx, logger, "Composing the answer", "Composing the answer from %s.",
+ step(ctx, logger, composeStage, "Composing the answer from %s.",
runtime.CountOf(len(chunks), "gathered passage"))
}
@@ -321,7 +326,7 @@ func ComposeAnswerWith(ctx context.Context, deps AnswerDeps, kb *runtime.Kbinfos
// _compose_answer_from_evidence: no_evidence = abstain or empty_result or not chunks.
noEvidence := abstain || emptyResult || len(chunks) == 0
if noEvidence && deps.EmptyResponse != "" {
- step(ctx, logger, "Composing the answer", "No supporting evidence was found, so the configured empty response is returned without calling the model.")
+ step(ctx, logger, composeStage, "No supporting evidence was found, so the configured empty response is returned without calling the model.")
return AnswerResult{Answer: deps.EmptyResponse, NoEvidence: true}
}
if deps.Model == nil {
@@ -368,8 +373,19 @@ func ComposeAnswerWith(ctx context.Context, deps AnswerDeps, kb *runtime.Kbinfos
*userMsg,
}, nil)
if err != nil {
- logger.Printf("[Composing the answer] composition failed: %v", err)
- return AnswerResult{Answer: answerErrorFallback, Failed: true}
+ // Deliberately NOT Python's bare "I'm sorry…" sentence. The ANSWER
+ // carries the provider's own message in the classic `**ERROR**: …` shape
+ // the rest of the chat pipeline uses for its failures, so an agentic
+ // failure reads exactly like a naive-mode one instead of hiding the
+ // cause — an exhausted quota or a provider 5xx used to look like a RAG
+ // bug. The think block gets the same text as a reasoning step, and the
+ // developer log line stays byte-for-byte what it was (the detail half of
+ // StageLineDetail).
+ summary := providerErrorSummary(err)
+ runtime.StepsFrom(ctx).StageLineDetail(logger, composeStage,
+ "Composing the answer failed: "+summary,
+ fmt.Sprintf("composition failed: %v", err))
+ return AnswerResult{Answer: errorAnswerText(err), Failed: true}
}
answer := citeAnchoredMembers(cleanAnswer(reply.Content), kb)
logComposeDone(logger, started, answer, len(chunks))
@@ -468,7 +484,7 @@ func ComposeAnswerStream(ctx context.Context, deps AnswerDeps, model runtime.Str
// streaming path has no separate `abstain` signal (Go threads only
// partial/emptyResult), so the abstain note term cannot fire here.
if !finalizeAnnounced(ctx) {
- step(ctx, logger, "Composing the answer", "Composing the answer from %s.",
+ step(ctx, logger, composeStage, "Composing the answer from %s.",
runtime.CountOf(len(chunks), "gathered passage"))
}
prompt := deps.answerPromptWithEvidence(kb, question, partial, noEvidence)