Citation Attribution Assembly
Background
RAG outputs cite their sources inline (e.g. ... [1] ... [3]). The UI parses these markers out of the answer text to render footnotes / inline source links. Markers are 1-indexed in the answer, 0-indexed in the source-id list, and should be deduplicated in first-seen order.
Problem statement
Implement extract_citations(answer). Walk answer; for every [<int>] marker (whitespace tolerated inside the brackets), subtract 1 to convert to 0-indexed. Track a seen set; emit each citation only the first time it appears. Return a list[int] in first-seen order.
Input
answer—str(orNone/empty).
Output
Returns list[int] — the 0-indexed citation IDs in first-seen order.
Examples
Example 1 — simple list
Input: "This is [1] and also [3]"
Output: [0, 2]
Example 2 — dedup preserves order
Input: "[2] then [1] then [2] again"
Output: [1, 0]
Example 3 — whitespace inside brackets tolerated
Input: "see [ 5 ]"
Output: [4]
Example 4 — no citations
Input: "plain text"
Output: []
Constraints
- Citations are integers wrapped in
[...](regex\[\s*(\d+)\s*\]). - Non-integer brackets (
[ref]) are skipped. - 1-indexed in the answer text; 0-indexed in the output.
Notes
- Verification. Calling code should check that each emitted index is within the source list (
0 <= idx < len(sources)); a model can hallucinate[99]. - Numbered vs anchor. Some RAG stacks use anchor IDs (
[doc_42]) instead of integers. Same kernel, different regex.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •reference: simple two-citation answer
- •example: dedup preserves first-seen order
- •sample: whitespace inside brackets is tolerated