Skip to content

Keep source text from closing the <source> block in the prompt - #5

Merged
sinnercode228 merged 1 commit into
mainfrom
fix/escape-source-tags
Oct 20, 2026
Merged

sinnercode228 merged 1 commit into
mainfrom
fix/escape-source-tags

Conversation

@sinnercode228

Copy link
Copy Markdown
Owner

What was wrong: render_sources() in backend/src/docmind/rag/prompt.py HTML-escaped the source title and location but inserted the chunk text as is. A document containing </source> or </sources> could end the block early, and the rest of that chunk landed outside <sources>, right next to the question, where the "treat sources as reference material" rule no longer covers it.

The fix: before rendering, the < of any <source, </source, <sources or </sources tag inside the text (any case) is replaced with &lt;, so the renderer's own tags are the only real ones. Everything else in the text, including <, & and quotes in code, is left unchanged.

How it was tested: new test test_source_text_cannot_close_the_block in backend/tests/test_llm.py; it failed before the fix (two </source> tags in the output). Locally ran what CI runs: ruff check, ruff format --check, mypy src, pytest (115 passed), and on the frontend npm ci, lint, typecheck, test, build:pages.

Fixes #4

render_sources() escaped the title and location but inserted the chunk text as is, so a document containing </source> or </sources> could end the block early and put the rest of the chunk outside the sources. Escape the < of any source/sources tag inside the text; the rest of the text is unchanged.

Fixes #4
@sinnercode228
sinnercode228 merged commit 418ac99 into main Oct 20, 2026
3 checks passed
@sinnercode228
sinnercode228 deleted the fix/escape-source-tags branch October 20, 2026 07:49
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.

Source text can close the <source> block in the prompt

1 participant