Fix duplicate references + runaway citation tooltips in RAG answers#26
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 citationtitleattribute that looped"H. "~749 times (2,247 chars).Prompt (
get_system_prompt)titleattribute previously had to hold the full author list (<All authors>. <Year>. <Title>. <Journal>.) — which small models loop on. Shortened toTitle. 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).Defensive sanitizer (
collapse_runaway_repeats)Verification
nothias-hdr): one## Referencessection, no runaway repetition, tooltip ="Feature-based Molecular Networking in the GNPS Analysis Environment. 2019."(74 chars vs the old 2,247).138 passedacross prompt/citation/reference/route unit tests locally.Note: the
testCI job fails on a pre-existingtest_web_app_routes.pyissue already present onmain(the CI "safe subset" sandbox registers no API routers); it passes locally (30/30) and is unrelated to this change.🤖 Generated with Claude Code