Lifecycle Hooks¶
Hooks let you observe pipeline execution without modifying pipeline logic. Use them for logging, metrics, progress tracking, or integration with external systems.
Minimal example¶
from accrue import Pipeline, EnrichmentHooks
hooks = EnrichmentHooks(
on_step_end=lambda e: print(f"Step '{e.step_name}' done in {e.elapsed_seconds:.1f}s"),
)
result = pipeline.run(data, hooks=hooks)
Full hook registration¶
from accrue import EnrichmentHooks
hooks = EnrichmentHooks(
on_pipeline_start=lambda e: print(f"Starting {len(e.step_names)} steps, {e.num_rows} rows"),
on_step_start=lambda e: print(f" Step '{e.step_name}' starting ({e.num_rows} rows)"),
on_step_end=lambda e: print(f" Step '{e.step_name}' done in {e.elapsed_seconds:.1f}s"),
on_row_complete=lambda e: log_row(e),
on_pipeline_end=lambda e: print(f"Done. {e.total_errors} errors in {e.elapsed_seconds:.1f}s"),
)
result = pipeline.run(data, hooks=hooks)
Event types¶
PipelineStartEvent¶
Fired once at the beginning of pipeline execution.
| Field | Type | Description |
|---|---|---|
step_names |
list[str] |
Ordered list of step names |
num_rows |
int |
Total rows in the input data |
config |
EnrichmentConfig |
The config used for this run |
PipelineEndEvent¶
Fired once at the end of pipeline execution (including on error).
| Field | Type | Description |
|---|---|---|
num_rows |
int |
Total rows processed |
total_errors |
int |
Number of row-level errors |
cost |
CostSummary |
Aggregated token usage |
elapsed_seconds |
float |
Wall-clock time for the full run |
StepStartEvent¶
Fired before each step begins processing.
| Field | Type | Description |
|---|---|---|
step_name |
str |
Name of the step |
num_rows |
int |
Rows to process in this step |
level |
int |
DAG level (0-based) |
execution_mode |
str |
"realtime" or "batch" |
StepEndEvent¶
Fired after each step finishes.
| Field | Type | Description |
|---|---|---|
step_name |
str |
Name of the step |
num_rows |
int |
Rows processed |
num_errors |
int |
Row-level errors in this step |
usage |
StepUsage |
Token usage for this step |
elapsed_seconds |
float |
Wall-clock time for this step |
execution_mode |
str |
"realtime" or "batch" |
batch_id |
str \| None |
First batch job ID (batch mode only) |
batch_ids |
list[str] |
Every batch job ID the step submitted, in order — more than one when requests were split by count or bytes |
RowCompleteEvent¶
Fired after each individual row completes within a step.
| Field | Type | Description |
|---|---|---|
step_name |
str |
Name of the step |
row_index |
int |
Index of the completed row |
values |
dict[str, Any] |
Enrichment values produced |
error |
BaseException \| None |
Error if the row failed |
from_cache |
bool |
Whether the result came from cache |
skipped |
bool |
Whether the row was skipped: by run_if/skip_if, or because a step it depends on errored or was skipped on the row (values is then all None) |
usage |
UsageInfo \| None |
Per-row token usage when available (realtime LLM rows, every billed attempt, including rows that failed); None for function steps, cache hits, skipped rows, and batch mode |
elapsed_ms |
float \| None |
Wall-clock milliseconds for this row (excludes queue wait); None in batch mode |
RowAttemptEvent¶
Fired once per LLM provider attempt, inside LLMStep.run()'s retry loops — so a cell that retries emits several before its single RowCompleteEvent, and a cell that succeeds first try emits one. Only LLM steps fire it; function steps do not.
| Field | Type | Description |
|---|---|---|
step_name |
str |
Name of the step |
row_index |
int |
Index of the row being attempted |
attempt |
int |
1-based attempt number, counted across both retry loops |
kind |
str |
"api" (the provider call raised LLMAPIError) or "parse" (the call returned and was parsed/validated, whether or not that succeeded) |
status |
str |
Short outcome: "ok", "rate_limited", "timeout", "api_error", "parse_error", "validation_error" |
latency_ms |
float \| None |
Wall-clock milliseconds for this attempt's provider call |
backoff_s |
float \| None |
Seconds slept before the next attempt (set on a retrying api attempt), else None |
error |
BaseException \| None |
The attempt's exception, or None on success |
body |
dict \| None |
Rendered {messages, response, parsed} for the attempt, present only when the run's capture tier is >= "prompts"; None at the default metadata tier |
hooks = EnrichmentHooks(
on_row_attempt=lambda e: print(f"{e.step_name} row {e.row_index}: {e.kind}/{e.status}"),
)
Async hooks¶
Both sync and async callables work. Async hooks are awaited automatically:
async def log_step_end(event):
await metrics_client.record(
step=event.step_name,
duration=event.elapsed_seconds,
errors=event.num_errors,
)
hooks = EnrichmentHooks(on_step_end=log_step_end)
Practical patterns¶
Tracking batch progress:
hooks = EnrichmentHooks(
on_step_start=lambda e: print(
f"Step '{e.step_name}' [{e.execution_mode}] starting"
),
on_step_end=lambda e: print(
f"Step '{e.step_name}' [{e.execution_mode}] "
f"batch_id={e.batch_id} "
f"done in {e.elapsed_seconds:.1f}s"
),
)
Counting cache hits per step:
def report_caching(event):
usage = event.usage
if usage.cache_hits > 0:
print(
f"Step '{event.step_name}': "
f"{usage.cache_hits} cache hits, "
f"{usage.cache_misses} misses "
f"({usage.cache_hit_rate:.0%} hit rate)"
)
hooks = EnrichmentHooks(on_step_end=report_caching)
Error alerting:
def alert_on_errors(event):
if event.error is not None:
send_alert(f"Row {event.row_index} failed in '{event.step_name}': {event.error}")
hooks = EnrichmentHooks(on_row_complete=alert_on_errors)
Importing event types¶
from accrue import (
EnrichmentHooks,
PipelineStartEvent,
PipelineEndEvent,
StepStartEvent,
StepEndEvent,
RowCompleteEvent,
RowAttemptEvent,
)
Gotchas¶
- All fields are optional. Only subscribe to events you care about. Unset hooks are simply not called.
- Hook errors never crash the pipeline. If a hook raises an exception, it is caught and logged as a warning. Pipeline execution continues normally.
- Hooks are passed to
run(), not stored on config. This keepsEnrichmentConfigserializable (plain data) while hooks can be arbitrary callables. on_row_completefires for every row in every step. For a pipeline with 3 steps and 1,000 rows, it fires 3,000 times. Keep the callback lightweight.PipelineEndEventfires even on error. Use it for cleanup or final reporting regardless of success or failure.