Repository navigation
Fix: streaming response reference mismatch with answer - #13835
Lntanohuang wants to merge 1 commit into
Conversation
async_ask() calls decorate_answer() which inserts citation markers and filters doc_aggs to the cited documents, then immediately overwrites final["answer"] with "", discarding the decorated text. Clients receive the filtered references but the raw undecorated answer text from the streamed deltas, so citations never match the displayed content. Removing the blank assignment lets the decorated answer reach the client as intended. The same issue in async_chat() is addressed in infiniflow#13835.
|
Appreciate your help on this. Could you please test on your local environment and then verify your fix? If testing evidence could be uploaded, that will be of great help to fix/close this bug. |
Remove `final["answer"] = ""` in both async_chat() and async_ask() so the final streaming event includes the citation-decorated answer alongside matching references, fixing the mismatch reported in infiniflow#13828.
ca84447 to
4cf7aa8
Compare
📝 WalkthroughWalkthroughA bug fix that removes explicit empty string assignments ( Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|
Thanks for the review! I've updated this PR with the following changes:
@edenfunf I noticed your PR #13917 addresses the same I'll follow up with local testing evidence shortly. |
|
I'll have the local testing evidence uploaded within the next day. Stay tuned! |
|
@Lntanohuang May I know if the testing goes well? |
…13917) ## What's the problem Both `async_chat()` and `async_ask()` call `decorate_answer()` to build the final SSE payload — it inserts citation markers (`##N$$`) into the answer text and prunes `doc_aggs` to only the cited documents. Immediately after, both functions overwrite `final["answer"]` with `""`: ```python # async_chat(), line ~774 (issue #13828) final = decorate_answer(thought + full_answer) final["final"] = True final["audio_binary"] = None final["answer"] = "" # discards decorated text yield final # async_ask(), line ~1444 (same bug, different path) final = decorate_answer(full_answer) final["final"] = True final["answer"] = "" # discards decorated text yield final ``` The client receives filtered references (built for a citation-decorated answer it never sees) while displaying the raw, undecorated streaming text. Citations can never match. ## Root cause `final["answer"] = ""` was left over from an earlier design where clients were meant to reconstruct the full answer purely from delta events. Once `decorate_answer()` started placing citation markers, this blank-out broke the contract: the final event is where the decorated answer should land. ## Fix Remove the two blank-override lines — one in `async_chat()`, one in `async_ask()`: ```diff - final["answer"] = "" ``` `decorate_answer()` already sets `final["answer"]` to the correct decorated string; there is nothing to override. ## Relation to #13828 Issue #13828 and PR #13835 identify the bug in `async_chat()`. This PR absorbs that fix and also corrects the identical pattern in `async_ask()` (used by the `/retrieval` route in `chat_api.py`), which PR #13835 does not touch. ## Regression test Added `test/unit_test/api/db/services/test_dialog_service_final_answer.py` with three tests: | Test | Purpose | |------|---------| | `test_buggy_pattern_drops_answer` | Documents the old behaviour: blank-override empties the final answer | | `test_fixed_pattern_preserves_decorated_answer` | Core invariant: final event carries the decorated text from `decorate_answer()` | | `test_final_event_reference_matches_decorated_result` | Citation markers in the answer must match the pruned `doc_aggs` in the same event | Local run result: ``` test_dialog_service_final_answer.py::test_buggy_pattern_drops_answer PASSED test_dialog_service_final_answer.py::test_fixed_pattern_preserves_decorated_answer PASSED test_dialog_service_final_answer.py::test_final_event_reference_matches_decorated_result PASSED 3 passed in 0.04s ``` `ruff check` passes with no issues on all changed files. --------- Co-authored-by: edenfunf <[email protected]> Co-authored-by: Yingfeng <[email protected]>
|
#13917 has been merged which contains the fix as well. Thus close this one. |
|
Sorry for the delay — I've been swamped with work and feeling under the weather these past few days, so I couldn't follow up in time. Glad to see this got resolved through #13917. Thanks @edenfunf for picking it up and @xugangqiang for coordinating! |
…nfiniflow#13917) ## What's the problem Both `async_chat()` and `async_ask()` call `decorate_answer()` to build the final SSE payload — it inserts citation markers (`##N$$`) into the answer text and prunes `doc_aggs` to only the cited documents. Immediately after, both functions overwrite `final["answer"]` with `""`: ```python # async_chat(), line ~774 (issue infiniflow#13828) final = decorate_answer(thought + full_answer) final["final"] = True final["audio_binary"] = None final["answer"] = "" # discards decorated text yield final # async_ask(), line ~1444 (same bug, different path) final = decorate_answer(full_answer) final["final"] = True final["answer"] = "" # discards decorated text yield final ``` The client receives filtered references (built for a citation-decorated answer it never sees) while displaying the raw, undecorated streaming text. Citations can never match. ## Root cause `final["answer"] = ""` was left over from an earlier design where clients were meant to reconstruct the full answer purely from delta events. Once `decorate_answer()` started placing citation markers, this blank-out broke the contract: the final event is where the decorated answer should land. ## Fix Remove the two blank-override lines — one in `async_chat()`, one in `async_ask()`: ```diff - final["answer"] = "" ``` `decorate_answer()` already sets `final["answer"]` to the correct decorated string; there is nothing to override. ## Relation to infiniflow#13828 Issue infiniflow#13828 and PR infiniflow#13835 identify the bug in `async_chat()`. This PR absorbs that fix and also corrects the identical pattern in `async_ask()` (used by the `/retrieval` route in `chat_api.py`), which PR infiniflow#13835 does not touch. ## Regression test Added `test/unit_test/api/db/services/test_dialog_service_final_answer.py` with three tests: | Test | Purpose | |------|---------| | `test_buggy_pattern_drops_answer` | Documents the old behaviour: blank-override empties the final answer | | `test_fixed_pattern_preserves_decorated_answer` | Core invariant: final event carries the decorated text from `decorate_answer()` | | `test_final_event_reference_matches_decorated_result` | Citation markers in the answer must match the pruned `doc_aggs` in the same event | Local run result: ``` test_dialog_service_final_answer.py::test_buggy_pattern_drops_answer PASSED test_dialog_service_final_answer.py::test_fixed_pattern_preserves_decorated_answer PASSED test_dialog_service_final_answer.py::test_final_event_reference_matches_decorated_result PASSED 3 passed in 0.04s ``` `ruff check` passes with no issues on all changed files. --------- Co-authored-by: edenfunf <[email protected]> Co-authored-by: Yingfeng <[email protected]>
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:reference: {}(empty) — expected, since references are computed after the full answer is available.decorate_answer()which:##N$$) into the answer textdoc_aggsbased on which chunks were actually citedfinal["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 persistsconv.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
POST /api/v1/chats/{chat_id}/completionswithstream=trueand citation enabledstream=false) behavior is unchanged🤖 Generated with Claude Code
Update: Also fixes the same issue in
async_ask(). Theinfinity_conn.pyescaping fix has been moved to a separate PR (#14053) to keep this one focused on the streaming reference mismatch.