﻿# n8n pairedItem preserves the source of a transformed document result

Preserve n8n document provenance when Code nodes create new results, distinguishing item lineage from list position and diagnosing ambiguous or cross-branch references.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-n8n-paired-item-source-linking/
Language: en

## Keep source identity through transformation

A workflow extracts passages from several documents and creates new summary items. Later it needs each summary's original URL. Matching by the current list position is fragile after filtering or sorting. n8n's [item-linking concepts](https://docs.n8n.io/build/work-with-data/reference-data/link-data-items/how-items-link-through-workflows.md) describe metadata connecting outputs to their generating inputs. Preserve that relationship alongside the content so a correct summary does not acquire a neighboring document's citation.

## Know when automatic linking is insufficient

n8n can infer many ordinary input/output relationships, but creating new items from multiple inputs can leave provenance unknown. The [Code-node linking guide](https://docs.n8n.io/build/work-with-data/reference-data/link-data-items/preserving-linking-in-the-code-node.md) requires `pairedItem` in relevant cases when later expressions use a previous node's `.item`. Returning a new JSON object with the right visible fields does not by itself retain the hidden relationship. Trace the transformation that first created the unlinked result.

## Represent the real relationship

When a new result derives from a particular input, preserve that source association. When a summary combines several documents, a singular source may be conceptually wrong; retain a source set or another explicit mapping appropriate to the task. Do not manufacture a single-item lineage merely to silence an expression error. The workflow must express what produced the content, rather than choose whichever source makes the downstream field nonempty.

## Diagnose the error category before substituting position

The [linking-error guide](https://docs.n8n.io/build/work-with-data/reference-data/link-data-items/item-linking-errors.md) distinguishes a broken thread, multiple matching items, and a node on another branch. A cross-branch reference can resolve to `null` while execution still succeeds. `.first()` or indexed access is appropriate only when the intended source is genuinely positional. Replacing every failed `.item` with `.first()` can produce clean runs that consistently cite the wrong document, especially after an append-style merge.

## Test provenance as part of answer quality

Use documents with unmistakably different URLs and facts, then reorder and filter them before the summary stage. Inspect both the passage and attributed source. The [citation-support guide](/journal/rag-citation-precision-completeness/) makes clear why plausible text with a wrong reference fails the evidence requirement. [Tool observations](/journal/learn-codex-tool-observation/) distinguish a successful node from correct lineage. Preserve a case that checks attribution after transformation so later refactoring cannot quietly break source tracking.
