Skip to content

Fix: streaming response reference mismatch with answer#13835

Open
Lntanohuang wants to merge 2 commits intoinfiniflow:mainfrom
Lntanohuang:fix/stream-reference-mismatch
Open

Fix: streaming response reference mismatch with answer#13835
Lntanohuang wants to merge 2 commits intoinfiniflow:mainfrom
Lntanohuang:fix/stream-reference-mismatch

Conversation

@Lntanohuang
Copy link
Copy Markdown
Contributor

Summary

Fixes #13828 — streaming response references are mismatched with the answer, while chat history references are correct.

Root Cause

In async_chat() (api/db/services/dialog_service.py), the streaming code path:

  1. Sends intermediate SSE events with reference: {} (empty) — expected, since references are computed after the full answer is available.
  2. After streaming completes, calls decorate_answer() which:
    • Inserts citation markers (##N$$) into the answer text
    • Filters doc_aggs based on which chunks were actually cited
    • Returns the decorated answer with matching references
  3. Bug: The final SSE event then blanks the answer (final["answer"] = ""), discarding the citation-processed text while keeping the citation-filtered references.

This means the client accumulates raw answer text (no citation markers) from delta events, but receives references filtered based on citation analysis it never sees — causing the mismatch reported in the issue.

Chat history is unaffected because structure_answer() correctly persists conv.reference[-1] from the final event's reference data.

Fix

Remove final["answer"] = "" so the final SSE event includes the full decorated answer (with citation markers) alongside its matching references. This is consistent with the non-streaming code path behavior.

Behavior Change

Previously, the final streaming SSE event had answer: "". After this fix, it contains the complete citation-processed answer. Clients that accumulate delta text should use the final event's answer as the authoritative text (replacing accumulated deltas), which is the same pattern used in the non-streaming response.

Test plan

  • Call POST /api/v1/chats/{chat_id}/completions with stream=true and citation enabled
  • Verify the final SSE event contains both the decorated answer and matching references
  • Verify the references in the streaming response match those in the chat history
  • Verify non-streaming (stream=false) behavior is unchanged

🤖 Generated with Claude Code

Asksksn and others added 2 commits March 28, 2026 18:30
…ence

In streaming mode, the final SSE event contains the correctly filtered
reference (from decorate_answer) but the answer field was blanked to "".
This caused a mismatch: the client accumulated raw answer text (without
citation markers) while the reference was filtered based on citation
analysis of the decorated answer. Chat history was unaffected because
conv.reference was correctly persisted from the final event.

By keeping the decorated answer in the final event, clients can replace
accumulated text with the citation-processed answer that matches the
reference, consistent with the non-streaming code path.

Closes infiniflow#13828

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…enError

When searching words like "cat", synonym expansion produces phrases such as
"big cat" and "computerized tomography". These are embedded in a single-quoted
SQL string for filter_fulltext(), but any single quote in the matching text
breaks sqlglot's tokenizer. Escape single quotes using the standard SQL
convention (' -> '') before interpolation.

Closes infiniflow#13823

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🐖api The modified files are located under directory 'api/apps/sdk' 🐞 bug Something isn't working, pull request that fix bug. labels Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐖api The modified files are located under directory 'api/apps/sdk' 🐞 bug Something isn't working, pull request that fix bug. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: /api/v1/chats/{chat_id}/completions stream returns wrong references, but chat history is correct

1 participant