Skip to main content

Artifact Handling

Artifacts are durable files attached to evidence. Prefer files that were produced or consumed by the observed operation: a fitted model, a checkpoint, an evaluation set, a prompt template, or a generated report.

Executable Artifact Examples

The examples below register output files when the workflow produces them. Other runtime evidence is captured within the same run context.

"""Persist a fitted regression model and register it as observed evidence."""

import pickle
from pathlib import Path

from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split

from contexta import Contexta
from contexta.capture import LocalJsonlSink


features, targets = load_diabetes(return_X_y=True)
train_x, test_x, train_y, test_y = train_test_split(
features, targets, test_size=0.2, random_state=42
)

workspace = Path(".contexta")
ctx = Contexta(workspace=str(workspace), config={"project_name": "diabetes-artifact"})
local_sink = next(sink for sink in ctx.sinks if isinstance(sink, LocalJsonlSink))
model = LinearRegression()

with ctx.run("fitted-model", dataset_ref="dataset:sklearn.diabetes") as run:
with run.stage("train"):
model.fit(train_x, train_y)

with run.stage("evaluate") as stage:
r2 = r2_score(test_y, model.predict(test_x))
stage.metric("r2", r2, unit="ratio")

model_path = workspace / "models" / "linear-regression.pkl"
model_path.parent.mkdir(parents=True, exist_ok=True)
model_path.write_bytes(pickle.dumps(model))
registration = run.register_artifact(
"model",
str(model_path),
attributes={"framework": "scikit-learn", "format": "pickle"},
)

artifact_ref = registration.payload["manifest"].artifact_ref
artifacts_path = local_sink.file_path_for("ARTIFACT").relative_to(Path.cwd())

print(f"Captured run: {run.ref}")
print(f"Measured r2: {r2:.3f}")
print(f"Registered artifact: {artifact_ref}")
print(f"Model file: {model_path.as_posix()}")
print(f"Artifact records: {artifacts_path.as_posix()}")

Naming Guidance

Use lower snake case for artifact kinds: model, checkpoint, prompt_template, eval_set, schema, report.

Do not create a text file labelled "model" or "checkpoint" merely to make an example look complete. When a page describes training, register the artifact actually written by the fitted model or framework.