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.
from sqlbuild.assets import AssetContext, assetfrom sqlbuild.discovery import Discovered, Item, discoverfrom 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].
Declaring a discovered set
Section titled “Declaring a discovered set”@discovermarks a function underpython/nodes/returningDiscovered(supports provider injection, optionalname=, and optionalcheck_every=, minimum1m).Discovered(items, tags=..., max_parallel=...):itemsmust be non-empty;tagsand itemargsmust be JSON-compatible.Item(id, depends_on=..., args=...):idis unique within the set (max 200 chars, alphanumeric and_ . : -).depends_onaccepts sibling item IDs ormodel(),source(),seed(),job(),tag()refs.argsbind to the asset’s non-context/non-provider parameters.@asset(for_each=...)generates one node per item named<asset>[<item_id>]. The asset’s owndepends_onattaches to the set’s root items; downstreamjob()/tag()selectors depend on its leaf items. Each node’s identity includes its item’sargsand the set’stags.
Invalid or empty discovery results fail the set as a whole and retain its last good items.
In a node
Section titled “In a node”| 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) |
Selecting discovered nodes
Section titled “Selecting discovered nodes”| 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) |
Running items in parallel
Section titled “Running items in parallel”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.
When discovery runs
Section titled “When discovery runs”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.
Outside a location
Section titled “Outside a location”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]