Skip to content

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

fix(agentic): preserve final answers after conversation reloads (#20718)

Sbaaoui Idrisse79c970b7c14
diff --git a/internal/service/agentic_pipeline.go b/internal/service/agentic_pipeline.go
index 4554f712d7..20ad17694e 100644
--- a/internal/service/agentic_pipeline.go
+++ b/internal/service/agentic_pipeline.go
@@ -19,6 +19,7 @@ package service
import (
"context"
"fmt"
+ "strings"
"time"
"github.com/cloudwego/eino/schema"
@@ -157,6 +158,7 @@ func (s *ChatPipelineService) agenticRag(
// StartToThink marker is emitted once and EndToThink fires when the
// agent's final answer arrives.
thinking := false
+ var reasoning strings.Builder
final, runErr := agentic_rag.Run(runCtx, agentic_rag.Input{
Model: model,
SynthModel: synth,
@@ -168,6 +170,7 @@ func (s *ChatPipelineService) agenticRag(
ToolCallCounts: toolCounts,
ToolCallErrors: toolErrors,
OnDelta: func(contentDelta, thinkingDelta string) {
+ reasoning.WriteString(thinkingDelta)
startToThink, endToThink := false, false
if thinkingDelta != "" {
if !thinking {
@@ -185,6 +188,9 @@ func (s *ChatPipelineService) agenticRag(
if startToThink {
emitResult(AsyncChatResult{Final: false, StartToThink: true})
}
+ if endToThink {
+ emitResult(AsyncChatResult{Final: false, EndToThink: true})
+ }
if contentDelta != "" || thinkingDelta != "" {
emitResult(AsyncChatResult{
Answer: contentDelta,
@@ -192,9 +198,6 @@ func (s *ChatPipelineService) agenticRag(
Final: false,
})
}
- if endToThink {
- emitResult(AsyncChatResult{Final: false, EndToThink: true})
- }
},
})
if thinking {
@@ -215,16 +218,17 @@ func (s *ChatPipelineService) agenticRag(
// payload, matching what the regular pipeline ships when quote is
// false. The answer text is still whatever the agent wrote.
answer := final
+ if reasoning.Len() > 0 {
+ answer = agentSessionMessageContent("\n\n"+final, reasoning.String())
+ }
reference := map[string]interface{}{}
if quote {
- reference, answer = s.buildAgenticReference(ctx, chat.TenantID, chatDatasetIDs(chat), final, registry)
- } else if len(agentic_rag.CitedIDsFromMarkers(final, registry)) > 0 || len(agentic_rag.ExtractCitedChunkIDs(final)) > 0 {
+ reference, answer = s.buildAgenticReference(ctx, chat.TenantID, chatDatasetIDs(chat), answer, registry)
+ } else if len(agentic_rag.CitedIDsFromMarkers(answer, registry)) > 0 || len(agentic_rag.ExtractCitedChunkIDs(answer)) > 0 {
common.InfoCtx(ctx, "agentic citations suppressed by quote=false")
}
- // The agentic answer is the run's whole output — <think> stretch included
- // — with its handles already compacted against `reference`, so the session
- // writer must persist THIS text and not the streamed deltas, which still
- // carry the raw handles the model emitted.
+ // Persist the same thinking and final answer the stream renders, with
+ // the final answer's citation handles compacted against its reference.
emitResult(AsyncChatResult{
Answer: answer,
Reference: reference,
diff --git a/internal/service/chat_session.go b/internal/service/chat_session.go
index adafdecb2b..c15b893445 100644
--- a/internal/service/chat_session.go
+++ b/internal/service/chat_session.go
@@ -1411,6 +1411,10 @@ func (s *ChatSessionService) ChatCompletions(
s.updateSessionMessages(ctx, session, s.getSessionMessagesAsSlice(session), reference)
}
}
+ if result.AnswerIsAuthoritative {
+ fullAnswer.Reset()
+ fullAnswer.WriteString(result.Answer)
+ }
}
if legacy {
if result.Final {
diff --git a/internal/service/openai_chat.go b/internal/service/openai_chat.go
index f81846cfc5..a181b05a50 100644
--- a/internal/service/openai_chat.go
+++ b/internal/service/openai_chat.go
@@ -373,6 +373,9 @@ func (s *OpenAIChatService) OpenAIChatCompletions(c *gin.Context, userID, chatID
if result.Final {
finalContent := strings.TrimSpace(result.Answer)
+ if result.AnswerIsAuthoritative {
+ finalContent = strings.TrimSpace(ExtractVisibleAnswer(result.Answer))
+ }
fullContent = finalContent
finalReference = []FormattedChunk{}
if ref, ok := result.Reference["chunks"]; ok {
@@ -381,7 +384,7 @@ func (s *OpenAIChatService) OpenAIChatCompletions(c *gin.Context, userID, chatID
}
}
s.enrichChunksWithDocumentMetadata(ctx, finalReference, dialog.TenantID, openaiReq.IncludeRefMetadata, openaiReq.MetadataFields)
- completionTok = tokenizer.NumTokensFromString(result.Answer)
+ completionTok = tokenizer.NumTokensFromString(finalContent)
events <- OpenAIStreamEvent{
Kind: OpenAIEventFinal,
FinalAnswer: finalContent,
@@ -450,6 +453,9 @@ func (s *OpenAIChatService) OpenAIChatCompletions(c *gin.Context, userID, chatID
}
content := strings.TrimSpace(finalResult.Answer)
+ if finalResult.AnswerIsAuthoritative {
+ content = strings.TrimSpace(ExtractVisibleAnswer(finalResult.Answer))
+ }
completionTokens := tokenizer.NumTokensFromString(content)
resp := &OpenAICompletionResponse{
Model: openaiReq.Model,