All entries

    DeepSeek Streaming Citations: Keep Source Links Attached to the Answer

    Render DeepSeek streamed citations on the text block they support. Preserve source identity, validate links and test interleaved events before shipping.

    A pixel-art stream of answer blocks with separate source cards attached to the correct block, on a technical editing desk.
    Original GPT-generated pixel-art editorial illustration; conceptual artwork, not a product screenshot or test result.

    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.

    JavaScript — illustrative application state, not provider event syntax
    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] });
    }
    EXPLAINED / SEQUENCE

    Keep the block boundary

    A simplified rendering sequence based on documented text and citation events, not a captured customer conversation.

    Incoming stream

    1. Text block

      Accumulate text for the current block identity.

    2. Citation event

      Attach source metadata to that block, not an unrelated answer.

    Rendered answer

    1. Next block

      Start a separate text-and-source record.

    2. Final state

      Preserve sources and completion status when the stream ends.

    THE KEY DISTINCTION

    Citations are structured state. A final list of URLs is not a substitute for preserving which text they support.

    Author-created explanatory diagram. Arrows show the process described in the article, not measured performance.

    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.

    Treat source metadata as untrusted input
    FixtureExpected behaviourFailure to catch
    Text then citationSource attaches to that blockSource moves to the next answer
    Two cited blocksEach retains its own sourcesGlobal list erases scope
    Missing source titleReadable fallback labelEmpty link or crash
    Unsafe destination schemeLink rejected or rendered inertExecutable untrusted link
    Stream interruptedPartial state remains clearly incompletePartial 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.

    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.

    FURTHER READING

    Sources

    Technical references checked .

    END OF ENTRY / 137
    Eeshan Singh with Big Ben in London
    BEHIND THE JOURNAL

    Eeshan Singh

    I’m Eeshan Singh Pokharia, an AI Full Stack Developer and CoFounder of UltraFit360. I work across AI features, full-stack product development, and web and mobile experiences—from Figma and frontend interactions to the systems behind them.

    The event-reducer idea here is a simplified teaching model, not a copied private AI-coach implementation. A production adapter must follow the exact protocol and version it consumes.

    Meet the author
    THE CONVERSATION

    Your side of the story.