Back to examples
Artifacts
JSON Artifact
Persist structured data as a JSON file artifact.
When to use
Use this when a task produces structured output (metrics, configs, results) that should be stored as a downloadable JSON file.
artifacts/json-artifact.py
"""
02_json_artifact.py: Producing a JSON LocalArtifact.
Demonstrates:
- Returning LocalArtifact(type="json", value=json.dumps(...), ...)
- Storing structured data as an artifact rather than passing it downstream
"""
import json
from dagy import LocalArtifact, flow, task
@task
def collect_metrics(run_id: str) -> dict:
return {
"run_id": run_id,
"rows_processed": 1_024,
"errors": 0,
"duration_seconds": 3.7,
"tags": ["nightly", "batch"],
}
@task
def save_metrics(metrics: dict) -> LocalArtifact:
return LocalArtifact(
type="json",
value=json.dumps(metrics, indent=2),
filename="metrics.json",
)
@flow(name="json_artifact_flow")
def json_artifact_flow(run_id: str = "run-001") -> None:
metrics = collect_metrics(run_id)
save_metrics(metrics)
if __name__ == "__main__":
result = json_artifact_flow.run_local(run_id="run-042")
print(f"Run ID : {result.run_id}")
print(f"Status : {result.status}")How it works
- `collect_metrics` gathers run metrics into a dict.
- `save_metrics` serialises the dict with `json.dumps` and wraps it in a `LocalArtifact(type="json", ...)`.
- DAGY stores the JSON file alongside the run.
Inputs
- `run_id` (str, default `"run-001"`): identifier for the metrics.
Outputs
- A `metrics.json` file stored as a run artifact.
Dependencies
`dagy`, `LocalArtifact`, `json` (stdlib)