In DeepSeek's documented Messages API stream, citation events belong to the current text block. Flattening the whole answer into one string and attaching every source at the end can lose that relationship. Model the stream as blocks with text and citations, then render source links where their scope remains understandable.
Choose one protocol before writing the reducer
DeepSeek exposes related capabilities through several API styles. Their event shapes are not interchangeable. Start with the exact web-tools documentation for the protocol your client uses and retain representative sanitised fixtures. A reducer written for one protocol should not guess how another encodes citations.
The Messages API documentation describes citations arriving on the current text block and new text starting a subsequent block after a citation. It also distinguishes answer-associated text from a source quotation. Those details affect both data structures and the language used in a source tooltip.
Use stable block identity
Store each block independently with its text, citation records and completion state. When a citation arrives, attach it to the documented current block. When another block begins, do not silently merge its source scope into the previous one. A streaming answer may be visually continuous while retaining meaningful internal boundaries.
The following is deliberately an internal model, not a literal DeepSeek wire event. An adapter translates the documented protocol into it. That separation makes the rendering logic testable without pretending that invented event names belong to the provider API.
const blocks = new Map();
function addText(id, text) {
const b = blocks.get(id) ?? { text: '', citations: [] };
blocks.set(id, { ...b, text: b.text + text });
}
function addCitation(id, citation) {
const b = blocks.get(id);
if (!b) throw new Error('Citation has no text block');
blocks.set(id, { ...b, citations: [...b.citations, citation] });
}Keep the block boundary
A simplified rendering sequence based on documented text and citation events, not a captured customer conversation.
Incoming stream
- Text block
Accumulate text for the current block identity.
- Citation event
Attach source metadata to that block, not an unrelated answer.
Rendered answer
- Next block
Start a separate text-and-source record.
- Final state
Preserve sources and completion status when the stream ends.
Citations are structured state. A final list of URLs is not a substitute for preserving which text they support.
Treat source metadata as untrusted input
Validate destination schemes and render titles as text. Do not insert a provider-supplied title or page excerpt as raw HTML. A missing title needs a sensible fallback, such as the verified host or URL, rather than an empty clickable control.
Deduplicate only within the scope that preserves meaning. If the same source supports two blocks, removing its second occurrence from the data model can make the second relationship disappear. The interface can choose a compact presentation while the underlying association remains intact.
Scroll the table horizontally to see all columns.
| Fixture | Expected behaviour | Failure to catch |
|---|---|---|
| Text then citation | Source attaches to that block | Source moves to the next answer |
| Two cited blocks | Each retains its own sources | Global list erases scope |
| Missing source title | Readable fallback label | Empty link or crash |
| Unsafe destination scheme | Link rejected or rendered inert | Executable untrusted link |
| Stream interrupted | Partial state remains clearly incomplete | Partial answer labelled finished |
Separate transport activity from answer progress
A keep-alive event means the connection is active; it is not necessarily new answer text. Render an appropriate waiting state without inserting protocol comments into the answer. If the stream ends early, preserve the partial answer with a clear incomplete state and a bounded retry path.
Do not replay a retry into the same answer state without deciding how duplicates are handled. Use a request identity and reset or reconcile deliberately. Otherwise text and citation records from two attempts can become one misleading answer.
Check claim support, not just link presence
A valid HTTPS source link does not prove the nearby sentence is accurate. For important claims, inspect whether the source actually supports the wording and date. Distinguish an official specification from commentary, and label uncertainty when sources conflict.
This is especially important for coding and product documentation: a citation can point to the right project but the wrong release. Keep version context near the answer where it matters. The UI should make checking evidence easier, not use a source badge as a substitute for checking.
Test the final rendered result
Run interleaved and interrupted synthetic fixtures through the adapter and view the output at narrow widths and with keyboard navigation. Check that source controls have useful labels, focus order is understandable and citations remain attached to the intended text after completion. Those checks provide a stronger guarantee than a screenshot of one successful streamed reply.
Frequently asked questions
Are DeepSeek citation events identical across API protocols? Do not assume that. The web-tools guide describes protocol-specific formats. Build and test the adapter for the Messages, Responses or Chat Completions API you actually use.
Is cited_text necessarily a quotation from the source? The documented Messages API citation field describes answer text associated with the block, not a guarantee that it is quoted source text. Preserve that distinction in your UI.
Does a citation prove the answer is correct? No. It provides a source relationship to inspect. The claim can still misread, overstate or conflict with the source, so important answers need evidence review.
Sources
Technical references checked .



