---
related_nodes: ["input", "set_workflow_output", "subworkflow_tool", "loop"]
---

# Subworkflow

Runs another workflow as a nested step and returns its output.

A Subworkflow node is a **function call**. It passes a value in, waits for the child to finish, and receives what the child returned. The caller decides what happens to the result.

## The Function Contract

Three nodes form one contract, and a composed workflow needs all three:

| Role | Node | Where |
|---|---|---|
| Parameters | `input` | Child, as the configured trigger node |
| Call | `subworkflow` | Parent |
| Return value | `set_workflow_output` | Child |

The value on the Subworkflow node's `input` socket arrives in the child under the key `value`, at the trigger node named by `trigger_node_id`. The value the child passes to `set_workflow_output` comes back on the Subworkflow node's `output` socket. A child with no `set_workflow_output` returns `null` — the call still succeeds, it simply answers with nothing.

```text
Parent                                  Child
------                                  -----
[Build request] -> Subworkflow.input -> [Input trigger]
                                             |
                                        [ ...work... ]
                                             |
[Use result]  <- Subworkflow.output <-  [Set Workflow Output]
```

## Write Children That Return, Not Children That Act

The most common mistake is a child workflow that performs the visible side effect itself — sends the message, writes the row, posts the file — and returns nothing. That is a handoff, not a call, and it costs more than it looks:

- **The result cannot be combined.** A child that sends its own Discord message produces a second message. A child that returns the message content lets the parent send one.
- **The parent cannot branch on the outcome.** There is nothing to test.
- **Ownership of the side effect is split.** Two workflows post to the same channel, and neither one is the place to go when the posting changes.

Put the side effect in the caller and let the child return the material for it. A child that generates an image should return the image; a child that scores a document should return the score. Reserve side effects inside a child for work the parent genuinely has no interest in.

### Returning Binary Data

Runtime binary handles do not survive the boundary. A child producing audio, an image or a file must persist it first — `Upload Media` turns a binary into a plain JSON reference (`media_object_id` and friends) that crosses cleanly and that the parent can hand to `Discord Action`, `Telegram Send` or another `Upload Media` consumer.

```text
[Generate Speech] -> [Audio Join] -> [Upload Media] -> [Set Workflow Output]
```

## Configuration

| Field | Required | Description |
|---|---|---|
| `workflow_id` | Yes | Slug or ULID of the workflow to run. |
| `trigger_node_id` | Yes | Which node in the child to start from. A workflow may hold several disconnected islands; this names the one you are calling. |
| `detach` | No | Defaults to `false`. See below. |
| `input_expression` | No | Off by default. A template over the input, such as `{{ input.start }}`, whose value is what the child receives. See below. |

The child runs under **its own persisted timeout**. A parent cannot raise or lower it; change it on the child.

Recursion is bounded at a depth of **8**. The same workflow may appear more than once in a path — only depth is limited.

## `input_expression`

A caller often holds a larger object than the child wants. Switch `input_expression` on and write a template that picks out the part to send, instead of putting a JavaScript node in front of the call:

```text
{{ input.start }}
```

It is the same `{{ … }}` template every other node field uses, over the value on the `input` socket. Switching it on starts from `{{ input }}`, which passes the input through unchanged. It applies to both `detach: false` and `detach: true`.

- **Off** (absent or `null`): the child receives the `input` socket's value as it is.
- **A single reference**, such as `{{ input.start }}`: the child receives that value with its type intact — a number, an object, a list, or `null`.
- **Text around references**, such as `Order {{ input.id }}`: the child receives the rendered text.
- **A template that references nothing** — raw text, or text with no `{{ … }}`, such as `inputs.input.start` with the braces forgotten: the node fails rather than sending the child constant text, and the editor flags the field.
- **A missing path, an unclosed `{{`, `{% … %}` blocks, or an empty template**: the node fails, and the error names the template. The input is never passed through in its place.

A template cannot end the branch. To skip the call, put an If or Branch node in front of it.

## `detach`

`detach` chooses between a call and a dispatch, and the difference is total.

**`detach: false` (default) — a call.** The parent waits. The wait is bounded by the smaller of the child's timeout and the parent's own remaining budget, and:

- The child's returned value arrives on `output`.
- If the child fails, **this node fails**, carrying the child's error message.
- If the child returns `skip(reason)`, the skip travels out on `output` and ends the caller's branch too. Its origin is re-attributed to the Subworkflow node, because the child's inner node id names nothing the caller's editor could show.
- If the child does not finish in time, the node fails with `Child workflow did not start or complete within N seconds`.

**`detach: true` — a dispatch.** The parent submits the job and moves on immediately. Then:

- `output` is `{"detached": true, "job_id": "<ulid>"}`. **The child's return value is never read.**
- **A child failure is invisible to the parent.** Only a failure to *submit* surfaces.
- The child is not fully independent: its job is registered against the parent, and **it is cancelled if the parent exhausts its time budget**.

Use `detach: true` only when the parent genuinely does not care about the outcome and must not wait for it — a long trailing job whose failure is acceptable, or work that will report elsewhere. If you want the result, or want a failure to be visible, leave it `false`.

Detaching to dodge a timeout is a trap. The child is cancelled when the parent's budget runs out anyway, so the work is lost without anything saying so. Raise the parent's timeout instead.

## Inputs and Outputs

| Socket | Direction | Description |
|---|---|---|
| `input` | Input | Passed to the child's trigger node as `value`, or, with `input_expression` on, rendered as `input` in the template and the result passed instead. |
| `output` | Output | The child's `set_workflow_output` value, or the detached job handle. |

## Subworkflow vs Subworkflow Tool

Both run another workflow; they differ in who decides to call it.

- **Subworkflow** — the graph calls it, every time the branch runs.
- **[Subworkflow Tool](subworkflow_tool.md)** — an AI node calls it, if and when the model chooses, with arguments the model supplies.

## Troubleshooting

**`No trigger node configured`** — `trigger_node_id` is empty. Pick the node in the child to start from.

**`Subworkflow not found: <id>`** — the slug or ULID does not resolve in this workspace.

**`output` is `null`** — the child ran and returned nothing. Add `set_workflow_output` to the child.

**`output` is `{"detached": true, ...}`** — `detach` is on. Turn it off to receive the child's value.

**Downstream nodes all skipped** — the child returned a `skip`. That is the documented behaviour: a child that ends its branch ends the caller's.

**``Subworkflow input expression `<template>` failed``, caused by ``Failed to interpolate '<path>': path is missing or undefined``** — the template references something the input does not have, usually a misspelt field such as `{{ input.strat }}`. The cause lists the fields the input does have.

**``Subworkflow input expression `<template>` failed``, caused by ``…unsupported interpolation expression…``** — the template has an unclosed `{{`, a `{% … %}` block, or a reference that is not a plain path. Use `{{ input.field }}` references only.

**``Subworkflow input expression `<template>` references nothing from the input``** — the template is raw text or has no `{{ … }}` reference, so it would send the child a constant. Wrap the path in braces, such as `{{ input.start }}`.

**``Subworkflow input expression `<template>` is empty``** — the field is switched on with nothing in it. Reference the input, or switch it off to pass the input unchanged.

**The child's side effect happened twice** — the parent is probably also performing it. Decide which side owns it; usually the parent.
