Skip to main content
Macros are trusted project-local Python functions. StreamBuild loads one deterministic registry per compile invocation and expands calls before SQL analysis.
Public synchronous functions defined by discovered modules become macros. Private functions, imported functions, async functions, __init__.py, private modules, and private directories are not registered.

Define and call a macro

Macros expand in model, test, and audit SQL bodies. They do not expand inside MODEL(), TEST(), or AUDIT() headers.

Arguments and nesting

Macro calls accept Python literals: strings, numbers, booleans, None, lists, dictionaries, tuples, and named arguments.
Nested calls resolve from the inside out. Inner calls may return structured Python values; a call inserted directly into SQL must return a string.
Macro output may not contain a new unexpanded @macro(...) call. Compose macros by calling Python functions directly instead.

Macro context

A first parameter named ctx requests the immutable invocation context. Annotate it as MacroContext:
The context exposes:
Structured project variables remain mappings in ctx.variables and are deeply immutable.

Loading and errors

Macro modules execute as trusted project code during compilation. StreamBuild reports syntax, import-time, duplicate-name, unknown-call, argument, return-type, and nested-expansion failures with both call and definition locations.