Skip to content
中文

Write a workflow

A workflow is a list of steps written in YAML. The runtime keeps checkpoints, so a workflow can wait, retry and run branches in parallel. This page goes from the minimal skeleton to publishing and running.

The examples call a custom Action, docs-samples/add-one (input {n}, returns {answer}). The commands reuse PRAXIS_API_URL and PRAXIS_API_KEY from the quickstart. Create the Action first (if the name is taken the call returns NAME_TAKEN; just use the existing one):

终端窗口
curl --fail-with-body -sS "$PRAXIS_API_URL/automation/v1/actions" \
-H "Authorization: Bearer $PRAXIS_API_KEY" -H 'Content-Type: application/json' \
--data '{"namespace":"docs-samples","name":"add-one",
"input_schema":{"type":"object","properties":{"n":{"type":"number"}},"required":["n"],"additionalProperties":false},
"output_schema":{"type":"object","properties":{"answer":{"type":"number"}},"required":["answer"],"additionalProperties":false},
"implementation":{"kind":"code","language":"ts","source":"export default async (input: { n: number }) => ({ answer: input.n + 1 });"}}' \
| jq -e '.id | startswith("act_")'

Minimal skeleton

document:
dsl: "1.0.3"
namespace: docs-samples
name: add-two
version: "1.0.0"
input:
schema:
format: json
document:
type: object
properties:
n:
type: number
required: [n]
do:
- first:
call: docs-samples/add-one
with:
n: '${ .n }'
- second:
call: docs-samples/add-one
with:
n: '${ .answer }'
  • document requires dsl, namespace, name and version. version is an exact semver and must equal the version you publish, otherwise publishing returns DEFINITION_IDENTITY_MISMATCH.
  • do is the step list. Each list item has one step name, and its value holds exactly one operation: call, run, set, switch, for, fork, try, wait, listen, raise or a nested do. Step names must be unique within a list. emit is not supported yet.
  • A root-level input.schema is optional and constrains the run input. Write it as format: json plus document.
  • The run input is . in the first step; each step’s output is the next step’s input.

Call an Action

call: <namespace/name> calls an Action: built-ins use the bare short name (chat, ocr, web-search, write-file), your own use namespace/name. with is the Action’s input and defaults to {}. The previous step’s output is not passed in automatically; take what you need in with through an expression. An Action always runs at its current revision and does not accept @version.

The input contract is in the get an action response at current_revision.input_schema, and the output contract at current_revision.output_schema; neither is at the top level.

Run a script or child workflow

do:
- child_script:
run:
script:
namespace: docs-samples
name: hello
version: "^1"
input:
n: '${ .n }'
- child_workflow:
run:
workflow: docs-samples/add-two@^1
with:
n: '${ .answer }'
- background:
run:
await: false
workflow: docs-samples/add-two@^1
with:
n: 1
  • A reference takes two forms: the string namespace/name@range, with the input in the step’s with; or the object {namespace, name, version, input}, with the input in the object’s input. version can be an exact version, a range or a tag. Publishing a workflow locks the exact versions of its dependencies.
  • By default the parent waits durably for the child and uses no compute while waiting. The child’s output becomes the step’s output; if the child fails, the step fails.
  • With await: false the step creates the child and immediately returns {run_id}. Cancelling the parent does not cascade.
  • Inline script.code is not supported. Save the code as a script first, then reference it.

Pass data between steps

A string is evaluated only when the whole string is written as ${ … }. The expression language is jq; every other string is a literal. Always single-quote the whole expression, for example pages: '${ .parsed.pages }', including inside a { } flow mapping: input: { pages: '${ .parsed.pages }' }. Unquoted, YAML reads the braces as a flow mapping and fails to parse.

WhereMeaning
.The current step’s input: the previous output, or the run input in the first step
$contextGlobal context. Starts as the run input and is replaced only by export.as
$workflow.inputThe original run input
$input / $outputThe current step’s transformed input / output
$value, $iThe each and at variable names of a for; defaults are item and index

A step can carry input.from (transform the input entering the step), output.as (transform the step’s output, which becomes the next input) and export.as (the result replaces $context, so keep old values with $context + {…}).

do:
- first:
call: docs-samples/add-one
with:
n: '${ .n }'
export:
as: '${ $context + {first: .answer} }'
- second:
call: docs-samples/add-one
with:
n: '${ .answer }'
- report:
set:
first: '${ $context.first }'
second: '${ .answer }'
started_with: '${ $workflow.input.n }'

Input {"n":1} yields {"first":2,"second":3,"started_with":1}.

Three rules, where multi-step workflows most often go wrong:

  • . sees only the previous step’s output. A later step that needs a field of the run input reads $workflow.input.field; do not expect the previous step to carry it along.
  • To use an earlier step’s result, write export: { as: '${ $context + { name: . } }' } in that step, then read $context.name from any later step. export.as is an expression whose result replaces $context as a whole; it does not name the value. as: name would turn $context into that string, so validation refuses it.
  • A field an expression cannot find evaluates to null. Leave an optional field out when you do not need it; passing null fails with INPUT_INVALID, errors[] names the field and hint suggests $workflow.input or $context.

Control flow

set replaces the current data with the given object. switch evaluates each when in order, and the then of the first true branch applies; a branch without when is the default. then is continue, end, exit or a step name in the same list. raise.error requires type and status; title and detail are kept in the run’s error.

do:
- route:
switch:
- too_big:
when: '${ .n > 100 }'
then: refuse
- otherwise:
then: continue
- accept:
set:
status: accepted
n: '${ .n }'
then: end
- refuse:
raise:
error:
type: https://example.com/errors/too-big
status: 422
title: n is too large

for.in evaluates to an array or object. Each iteration’s input is the previous iteration’s output, and the loop’s output is the last iteration’s output; to collect every result, write it into $context. fork runs branches in parallel and outputs an array of the branch outputs; compete: true keeps only the first branch to finish.

do:
- each_number:
for:
in: '${ .numbers }'
each: value
at: i
do:
- bump:
call: docs-samples/add-one
with:
n: '${ $value }'
export:
as: '${ $context + {results: (($context.results // []) + [.answer])} }'
- collect:
set:
results: '${ $context.results }'

Input {"numbers":[1,2,3]} yields {"results":[2,3,4]}.

Handle errors and retry

When a step inside try fails, catch.errors.with matches the error by field equality (for example status or type); omit it to catch everything. catch.do holds recovery steps and catch.as names the error. retry reruns the whole try list, and limit.attempt.count does not include the first execution.

do:
- guarded:
try:
- lookup:
call: docs-samples/flaky
with:
id: '${ .id }'
catch:
errors:
with:
status: 503
retry:
delay:
seconds: 2
backoff:
exponential: {}
limit:
attempt:
count: 3
- fallback_demo:
try:
- broken:
call: docs-samples/always-fails
catch:
as: err
do:
- recover:
set:
status: failed
reason: '${ $err.detail }'

Wait and events

wait takes an ISO 8601 duration (PT30S) or {minutes: 5}. A waiting run uses no compute.

listen supports only this subset today: to.one, or a non-empty to.any of 1–8 entries; the filter with.type is required and a literal, and with.data is an expression evaluated on the event data; read is data (default) or envelope; it cannot sit inside a fork; when timeout.after expires the step fails with a timeout error that catch can handle. The step’s output is an array with one element.

do:
- approval:
listen:
to:
one:
with:
type: approval
data: '${ .approved == true }'
read: data
timeout:
after: PT1H
- decision:
set:
approved: '${ .[0].approved }'

The only public way to deliver an event to a run is complete a run wait: get the wait_id from list run waits, then POST a payload. The payload becomes the event data, at most 32 KiB, and must satisfy with.data. An event carries only type and data, so a source or subject filter never matches. Runs have no automatic way to publish events to each other, and emit is not supported yet.

Save, validate, publish and run

Save the skeleton above as add-two.yaml. Validation does not save and saving does not validate, so validate first:

终端窗口
NAMESPACE="docs-samples"; NAME="add-two"
curl --fail-with-body -sS "$PRAXIS_API_URL/automation/v1/validate" \
-H "Authorization: Bearer $PRAXIS_API_KEY" -H 'Content-Type: application/json' \
--data "$(jq -n --rawfile source add-two.yaml '{kind:"workflow",source:$source}')" \
| jq -e '.valid == true'
curl --fail-with-body -sS "$PRAXIS_API_URL/automation/v1/workflows" \
-H "Authorization: Bearer $PRAXIS_API_KEY" -H 'Content-Type: application/json' \
--data "$(jq -n --arg ns "$NAMESPACE" --arg name "$NAME" --rawfile draft add-two.yaml \
'{namespace:$ns,name:$name,draft:{source:$draft}}')" | jq -e '.id | startswith("wf_")'
REVISION=$(curl --fail-with-body -sS "$PRAXIS_API_URL/automation/v1/workflows/$NAMESPACE/$NAME" \
-H "Authorization: Bearer $PRAXIS_API_KEY" | jq -er '.draft.revision')
curl --fail-with-body -sS "$PRAXIS_API_URL/automation/v1/workflows/$NAMESPACE/$NAME/versions" \
-H "Authorization: Bearer $PRAXIS_API_KEY" -H 'Content-Type: application/json' \
--data "$(jq -n --arg r "$REVISION" '{version:"1.0.0",draft:{revision:$r}}')" \
| jq -e '.version == "1.0.0"'
curl --fail-with-body -sS "$PRAXIS_API_URL/automation/v1/runs" \
-H "Authorization: Bearer $PRAXIS_API_KEY" -H 'Content-Type: application/json' \
--data "$(jq -n --arg t "$NAMESPACE/$NAME@1.0.0" '{target:$t,input:{n:40},wait:60}')" \
| jq '{id,status,output,error}'

Expected: valid is true, version 1.0.0 is published, and finally output is {"answer":42}. To change the draft use update a workflow draft with the draft.revision you read in the body. At publish, draft.revision must be a revision that has not changed since you read it, otherwise the call returns PRECONDITION_FAILED; read it again and republish. To test a draft without publishing, create a run with source and language: "yaml".

Limits

LimitValueWhen exceeded
One Action / run-sandbox5 min / 6 hThe step fails
Workflow run time24 h default, 30 days maximumRun is faulted, error code TIMEOUT
wait on create run0–60 sOnly how long the response waits; a non-terminal run returns 202, poll by id
One payload (input, output)1 MiBPAYLOAD_TOO_LARGE; store large content in a file and pass the file ID
payload of complete wait32 KiBPAYLOAD_TOO_LARGE
run-sandbox standard output32 KiBThe rest is truncated; use outputs to save large output as files
fork branches / nested concurrency10 / 25Validation returns fork 分支数或嵌套扇出超过限制
Step nesting levels10Validation returns 步骤嵌套超过限制
Task executions per run (loops and retries included) / for iterations4,000 / 1,000Run is faulted; detail says the task-execution or loop-iteration limit was exceeded (Chinese text: 任务执行次数超过上限 or 循环迭代次数超过上限)
Child-run nesting / child runs per run / active at once10 / 1,000 / 100Creating the child is rejected; title is 运行树深度超限 or 子运行数量超限. Beyond 100 active, children queue

Common errors

HTTP errors are problem responses: code is the stable error code, and errors[] lists each failure with its location pointer, the reason detail and a fix hint. A PraxisError thrown by the SDK is the same: the field list is in e.errors, and String(e) is only the summary. For a workflow, start with the first errors[] entry; later ones are often side effects of the same cause. The detail text of validation errors is in Chinese today and is quoted as returned.

SymptomCause and fix
缺少必填字段 versiondocument has no version; add an exact semver
Nested mappings are not allowed or Missing closing "quoteAn expression contains a colon or quote; wrap the whole value in single quotes
Unexpected flow-map-start, with a hint to quote the expressionA ${ … } is unquoted; single-quote the whole value, also inside a { } flow mapping
步骤必须且只能包含一种任务A step has two operations; split it into two steps
目标任务不存在于当前作用域then can only point at a step name in the same list
检查 Action IDcall is written as namespace::name or is not a string; use namespace/name
不支持内联 script.codeSave a script first, then reference it with run.script
暂不支持 emit 任务, 暂不支持 … 配置Only the listen subset above is supported and emit is not; this error is returned when a run is created, validation and publishing do not catch it
DEFINITION_IDENTITY_MISMATCHnamespace, name or version in document differs from the publish target
PRECONDITION_FAILEDThe draft changed after you read it; read draft.revision again and republish

More error codes are in limits and errors; the script side is in Write a script.

API reference → · Cookbook →

Workflow syntax reference: fields, data flow and the supported DSL subset.