How to use input, output, and export
This guide explains how to work with data flow in Lemline workflows, focusing on input validation, output transformation, and exporting data to the global context. You'll learn how to control data as it flows through your workflow.
Understanding Data Flow in Lemline
Lemline's data flow model consists of three main components:
Input: Data coming into a task, including validation and transformation
Output: Data produced by a task, including transformation and validation
Export: Data promoted to the global workflow context for use across the workflow
Understanding how to use these components effectively is key to building robust workflows.
Run the Examples
Each example below is a complete workflow. Complete the SQLite setup and keep its worker running. Run the commands from a second terminal in the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. The worker logs Workflow completed with output with each result. The examples that call HTTP services use the tutorial shop; start it from the root of the Lemline source checkout:
docker compose -f examples/tutorial-services/compose.yaml up -d
You can define an input schema at the workflow level to validate incoming data:
document:
dsl: '1.0.3'
namespace: examples
name: io-input-schema
version: '1.0.0'
title: Order Processing Workflow
input:
schema:
document:
type: object
required:
- orderId
- customerId
- items
properties:
orderId:
type: string
pattern: ^ORD-[0-9]{6}$
customerId:
type: string
items:
type: array
minItems: 1
items:
type: object
required:
- productId
- quantity
properties:
productId:
type: string
quantity:
type: integer
minimum: 1
do:
- acceptOrder:
set:
accepted: ${ .orderId }
Save it as defining-workflow-input-schema.yaml:
bin/lemline definition post -f defining-workflow-input-schema.yaml
bin/lemline instance start examples io-input-schema --input '{"orderId":"ORD-000123","customerId":"C-100","items":[{"productId":"SKU-1","quantity":2}]}'
bin/lemline instance start examples io-input-schema --input '{"orderId":"ORD-1","customerId":"C-100","items":[{"productId":"SKU-1","quantity":2}]}'
The first instance completes with {"accepted":"ORD-000123"}. The second is refused before any instance exists: the CLI prints Input validation failed against workflow schema, because ORD-1 does not match the pattern.
This schema ensures that workflow input contains the required fields with the correct data types and formats.
You can access the original workflow input in any task using $workflow.input:
document:
dsl: '1.0.3'
namespace: examples
name: accessing-workflow-input
version: '1.0.0'
do:
- summarize:
set:
itemCount: ${ .items | length }
- processOrder:
set:
orderId: ${ $workflow.input.orderId }
customerId: ${ $workflow.input.customerId }
itemCount: ${ .itemCount }
Save it as accessing-workflow-input.yaml:
bin/lemline definition post -f accessing-workflow-input.yaml
bin/lemline instance start examples accessing-workflow-input --input '{"orderId":"ORD-1","customerId":"C-100","items":[{"productId":"SKU-1"},{"productId":"SKU-2"}]}'
The second task only receives {"itemCount":2} from the first, but reads the IDs from $workflow.input: expect {"orderId":"ORD-1","customerId":"C-100","itemCount":2}.
You can provide default values for workflow input fields:
document:
dsl: '1.0.3'
namespace: examples
name: default-workflow-input
version: '1.0.0'
input:
from: '${ { priority: "NORMAL", currency: "USD" } + . }'
do:
- recordOrder:
set:
orderId: ${ .orderId }
priority: ${ .priority }
currency: ${ .currency }
Save it as default-workflow-input.yaml:
bin/lemline definition post -f default-workflow-input.yaml
bin/lemline instance start examples default-workflow-input --input '{"orderId":"ORD-2"}'
bin/lemline instance start examples default-workflow-input --input '{"orderId":"ORD-3","currency":"EUR"}'
Expect {"orderId":"ORD-2","priority":"NORMAL","currency":"USD"}, then {"orderId":"ORD-3","priority":"NORMAL","currency":"EUR"}. The workflow-level input.from expression merges defaults with the input before the first task runs. Values supplied by the caller take precedence. $workflow.input still holds the input as the caller supplied it.
By default, each task receives the output of the previous task as its input:
document:
dsl: '1.0.3'
namespace: examples
name: implicit-input
version: '1.0.0'
do:
- taskA:
set:
value: 42
message: Hello
- taskB:
set:
originalValue: ${ .value }
newMessage: ${ .message + ", World!" }
Save it as implicit-input.yaml:
bin/lemline definition post -f implicit-input.yaml
bin/lemline instance start examples implicit-input
Expect {"originalValue":42,"newMessage":"Hello, World!"}.
You can transform the input data before task execution:
document:
dsl: '1.0.3'
namespace: examples
name: input-transformation
version: '1.0.0'
do:
- formatData:
input:
from:
user: ${ .userProfile }
contactInfo:
email: ${ .userProfile.email | ascii_downcase }
phone: ${ .userProfile.phone | gsub("[^0-9]"; "") }
set:
formattedUser:
id: ${ .user.id }
name: ${ .user.name }
email: ${ .contactInfo.email }
phone: ${ .contactInfo.phone }
Save it as input-transformation.yaml:
bin/lemline definition post -f input-transformation.yaml
bin/lemline instance start examples input-transformation --input '{"userProfile":{"id":"C-100","name":"Ada Lovelace","email":"Ada@Example.com","phone":"+44 (20) 7946-0000"}}'
Expect {"formattedUser":{"id":"C-100","name":"Ada Lovelace","email":"ada@example.com","phone":"442079460000"}}.
The input.from field lets you reshape the incoming data before it's used in the task.
You can validate task input against a schema:
document:
dsl: '1.0.3'
namespace: examples
name: input-schema-validation
version: '1.0.0'
do:
- ValidatePaymentInfo:
set:
validPayment: true
formattedAmount: '${ "$" + (.amount | tostring) }'
input:
schema:
document:
type: object
required:
- amount
- currency
- paymentMethod
properties:
amount:
type: number
minimum: 0.01
currency:
type: string
enum:
- USD
- EUR
- GBP
paymentMethod:
type: object
required:
- type
properties:
type:
type: string
enum:
- CREDIT
- DEBIT
- PAYPAL
Save it as input-schema-validation.yaml:
bin/lemline definition post -f input-schema-validation.yaml
bin/lemline instance start examples input-schema-validation --input '{"amount":25,"currency":"EUR","paymentMethod":{"type":"CREDIT"}}'
bin/lemline instance start examples input-schema-validation --input '{"amount":25,"currency":"JPY","paymentMethod":{"type":"CREDIT"}}'
The first instance completes with {"validPayment":true,"formattedAmount":"$25"}. The second faults at the task with a validation error, because JPY is not one of the allowed currencies.
If the input doesn't match the schema, a validation error is raised.
Task Output Processing
Basic Output
The output of a set task is the object defined under set:
document:
dsl: '1.0.3'
namespace: examples
name: basic-output
version: '1.0.0'
do:
- generateOutput:
set:
status: SUCCESS
result: 42
Save it as basic-output.yaml:
bin/lemline definition post -f basic-output.yaml
bin/lemline instance start examples basic-output
Expect {"status":"SUCCESS","result":42}.
This task produces an output object with status, result, and timestamp fields.
You can transform the output data before passing it to the next task:
document:
dsl: '1.0.3'
namespace: examples
name: output-transformation
version: '1.0.0'
do:
- getCustomer:
call: http
with:
method: get
endpoint: ${ "http://127.0.0.1:8787/customers/" + .customerId }
output:
as:
customer: ${ .name }
tier: ${ .tier }
isGold: ${ .tier == "gold" }
Save it as output-transformation.yaml:
bin/lemline definition post -f output-transformation.yaml
bin/lemline instance start examples output-transformation --input '{"customerId":"C-100"}'
The shop returns the whole customer; output.as keeps three fields: {"customer":"Ada Lovelace","tier":"gold","isGold":true}.
The output.as field reshapes the task result before it's passed to the next task.
Output Schema Validation
You can validate task output against a schema:
document:
dsl: '1.0.3'
namespace: examples
name: output-schema-validation
version: '1.0.0'
do:
- GenerateUserProfile:
set:
profile:
id: ${ .userId }
name: ${ .firstName + " " + .lastName }
email: ${ .emailAddress }
memberSince: ${ .joinDate }
output:
schema:
document:
type: object
required:
- profile
properties:
profile:
type: object
required:
- id
- name
- email
properties:
id:
type: string
name:
type: string
email:
type: string
format: email
Save it as output-schema-validation.yaml:
bin/lemline definition post -f output-schema-validation.yaml
bin/lemline instance start examples output-schema-validation --input '{"userId":"C-100","firstName":"Ada","lastName":"Lovelace","emailAddress":"ada@example.com","joinDate":"2020-01-01"}'
bin/lemline instance start examples output-schema-validation --input '{"userId":"C-100","firstName":"Ada","lastName":"Lovelace","joinDate":"2020-01-01"}'
The first instance returns the profile. In the second, email is null, which is not a string: the task faults with a validation error.
If the output doesn't match the schema, a validation error is raised.
Exporting Data to Global Context
Basic Export
The export field lets you promote data to the global workflow context:
document:
dsl: '1.0.3'
namespace: examples
name: basic-export
version: '1.0.0'
do:
- fetchCustomerData:
call: http
with:
method: get
endpoint: ${ "http://127.0.0.1:8787/customers/" + .customerId }
export:
as:
customer: ${ . }
- greetCustomer:
set:
greeting: ${ "Hello " + $context.customer.name }
Save it as basic-export.yaml:
bin/lemline definition post -f basic-export.yaml
bin/lemline instance start examples basic-export --input '{"customerId":"C-100"}'
The second task reads the exported customer: expect {"greeting":"Hello Ada Lovelace"}.
Later tasks can read the exported customer data from $context.customer.
Exporting Selected Fields
You can export specific fields rather than the entire output:
document:
dsl: '1.0.3'
namespace: examples
name: exporting-selected-fields
version: '1.0.0'
do:
- getProduct:
call: http
with:
method: get
endpoint: ${ "http://127.0.0.1:8787/products/" + .productId }
export:
as:
productName: ${ .name }
unitPrice: ${ .price }
- showContext:
set:
context: ${ $context }
Save it as exporting-selected-fields.yaml:
bin/lemline definition post -f exporting-selected-fields.yaml
bin/lemline instance start examples exporting-selected-fields --input '{"productId":"SKU-2"}'
The last task shows the context: {"context":{"productName":"Watering can","unitPrice":12}}.
This exports only the selected fields to the global context.
You can transform data during export:
document:
dsl: '1.0.3'
namespace: examples
name: export-with-transformation
version: '1.0.0'
do:
- fetchProductData:
call: http
with:
method: get
endpoint: http://127.0.0.1:8787/products
query:
size: '10'
export:
as:
products: ${ .items }
productMap: '${ .items | map({key: .id, value: .name}) | from_entries }'
categories: ${ .items | map(.category) | unique }
- showContext:
set:
context: ${ $context }
Save it as export-with-transformation.yaml:
bin/lemline definition post -f export-with-transformation.yaml
bin/lemline instance start examples export-with-transformation
The context holds the seven products, a productMap from ID to name, and categories: ["clothing","garden","home"].
This exports the original products array plus derived data structures.
Export Schema Validation
You can validate exported data against a schema:
document:
dsl: '1.0.3'
namespace: examples
name: io-export-schema-validation
version: '1.0.0'
do:
- validateExportedData:
call: http
with:
method: get
endpoint: ${ "http://127.0.0.1:8787/products/" + .productId }
export:
as:
validatedProduct: ${ . }
schema:
document:
type: object
required:
- validatedProduct
properties:
validatedProduct:
type: object
required:
- id
- name
- category
properties:
id:
type: string
name:
type: string
category:
type: string
enum:
- garden
- home
- showContext:
set:
context: ${ $context }
Save it as export-schema-validation.yaml:
bin/lemline definition post -f export-schema-validation.yaml
bin/lemline instance start examples io-export-schema-validation --input '{"productId":"SKU-1"}'
bin/lemline instance start examples io-export-schema-validation --input '{"productId":"SKU-6"}'
SKU-1 is a garden product: the context holds it. SKU-6 is clothing, which the schema does not allow: the task faults with a validation error.
This ensures the exported data meets the specified schema requirements.
Comprehensive Example: Order Processing
This workflow validates the input, routes the order based on its total, and returns the order ID and processing status.
document:
dsl: '1.0.3'
namespace: examples
name: validate-and-route-order
version: '1.0.0'
input:
schema:
format: json
document:
type: object
required: [orderId, total]
properties:
orderId:
type: string
total:
type: number
minimum: 0
do:
- routeOrder:
switch:
- largeOrder:
when: ${ .total >= 1000 }
then: requestReview
- automatic:
then: acceptOrder
- requestReview:
set:
status: needs-review
then: exit
- acceptOrder:
set:
status: accepted
output:
as: '${ { orderId: $workflow.input.orderId, status: .status } }'
Save it as validate-and-route-order.yaml:
bin/lemline definition post -f validate-and-route-order.yaml
bin/lemline instance start examples validate-and-route-order --input '{"orderId":"ORD-301","total":250}'
bin/lemline instance start examples validate-and-route-order --input '{"orderId":"ORD-302","total":1500}'
Expect {"orderId":"ORD-301","status":"accepted"} and {"orderId":"ORD-302","status":"needs-review"}.
Advanced Patterns
Chain of Responsibility
Chain multiple data transformations for complex processing:
document:
dsl: '1.0.3'
namespace: examples
name: chain-of-responsibility
version: '1.0.0'
do:
- step1:
set:
intermediateResult: ${ .rawData | fromjson }
- step2:
set:
processedData: ${ .intermediateResult | map(.value) }
- step3:
set:
finalResult: ${ .processedData | add }
export:
as:
result: ${ .finalResult }
- showContext:
set:
context: ${ $context }
Save it as chain-of-responsibility.yaml:
bin/lemline definition post -f chain-of-responsibility.yaml
bin/lemline instance start examples chain-of-responsibility --input '{"rawData":"[{\"value\":1},{\"value\":2},{\"value\":3}]"}'
Expect {"context":{"result":6}}.
Selective Processing with Condition-Based Export
Export different data based on conditions:
document:
dsl: '1.0.3'
namespace: examples
name: selective-processing-with-condition-based-export
version: '1.0.0'
do:
- conditionalExport:
set:
status: '${ if .code == 200 then "SUCCESS" else "ERROR" end }'
successData: ${ if .code == 200 then .data else null end }
errorData: ${ if .code != 200 then .error else null end }
export:
as:
result: '${ if .status == "SUCCESS" then .successData else .errorData end }'
isError: ${ .status == "ERROR" }
- showContext:
set:
context: ${ $context }
Save it as selective-processing-with-condition-based-export.yaml:
bin/lemline definition post -f selective-processing-with-condition-based-export.yaml
bin/lemline instance start examples selective-processing-with-condition-based-export --input '{"code":200,"data":{"orderId":"ORD-1"}}'
bin/lemline instance start examples selective-processing-with-condition-based-export --input '{"code":500,"error":"Payment service unavailable"}'
Expect {"context":{"result":{"orderId":"ORD-1"},"isError":false}}, then {"context":{"result":"Payment service unavailable","isError":true}}.
Parameterized Tasks
Use exported data as task parameters:
document:
dsl: '1.0.3'
namespace: examples
name: share-order-context
version: '1.0.0'
do:
- identifyCustomer:
set:
customerId: ${ .customer.id }
export:
as: '${ $context + { customerId: .customerId } }'
- prepareOrder:
set:
orderId: ${ $workflow.input.orderId }
customerId: ${ $context.customerId }
Save it as share-order-context.yaml:
bin/lemline definition post -f share-order-context.yaml
bin/lemline instance start examples share-order-context --input '{"orderId":"ORD-803","customer":{"id":"C-100"}}'
Expect {"orderId":"ORD-803","customerId":"C-100"}.
Stop the shop when you are done:
docker compose -f examples/tutorial-services/compose.yaml down
Best Practices
Validate Early: Add schema validation as early as possible in the workflow
Use Specific Schemas: Create detailed, specific schemas for validation
Transform Near Use: Do data transformations close to where the data is used
Export Judiciously: Only export data needed by multiple tasks
Document the Data Flow: Add comments to explain complex transformations
Use Meaningful Names: Name exported data with clear, descriptive identifiers
Prefer Task Chaining: Pass data through task chains rather than global context when possible
Validate Exported Data: Add schemas to exported data for integrity
Minimize Global State: Avoid exporting too much to the global context
Clean Structure: Structure data consistently across the workflow
Common Errors and Solutions
Schema Validation Errors
Problem: Input data doesn't match the schema. Solution:
Verify data structure matches schema requirements
Check for missing required fields
Ensure data types are correct
Use default values for optional fields
Missing Context Variables
Problem: Accessing a field in $context that has not been exported. Solution:
Ensure the variable is exported before use
Check for typos in variable names
Add a fallback such as $context.variable // defaultValue.
Problem: jq transformation expressions fail. Solution:
Break complex transformations into simpler steps
Use try/catch in expressions for error handling
Add explicit type handling
Debug with a separate diagnostic task
25 November 2026