> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canary.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# choice node

> Route a run on a boolean or a fixed set of values, with one case for each value.

A `choice` picks exactly one case from a boolean or a closed string enum that an earlier step
produced. It doesn't evaluate conditions or make judgments: produce the decision first, with a
[`task`](https://docs.canary.effectiveai.app/procedures/reference/nodes/task), an [`agent`](https://docs.canary.effectiveai.app/procedures/reference/nodes/agent) or a
function through [`call`](https://docs.canary.effectiveai.app/procedures/reference/nodes/call), then route on it.

## Syntax

```yaml choice id=route-filing theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
on: $disposition.status
cases:
  'approved': continue
  'objection': '#objection-round'
  'withdrawn': { complete: { status: withdrawn } }
```

With region outputs, every case that continues must bind them:

```yaml choice id=route-portfolio theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
on: $portfolio_pull.status
cases:
  'ok': '#use-live-portfolio'
  'unavailable': { portfolio_source: $snapshot_pull }
outputs:
  portfolio_source:
    schema: portfolio-pull
```

## Fields

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `on` | reference | Yes | None | One reference to a required boolean or string-enum member, such as `$approval.value.approved` |
| `cases` | mapping | Yes | None | One case for every possible value of `on`, and no others. See [Cases](#cases) |
| `outputs` | mapping | No | None | Names the choice binds for later steps, each with a `schema`. See [Region outputs](#region-outputs) |

The fence takes no attributes after `id`, and its body takes no other fields.

### What you can route on

`on` must point to a required member whose schema is `boolean` or a string `enum`, read from an
input or an earlier step's output. Open strings, numbers, null, arrays, objects, optional members
and unions can't be routed. Expressions such as `$approval.score > 80` aren't allowed; compute a
boolean or enum in an earlier step.

### Cases

Case keys are strings. Quote them so YAML keeps them as strings.

* For a boolean, the keys are exactly `'true'` and `'false'`.
* For an enum, the keys are exactly the enum's values.
* Every value appears exactly once. There is no `default` case.

Each case is one of four forms:

| Case form | Example | What it does |
| - | - | - |
| Heading body | `'#objection-round'` | Runs the steps under that child heading, then continues after the choice |
| Inline outputs | `{ portfolio_source: $snapshot_pull }` | Binds every declared output from plain values and references, then continues |
| Inline complete | `{ complete: { status: withdrawn } }` | Ends the run with this value as the procedure's output |
| `continue` | `continue` | Binds nothing and continues. Allowed only when the choice declares no outputs |

Inline cases hold only data: literals, built objects and references. Anything that does work, such
as a task, call, wait or agent, goes in a heading body.

Several case keys can share one heading body. For example, `'approved'` and
`'approved-with-modifications'` can both point to `'#promote-package'`.

### Heading bodies

A heading body is a child heading of the section that holds the choice. The rules:

* The choice is the last fence in its section.
* Each `'#block'` target is a direct child heading of that section. Use a custom ID, such as
  `### Use the snapshot {#use-snapshot}`, so renaming the heading doesn't change the target.
* The headings the choice uses come first among the section's child headings, one after another.
* After a body finishes, the run continues at the first child heading the choice doesn't use. If
  there is none, it continues at the next heading after the section.
* A body can contain any steps, including a nested `choice`, [`repeat`](https://docs.canary.effectiveai.app/procedures/reference/nodes/repeat)
  or [`for-each`](https://docs.canary.effectiveai.app/procedures/reference/nodes/for-each), and can end the run with
  [`complete`](https://docs.canary.effectiveai.app/procedures/reference/nodes/complete).

A choice whose every case ends the run needs nothing after it.

### Region outputs

`outputs` declares the values the choice hands to later steps. Every case that continues must bind
each declared output exactly once:

* an inline outputs case binds all of them by itself;
* a heading body binds each one with an ordinary step whose `output` has that name, on every path
  that reaches the end of the body.

Cases that end the run don't bind outputs.

After the choice, read an output by its own name, such as `$portfolio_source`, not through the
choice's ID. Values a body binds that aren't declared outputs stay inside the body and can't be
read after the choice.

If cases produce different shapes, declare an output schema with nullable members, and collapse
them with `??` in a later [`value`](https://docs.canary.effectiveai.app/procedures/reference/nodes/value) node.

## Example

A minimal approval route, with a body for one value and an inline end for the other:

````markdown theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
```yaml choice id=route-approval
on: $approval.value.approved
cases:
  'true': '#deploy-approved'
  'false': { complete: { approved: false } }
```

### Deploy {#deploy-approved}

```yaml call id=run-deploy
function: '@local.deploy'
input: {}
output: deployment
```
````

This procedure reads a policy's balance from billing. If billing is down, it falls back to last
night's export; if billing has no such policy, it ends at once. Both the live and the fallback
path bind `balance`, which later steps read. A second choice uses `continue` for the past-due case.
The policy values and hosts are illustrative.

````markdown balance-check.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: balance-check
inputs:
  policy_number: { type: string }
outputs:
  result: { schema: balance-result }
---

# Balance check

Read a policy's balance from billing, falling back to the nightly snapshot.

## Read the balance

```yaml call id=call-live-balance
function: '@local.read-live-balance'
input:
  policyNumber: $inputs.policy_number
output: live_balance
```

```yaml choice id=pick-balance
on: $live_balance.status
cases:
  'ok': { balance: $live_balance.balance }
  'unavailable': '#use-snapshot'
  'not-found':
    complete:
      policyNumber: $inputs.policy_number
      pastDue: false
      amountDue: '0'
outputs:
  balance:
    schema: balance
```

### Use the nightly snapshot {#use-snapshot}

Billing is down, so read last night's export instead.

```yaml call id=call-snapshot-balance
function: '@local.read-snapshot-balance'
input:
  policyNumber: $inputs.policy_number
output: balance
```

## Decide whether it is past due

```yaml call id=classify
function: '@local.classify-balance'
input:
  amountDue: $balance.amountDue
  daysPastDue: $balance.daysPastDue
output: classification
```

```yaml choice id=route-past-due
on: $classification.pastDue
cases:
  'true': continue
  'false':
    complete:
      policyNumber: $inputs.policy_number
      pastDue: false
      amountDue: $balance.amountDue
```

## Report the past-due balance

```yaml complete id=finish
output: result
value:
  policyNumber: $inputs.policy_number
  pastDue: true
  amountDue: $balance.amountDue
```

## Resources

```yaml schema id=policy-request
type: object
additionalProperties: false
required: [policyNumber]
properties:
  policyNumber: { type: string }
```

```yaml schema id=balance
type: object
additionalProperties: false
required: [amountDue, daysPastDue]
properties:
  amountDue: { type: string, format: decimal }
  daysPastDue: { type: integer, minimum: 0 }
```

```yaml schema id=live-balance-read
type: object
additionalProperties: false
required: [status, balance]
properties:
  status: { enum: [ok, unavailable, not-found] }
  balance:
    $ref: '#/schemas/balance'
```

```yaml schema id=classification
type: object
additionalProperties: false
required: [pastDue]
properties:
  pastDue: { type: boolean }
```

```yaml schema id=balance-result
type: object
additionalProperties: false
required: [policyNumber, pastDue, amountDue]
properties:
  policyNumber: { type: string }
  pastDue: { type: boolean }
  amountDue: { type: string, format: decimal }
```

```ts effect id=read-live-balance input=policy-request result=live-balance-read uses=fetch
async (request, { fetch }) => {
  const response = await fetch(
    `https://billing.northwind.example/balances/${request.policyNumber}`
  );
  if (response.status === 404)
    return { status: 'not-found', balance: { amountDue: '0', daysPastDue: 0 } };
  if (!response.ok) return { status: 'unavailable', balance: { amountDue: '0', daysPastDue: 0 } };
  return { status: 'ok', balance: { amountDue: '412.50', daysPastDue: 41 } };
};
```

```ts effect id=read-snapshot-balance input=policy-request result=balance
async (_request, _context) => ({ amountDue: '412.50', daysPastDue: 40 });
```

```ts function id=classify-balance input=balance result=classification
(balance) => ({ pastDue: balance.daysPastDue > 30 });
```
````

The two effects return fixed balances to keep the example short. A real effect would parse the
response from your billing system.

## On retry and resume

* **Routing follows the data.** The choice reads the recorded value of `on` and selects the
  matching case. It never calls anything, so a resumed run selects the same case again.
* **No override.** A run can't take the `'true'` case while `on` is `false`. To route
  differently, the value itself has to change.
* **Outputs appear together.** A body's declared outputs become visible to later steps in one
  step when the body ends, never one at a time. When a nested choice or loop ends at the same
  time as its enclosing body, every level closes together.
* **Ending inside a body.** When a body reaches `complete`, the run ends, and the choice and every
  enclosing construct are recorded as finished. If a step in a body fails, the run fails, and
  enclosing steps aren't marked finished.

## Common errors

| Code | Cause | Fix |
| - | - | - |
| `PROC-CHOICE-001` | An unknown or missing field, or `on` isn't one reference | Use `on`, `cases` and optionally `outputs`, with `on` as one reference |
| `PROC-CHOICE-002` | `on` isn't a boolean or a closed string enum | Route on a boolean or `enum` member, computed earlier if needed |
| `PROC-CHOICE-003` | A case is missing, extra or repeated, or there is a `default` case (`Choice cases must exactly cover not-found, ok, unavailable.`) | Give exactly one case per value |
| `PROC-CHOICE-006` | An inline outputs case binds too few or too many outputs, uses something other than data, or `continue` appears when the choice declares outputs (`Case "ok" may not continue: this choice declares outputs.`) | Bind every declared output in each continuing case |
| `PROC-REGION-001` | A target heading doesn't exist, isn't a heading with steps, or another construct already uses it | Point at an existing child heading that only this choice uses |
| `PROC-REGION-002` | A target isn't a direct child heading, the used headings aren't together and first, or a fence follows the choice in its section | Move the choice to the end of its section and its bodies directly under it |
| `PROC-REGION-003` | A body path reaches the end without binding a declared output, binds one twice, or binds an undeclared output | Bind each declared output once on every path |
| `PROC-DATA-004`, `PROC-DATA-005` | `on` is unknown or optional, or a later step reads a value that stayed inside a body | Route on a required member; declare values that later steps need as outputs |
| `PROC-GRAPH-005` | A heading isn't claimed by the choice, so it runs in order and binds a name a second time | List the heading as a case target, or rename the value |

## See also

* [repeat node](https://docs.canary.effectiveai.app/procedures/reference/nodes/repeat.md) and [for-each node](https://docs.canary.effectiveai.app/procedures/reference/nodes/for-each.md): other constructs with heading bodies
* [parallel node](https://docs.canary.effectiveai.app/procedures/reference/nodes/parallel.md)
* [complete node](https://docs.canary.effectiveai.app/procedures/reference/nodes/complete.md)
* [value node](https://docs.canary.effectiveai.app/procedures/reference/nodes/value.md)
* [task node](https://docs.canary.effectiveai.app/procedures/reference/nodes/task.md): the usual source of an approval decision


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.