Skip to content

Python nodes

Discovered nodes

Run a Python asset once per item an external system lists, with @discover and @asset(for_each=...).

A @discover function queries an external system for items, and an @asset(for_each=...) runs once per item. Locations call discover functions at publish time and save the result in the snapshot so every run executes the exact published graph.

python/nodes/fulfilment/nodes.py
from sqlbuild.assets import AssetContext, asset
from sqlbuild.discovery import Discovered, Item, discover
from sqlbuild.refs import model
@discover(check_every="15m")
def fulfilment_plan(warehouse_api) -> Discovered:
"""Steps of today's fulfilment plan."""
plan = warehouse_api.todays_plan()
return Discovered(
items=[
Item(id=step["id"], depends_on=step["after"], args={"region": step["region"]})
for step in plan["steps"]
],
tags={"plan_version": plan["version"]},
max_parallel=4,
)
@asset(for_each=fulfilment_plan, depends_on=model("orders"))
def fulfil(ctx: AssetContext, region: str) -> None:
"""Run one step of the plan."""
ctx.log.info(f"step {ctx.discovered.item} for {region}, plan {ctx.discovered.tags['plan_version']}")

With items pick -> pack -> ship, the graph becomes orders -> fulfil[pick] -> fulfil[pack] -> fulfil[ship].

  • @discover marks a function under python/nodes/ returning Discovered (supports provider injection, optional name=, and optional check_every=, minimum 1m).
  • Discovered(items, tags=..., max_parallel=...): items must be non-empty; tags and item args must be JSON-compatible.
  • Item(id, depends_on=..., args=...): id is unique within the set (max 200 chars, alphanumeric and _ . : -). depends_on accepts sibling item IDs or model(), source(), seed(), job(), tag() refs. args bind to the asset’s non-context/non-provider parameters.
  • @asset(for_each=...) generates one node per item named <asset>[<item_id>]. The asset’s own depends_on attaches to the set’s root items; downstream job() / tag() selectors depend on its leaf items. Each node’s identity includes its item’s args and the set’s tags.

Invalid or empty discovery results fail the set as a whole and retain its last good items.

Attribute Description
ctx.discovered.item Item ID (None on non-for_each assets)
ctx.discovered.set Discover function name
ctx.discovered.tags Saved set tags
ctx.root_run_id First server run ID across reruns/retries (local run ID outside a worker)
Selector Selects
fulfil[pack] Single item node
+fulfil[ship] Item ship and all upstream items/nodes
fulfil or asset:fulfil All items of the asset (use in jobs so job files stay stable)

max_parallel caps concurrent items for a discover set within one run (shared across assets using that discover function, independent of concurrency):

  • Shared providers must be thread-safe; steps run on worker threads and cannot install signal handlers.
  • Each parallel step gets its own warehouse connection (in-memory DuckDB, MotherDuck, and SQLite serialize to 1).
  • On cancellation, running statements cancel and SQLBuild waits up to 5 seconds for active Python steps before exiting; providers are not torn down if a step is still running after 5 seconds.

Only locations (and sqb dev) run @discover functions automatically:

Trigger Behavior
Location startup, r in sqb dev, or republish Runs all discover functions and publishes
Project file change Reuses saved sets unless the discover module itself changed
check_every timer Checks in background; republishes only if items or tags changed

Runs, reruns, and plan previews always compile from the snapshot’s saved sets. If a discover set fails, it keeps its last good items (unless those reference deleted models/tags) and flags Discovery failed on the location; a set that has never succeeded compiles as a placeholder <asset>[*] that fails if executed.

Local sqb compile, sqb plan, and sqb build compile for_each assets as <asset>[*] placeholders without calling @discover. Pass --discover to evaluate discover functions live for one invocation without writing to disk:

sqb build --discover --select +fulfil[ship]