Skip to content

Fix duplicate references + runaway citation tooltips in RAG answers#26

Merged
lfnothias merged 1 commit into
mainfrom
fix-rag-citation-degeneration
Jun 22, 2026
Merged

Fix duplicate references + runaway citation tooltips in RAG answers#26
lfnothias merged 1 commit into
mainfrom
fix-rag-citation-degeneration

Conversation

@lfnothias

Copy link
Copy Markdown
Collaborator

Summary

Fixes two compounding bugs that made small models (e.g. gpt-4o-mini) emit a duplicate, partly-fabricated references block with a degenerate author tooltip — a citation title attribute that looped "H. " ~749 times (2,247 chars).

Prompt (get_system_prompt)

  • The citation title attribute previously had to hold the full author list (<All authors>. <Year>. <Title>. <Journal>.) — which small models loop on. Shortened to Title. Year. Journal. (no author list; the grounded reference list keeps the authors). This removes the loop trigger and fixes the live stream (the model never generates it).
  • Added an explicit instruction not to write a "References"/"Bibliography"/"Sources" section — the backend already appends the authoritative, grounded list, so the model's self-made one was a duplicate (and a source of fabricated entries).

Defensive sanitizer (collapse_runaway_repeats)

  • Collapses any short unit repeated 8+ times in a row, applied to the assistant answer before it is persisted — belt-and-suspenders for the rare case a model loops anyway.

Verification

  • Re-ran the triggering condition on a real multi-author paper (nothias-hdr): one ## References section, no runaway repetition, tooltip = "Feature-based Molecular Networking in the GNPS Analysis Environment. 2019." (74 chars vs the old 2,247).
  • Sanitizer unit-checked on the real degenerate answer: 6219 → 3979 chars in ~2 ms; normal prose, numbered lists, and table separators untouched.
  • 138 passed across prompt/citation/reference/route unit tests locally.

Note: the test CI job fails on a pre-existing test_web_app_routes.py issue already present on main (the CI "safe subset" sandbox registers no API routers); it passes locally (30/30) and is unrelated to this change.

🤖 Generated with Claude Code

Two compounding bugs made small models (e.g. gpt-4o-mini) emit a duplicate,
partly-fabricated references block with a degenerate author tooltip
("H. H. H. …" repeated hundreds of times):

- get_system_prompt required every citation's `title` attribute to hold the
  FULL author list, which small models loop on. Shorten it to
  `Title. Year. Journal.` (no author list — the grounded reference list keeps
  the authors), and explicitly forbid the model from writing its own
  References/Bibliography section (the backend appends the authoritative one).
- Add collapse_runaway_repeats(): a defensive sanitizer that collapses any
  short unit repeated 8+ times in a row, applied to the assistant answer
  before it is persisted — belt-and-suspenders for the rare case a model
  loops anyway. Verified 6219->3979 chars on the real case; normal prose,
  lists and tables untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@lfnothias lfnothias merged commit 105f995 into main Jun 22, 2026
1 of 2 checks passed
@lfnothias lfnothias deleted the fix-rag-citation-degeneration branch June 22, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant