Sub topic 10
Showing Citations to Users
This chapter assumes you can write basic Python and that your pipeline already numbers sources, prompts the model to cite them as [n], and validates the markers. Here you turn the validated markers into something a reader can click and check.
Why rendering is its own step
The model writes [2]. A reader needs to know what [2] is. If the marker stays as plain text, the reader must trust it without a way to check it. A link to the source turns the marker into something they can verify.
Rendering also belongs after validation. If you link a marker before checking it, you can send a reader to the wrong page, or to nothing. The order is: validate, then render.
Turning markers into links
The function below replaces each [n] with a Markdown link to the URL of source n. A marker that does not match any source is left unchanged.
import re
def render_with_links(answer, sources):
"""sources: list of dicts with 'url'. Replaces [n] with a Markdown link to source n."""
def repl(match):
n = int(match.group(1))
if 1 <= n <= len(sources):
return f"[[{n}]]({sources[n - 1]['url']})"
return match.group(0)
return re.sub(r"\[(\d+)\]", repl, answer)
sources = [
{"url": "https://example.com/retries"},
{"url": "https://example.com/timeouts"},
]
print(render_with_links("Failed requests are retried three times [1].", sources))This prints Failed requests are retried three times [[1]](https://example.com/retries). The outer brackets are literal text and the inner part is the link, so the reader still sees [1] as a clickable number.
Source numbers start at 1, so the code looks up sources[n - 1]. If your context block numbered from 0, this lookup would be off by one and every link would point to the wrong page.
Design choices for the reader
Links alone are the minimum. These choices make citations easier to use:
- Show a source list. Print the numbered titles and URLs under the answer. Readers can scan all sources at once.
- Use titles, not only numbers. A link labeled "Retry policy" says more than
[1]. - Link to the passage when you can. A URL with an anchor or a text fragment takes the reader to the cited section instead of the top of a long page.
- Show the quote. If you have a verified quote, display it on hover or in a list. The reader sees the evidence without leaving the page.
- Cite only what was used. List sources the answer actually cites, not every chunk that was retrieved.
Handling invalid markers
The renderer above leaves an out-of-range marker as plain [7]. That is safe from a security view but confusing for a reader. Decide on a policy before you ship:
| Policy | What the user sees | Use when |
|---|---|---|
| Leave as text | A dead `[7]` | Debugging only |
| Remove the marker | A claim with no citation | You prefer a clean display |
| Flag the claim | A note that the claim is unverified | Trust matters more than polish |
| Reject the answer | A retry or a fallback message | Strict accuracy is required |
Do not hide the problem silently when you remove a marker. A claim with no citation looks the same as a claim the model never tried to support, so consider marking it.
Escaping and safety
URLs come from your own chunk metadata, so they are usually trusted. Still, if sources come from user uploads or web pages, check that each URL uses https or http before you build a link. A javascript: URL in a link is an attack. Never take the URL from the model's text. Always look it up by number in your own list.
Exercise
A reader reports that every citation link in your app opens the page for the source after the one the answer meant. The model's markers are correct. What is the most likely cause, and where in the code would you look?
Show answer
An off-by-one error between numbering and lookup. The context was numbered from 1, but the renderer indexes the source list without subtracting 1, so [n] opens sources[n] instead of sources[n - 1]. Check the lookup in the rendering function.
Exercise
Name two reasons to render only the sources an answer actually cites, instead of listing every retrieved chunk.
Show answer
The list stays short, so readers can find the evidence quickly. It also avoids implying that uncited sources support the answer, which would overstate how well grounded the answer is.