# withFallback
Source: https://docs.maelstrom-ai.co/0.2/reference/engine/functions/withfallback/
Documentation revision: 89a8f5e9d10cedcf0b30376d7846113f06f26120
SDK manifests in this build: @maelstrom-co/react@0.2.1, @maelstrom-co/client@0.2.1, @maelstrom-co/connectors@0.2.1, @maelstrom-co/protocol@0.2.1, @maelstrom-co/engine@0.2.1
These docs follow this source revision. Manifest versions do not prove release compatibility; verify the installed package exports before using an API.

> **withFallback**<`In`, `Out`\>(`primary`, `fallback`): [`Stage`](https://docs.maelstrom-ai.co/0.2/reference/engine/type-aliases/stage/)<`In`, `Out`\>

Defined in: packages/engine/src/lib/planner/with-fallback.ts:24

Runs `primary`, and runs `fallback` with the same input and context only when `primary` returns a `Deferred`. Candidates and `EngineError`s from `primary` are returned unchanged, so a real failure is never masked by a second attempt.

The two run one after the other: on deferral the latencies add up. Both stages share the request’s signal and must honour it.

## Type Parameters

### In

`In`

### Out

`Out`

## Parameters

### primary

[`Stage`](https://docs.maelstrom-ai.co/0.2/reference/engine/type-aliases/stage/)<`In`, `Out`\>

### fallback

[`Stage`](https://docs.maelstrom-ai.co/0.2/reference/engine/type-aliases/stage/)<`In`, `Out`\>

## Returns

[`Stage`](https://docs.maelstrom-ai.co/0.2/reference/engine/type-aliases/stage/)<`In`, `Out`\>

## Example

```ts
// `modelPlanner` is any `Planner`, for example one that calls a model.
const planner = withFallback(
  pipeline(selectVessels, arrangeLayout),
  modelPlanner,
);
```
