1. Building blocks
  2. Python steps

A Python step runs your code as one step of a workflow. No model is involved. Use it for rules, calculations, API calls and anything with a consequence, such as sending or saving.

import avalanche as ava


@ava.step
def invoice_total(items: list[LineItem], discount_percent: int = 0) -> int:
    """Amount due in cents."""
    subtotal = sum(item.quantity * item.unit_price_cents for item in items)
    return subtotal * (100 - discount_percent) // 100

The parameters are the step’s inputs. The return value is its output, passed to the next step. The docstring is shown on the step’s card in the UI. async def works too.

​
Three decorators, one kind of step

All three run plain Python. The name tells the reader what the step is for.

DecoratorUse it for
@ava.sourceBringing data in
@ava.stepWorking on data
@ava.destSending or saving a result
@ava.source
def load_orders() -> list[Order]: ...

@ava.step
def keep_unpaid(orders: list[Order]) -> list[Order]: ...

@ava.dest
def send_reminders(orders: list[Order]) -> int: ...

@ava.workflow
def reminders():
    return load_orders() >> keep_unpaid() >> send_reminders()
Rendering diagram…

​
Options

@ava.step(retries=2, slug="total")
def invoice_total(...): ...
OptionDefaultWhat it does
retries7Extra attempts after a failure.
on_uncertain"retry"What to do when it is unknown whether the step’s action happened: "retry", "hold" or "fail".
slugfunction nameThe step’s name in runs and reruns.
num_returns1Set to 2 or more when the step returns several values used separately.
integrationsnoneConnectors the step calls. @ava.step only.

​
What a step can ask for

A step receives more than the previous step’s output. Declare a parameter and Avalanche fills it.

@ava.step
def load(config: InvoiceInput, ctx: ava.RunContext, log=ava.Logger()) -> str:
    log.info(f"run {ctx.run_id}")
    return config.text
ParameterYou get
Typed with the workflow’s input modelThe run input
ava.RunContextThe run ID, workflow name and step name
ava.Logger() as a defaultA logger whose lines appear in the run’s logs

​
Stop without failing

@ava.step
def score(lead: Lead) -> Score:
    if not lead.qualified:
        ava.skip("Lead is not qualified")
    ...

ava.skip ends the step as skipped. The run still succeeds, and the reason is shown on the step.