# One document or a batch

> How many documents a step sees at once, what the email trigger's Attachments setting changes, and how a merge step gathers the documents of one arrival.

Source: https://docs.go-lumen.ai/he/concepts/one-document-or-a-batch

**In one sentence**: A run carries one or more documents from the same arrival; most steps work on each document on its own (an `item` step), a few see all of them together (a `batch` step, such as merging PDFs), and the email trigger's Attachments setting decides whether an email becomes one document or one per attachment.

A **document** is what one step hands to the next: a file or several, the text and fields found in them, and the email they came with. Everything that arrived together, such as one email and its attachments, belongs to one **arrival**, and an arrival is processed in one run.

## Item steps and batch steps

Every step declares how many documents it takes at once.

| Input | The step sees | Example |
| --- | --- | --- |
| `item` (the default) | One document at a time. When the run holds several, the step runs once for each. | **Read the text**, **Find the details**, **Rename the file**, **Send it to me by email** |
| `batch` | All the documents of the run together. | **Merge them into one PDF** |

Almost every step is an item step. **Merge them into one PDF** is the one built-in batch step: it takes the PDFs of every document in the run and joins them into a single file. It could not be an item step, because one document alone has nothing to merge with.

A step that turns one document into several is the opposite. **Split the PDF** takes one document and produces one per page, and each goes on by itself.

The effect is that you rarely think about it. Build the flow in the order you would do the work: keep the PDFs, merge them, rename the result. Item steps run for every document, and the merge step waits for the whole arrival and gathers it.

## An email can be one document or many

The email trigger (**When an email arrives**) has an **Attachments** setting with two choices:

| Choice | In YAML | What a run holds |
| --- | --- | --- |
| **Each attachment separately** | `attachments: separate` | One document per attachment. Each carries the whole email (sender, subject, body) and its own single file. |
| **The whole email as one** | `attachments: together` | One document holding every file. |

An email with no attachment is always one document, holding the body.

**Each attachment separately** is what a new trigger starts with. Pick it when each file is its own thing: ten invoices in one email should each be read, named and filed on their own. Every item step then runs ten times, once per file. **The whole email as one** suits flows that treat the message as a unit: read the whole email, then answer once.

> **Replies are sent once per document:**
> 
> With **Each attachment separately**, a step that replies or forwards runs for every document, so it sends one message per attachment. If you want a single reply, choose **The whole email as one**, or gather the documents with a batch step first.

> **Older flows:**
> 
> A trigger saved before this setting existed has no value, and a missing value means **The whole email as one**, so those flows keep running as they always did. The editor writes the setting only when you choose it.

### Limits

-   At most 20 attachments of one email go into a run. The rest are left out, and every document of the run carries a `skippedAttachments` count so you can see it.
-   Images are never files of a run. Pictures in the email body or attached as pictures are kept for AI steps to look at, but they are not counted as attachments and no file step receives them. The one exception: an email with no document at all, whose pictures were attached as files (a photographed receipt), has those pictures taken as its files.
-   The email's own text is available in either mode as `{email.body}`; see [Data and placeholders](https://docs.go-lumen.ai/he/concepts/data-and-placeholders.md).

## Gathering a batch

Take the invoice-merge flow from [Your first flow](https://docs.go-lumen.ai/he/getting-started/first-flow.md): an email arrives with three PDF invoices, and you want one file.

1.  The trigger is set to **Each attachment separately**, so the run holds three documents, each with one PDF and the same email.
2.  **Keep only the PDFs** is an item step. It runs three times and drops anything that is not a PDF.
3.  **Merge them into one PDF** is a batch step. It runs once, receives all three documents and joins their PDFs, in order, into one file named by its **Name** setting (`merged.pdf` if you leave it empty).
4.  The merge produces one document, holding one file and the email's details. Every step after it, **Rename the file**, **Keep it in Lumen**, **Send it to me by email**, runs once.

The merge step's answer replaces the whole batch, so a flow that merges shrinks from three documents to one at that step. If only one PDF arrives, there is nothing to join; the step passes it on unchanged. If none arrive, the step fails with "there were no PDFs to merge".

In a run you can see the count on each edge of the canvas: it reads **1 email · N files** when one document holds several files, and **N documents** otherwise.

> **A batch is one arrival:**
> 
> A batch step gathers only the documents of its own arrival. It does not wait for later emails, so it cannot merge one month of invoices that came in separate messages.

## Batch, fan out, or loop

Three tools look alike; they answer different questions.

| You want | Use | Docs |
| --- | --- | --- |
| One result from all the documents of an arrival | A batch step | This page |
| One document to become many | A step that fans out, such as **Split the PDF** | [Steps](https://docs.go-lumen.ai/he/build/steps.md) |
| To repeat some nodes over the items of a list inside a document | **For each** | [Repeat for each item](https://docs.go-lumen.ai/he/build/repeat-for-each-item.md) |

## The same in YAML

**YAML:**

```yaml title="batch-statements-merge.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: batch-statements-merge
  namespace: tenant-acme
spec:
  displayName: One statement PDF per email
  triggers:
    - id: email
      ref: {name: email-arrives}
      connectorRef: {name: invoices-inbox}
      config: {attachments: separate, requireAttachment: true}
      next: [pdfs]
  nodes:
    - id: pdfs
      stepRef: {name: keep-pdfs}
      next: [merge]
    - id: merge
      stepRef: {name: merge-pdfs}
      config: {name: statements.pdf}
      next: [mail]
    - id: mail
      stepRef: {name: send-email}
      config:
        to: you@example.com
        subject: "Statements from {email.from}"
        body: Merged into one file.
```

With `attachments: together` the trigger would emit one document holding all the files, and [`merge-pdfs`](https://docs.go-lumen.ai/he/reference/steps/merge-pdfs.md) would join that document's PDFs just the same.

**Console:**

Add the trigger **When an email arrives**. Under **Attachments**, choose **Each attachment separately** or **The whole email as one**. Then add **Keep only the PDFs** and **Merge them into one PDF**. The merge step's **Name** setting names the result.

## Next steps

[Email forwardingGive your workspace an address that starts flows.](https://docs.go-lumen.ai/he/connectors/email-forwarding.md)[TriggersThe email trigger's other settings, including Start only when.](https://docs.go-lumen.ai/he/build/triggers.md)[Repeat for each itemRun a group of nodes once for every item of a list.](https://docs.go-lumen.ai/he/build/repeat-for-each-item.md)[How flows runRuns, steps and attempts, and how documents travel through them.](https://docs.go-lumen.ai/he/concepts/how-flows-run.md)
