You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add documentation for the StepContext object (#3953)
* Update metadata documentation for accessing context in steps
This update enhances the documentation on how to access the `StepContext` within steps. It provides detailed information on the availability of the context, how to retrieve it, and the properties it exposes, including pipeline and step run details, model information, and input metadata. Additionally, it includes examples demonstrating the usage of context in both standard steps and advanced scenarios involving hooks and materializers.
This change aims to improve clarity and usability for users working with step contexts in ZenML.
* Update docs/book/how-to/metadata/metadata.md
Co-authored-by: Copilot <[email protected]>
---------
Co-authored-by: Copilot <[email protected]>
@@ -303,29 +303,119 @@ When fetching metadata using a specific key, the returned value will always refl
303
303
304
304
### Accessing Context Within Steps
305
305
306
-
Within a step, you can access information about the current execution context using the `StepContext`:
306
+
The `StepContext` object is your handle to the *current* pipeline/step run while a step executes. Use it to read run/step information, inspect upstream input metadata, and work with step outputs: URIs, materializers, run metadata, and tags.
307
+
308
+
It is available:
309
+
- Inside functions decorated with `@step` (during execution, not composition time).
310
+
- Inside step hooks like `on_failure` / `on_success`.
311
+
- Inside materializers triggered by a step’s `save` / `load`.
*`ctx.step_run` → `StepRunResponse` (name, parameters via `ctx.step_run.config.parameters`, status).
332
+
*`ctx.model` → the configured `Model` (resolved from step or pipeline); raises if none configured.
333
+
*`ctx.inputs` → `{input_name: StepRunInputResponse}`; use `...["x"].run_metadata` to read upstream metadata.
334
+
*`ctx.step_name` → convenience name string.
335
+
336
+
### Working with outputs
337
+
338
+
For a single-output step you can omit `output_name`. For multi-output steps you **must** pass it (unnamed outputs are called `output_1`, `output_2`, …).
339
+
340
+
*`get_output_artifact_uri(output_name=None) -> str` – where the output artifact lives (write side files, etc.).
341
+
*`get_output_materializer(output_name=None, *, custom_materializer_class=None, data_type=None) -> BaseMaterializer` – get an initialized materializer; pass `data_type` to select from `Union[...]` materializers or `custom_materializer_class` to override.
342
+
*`add_output_metadata(metadata, output_name=None)` / `get_output_metadata(output_name=None)` – set/read run metadata for the output. Values provided via `ArtifactConfig(..., run_metadata=...)` on the return annotation are merged with runtime values.
343
+
*`add_output_tags(tags, output_name=None)` / `get_output_tags(output_name=None)` / `remove_output_tags(tags, output_name=None)` – manage tags for the produced artifact version. Configured tags via `ArtifactConfig(..., tags=...)` are unioned with runtime tags; duplicates are de‑duplicated in the final artifact.
344
+
345
+
Minimal example:
346
+
347
+
```python
348
+
from typing import Annotated, Tuple
349
+
from zenml import step, get_step_context, log_metadata
350
+
from zenml.artifacts.artifact_config import ArtifactConfig
351
+
352
+
@step
353
+
defproduce(name: str) -> Tuple[
354
+
Annotated[
355
+
str,
356
+
ArtifactConfig(
357
+
name="custom_name",
358
+
run_metadata={"config_metadata": "bar"},
359
+
tags=["config_tags"],
360
+
),
361
+
],
362
+
str,
363
+
]:
364
+
ctx = get_step_context()
365
+
# Attach metadata and tags to the named (or default) output
# Context is available while the step triggers materialization
400
+
data.meta = get_step_context().pipeline.name
401
+
super().save(data)
402
+
403
+
@step(on_failure=on_failure)
312
404
defmy_step():
313
-
# Get the step context
314
-
context = get_step_context()
315
-
316
-
# Access context information
317
-
pipeline_name = context.pipeline.name
318
-
run_name = context.pipeline_run.name
319
-
step_name = context.step_run.name
320
-
321
-
# Get information about output artifacts
322
-
output_uri = context.get_output_artifact_uri()
323
-
materializer = context.get_output_materializer()
324
-
325
-
# Use the context information
326
-
print(f"Executing step {step_name} in pipeline {pipeline_name}, run {run_name}")
405
+
raiseValueError("boom")
327
406
```
328
407
408
+
**Common errors to expect.**
409
+
410
+
*`RuntimeError` if `get_step_context()` is called outside a running step.
411
+
*`StepContextError` for output helpers when:
412
+
413
+
* The step has no outputs,
414
+
* You omit `output_name` on a multi‑output step,
415
+
* You reference an unknown `output_name`.
416
+
417
+
See the [full SDK docs for `StepContext`](https://sdkdocs.zenml.io/latest/core_code_docs/core-steps.html#zenml.steps.StepContext) for a concise reference to this object.
418
+
329
419
### Accessing Context During Pipeline Composition
330
420
331
421
During pipeline composition, you can access the pipeline configuration using the `PipelineContext`:
0 commit comments