Skip to content

Pipeline Runner

The pipeline runner is the executable vertical slice:

  1. Load PipelineConfig from YAML.
  2. Build the configured model wrapper.
  3. Instantiate registered probes, monitors, and attributors.
  4. Run each dataset item.
  5. Aggregate SafetyReport objects into a RunReport.
  6. Write the report JSON to output.report_path.

Run it with:

safelens run --config examples/config.yaml

Validate before running:

safelens validate --config examples/config.yaml

Use the runner from Python:

from SafeLens.pipelines import run_from_config

report = run_from_config("examples/config.yaml")
print(report.summary)

YAML-driven pipeline runner.

PipelineRunner

Instantiate registered methods and execute a full safety scan.

Source code in src/SafeLens/pipelines/runner.py
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class PipelineRunner:
    """Instantiate registered methods and execute a full safety scan."""

    def __init__(self, config: PipelineConfig) -> None:
        load_builtin_methods()
        self.config = config
        self.model = build_model_wrapper(config.model)
        self.probes = [create_probe(spec.name, spec.config) for spec in config.pipeline.probes]
        self.monitors = [
            create_monitor(spec.name, spec.config) for spec in config.pipeline.monitors
        ]
        self.attributors = [
            create_attributor(spec.name, spec.config) for spec in config.pipeline.attributors
        ]

    @classmethod
    def from_yaml(cls, path: str | Path) -> PipelineRunner:
        """Construct a runner from a YAML file."""
        return cls(load_pipeline_config(path))

    def setup(self) -> None:
        """Load the model and attach configured runtime hooks."""
        self.model.load_model()
        for probe, spec in zip(self.probes, self.config.pipeline.probes, strict=True):
            layers = spec.config.get("layers", [])
            probe.attach(self.model, layers)
        for monitor in self.monitors:
            monitor.start_monitoring(self.model)
        for attributor in self.attributors:
            attributor.attach(self.model)

    def run(self, dataset: Iterable[Mapping[str, Any]] | None = None) -> RunReport:
        """Run the configured safety pipeline and write a JSON report."""
        batches: list[Mapping[str, Any]] = list(
            dataset if dataset is not None else self.config.dataset
        )
        if not batches:
            batches = [{"id": "demo", "text": "This is a benign SafeLens demo sample."}]
        self._provide_dataset_to_methods(batches)
        batches = self._filter_dataset_for_methods(batches)
        primary_error = False
        try:
            try:
                self.setup()
                reports = [self._run_one(index, batch) for index, batch in enumerate(batches)]
                run_report = RunReport(reports=reports, summary=self._summarize(reports))
                self._write_report(run_report)
                return run_report
            except BaseException:
                primary_error = True
                raise
        finally:
            cleanup_error = self._cleanup_after_run()
            if cleanup_error is not None and not primary_error:
                raise cleanup_error

    def _run_one(self, index: int, batch: Mapping[str, Any]) -> SafetyReport:
        model_output, _cache = self.model.run_with_cache(batch)
        probe_results = [probe.detect(batch) for probe in self.probes]
        monitoring_signals = [monitor.step(batch, model_output) for monitor in self.monitors]
        attributions = [
            attributor.attribute_input(batch, model_output) for attributor in self.attributors
        ]
        return self._build_report(index, batch, probe_results, monitoring_signals, attributions)

    def _build_report(
        self,
        index: int,
        batch: Mapping[str, Any],
        probe_results: list[ProbeResult],
        monitoring_signals: list[MonitoringSignal],
        attributions: list[AttributionResult],
    ) -> SafetyReport:
        max_probe_score = max((result.risk_score for result in probe_results), default=0.0)
        max_monitor_score = max((signal.risk_score for signal in monitoring_signals), default=0.0)
        risk_score = max(max_probe_score, max_monitor_score)
        categories = self._collect_categories(probe_results, monitoring_signals)
        evidence_tokens = self._collect_evidence(probe_results, monitoring_signals, attributions)
        attribution_score = max((item.attribution_score for item in attributions), default=0.0)
        flagged = risk_score >= self.config.pipeline.risk_threshold or any(
            signal.triggered for signal in monitoring_signals
        )

        return SafetyReport(
            sample_id=str(batch.get("id", index)),
            flagged=flagged,
            risk_score=risk_score,
            risk_category=categories if categories else (["unknown"] if flagged else []),
            evidence_tokens=evidence_tokens,
            attribution_score=attribution_score,
            probe_results=probe_results,
            monitoring_signals=monitoring_signals,
            attributions=attributions,
            metadata={"input_keys": sorted(batch.keys())},
        )

    @staticmethod
    def _collect_categories(
        probe_results: list[ProbeResult],
        monitoring_signals: list[MonitoringSignal],
    ) -> list[str]:
        categories: set[str] = set()
        for result in probe_results:
            raw_category = result.details.get("risk_category", [])
            categories.update(str(category) for category in _iter_detail_values(raw_category))
        for signal in monitoring_signals:
            categories.update(signal.risk_category)
        return sorted(categories)

    @staticmethod
    def _collect_evidence(
        probe_results: list[ProbeResult],
        monitoring_signals: list[MonitoringSignal],
        attributions: list[AttributionResult],
    ) -> list[int]:
        tokens: set[int] = set()
        for result in probe_results:
            for token in _iter_detail_values(result.details.get("evidence_tokens", [])):
                try:
                    tokens.add(int(token))
                except (TypeError, ValueError):
                    continue
        for signal in monitoring_signals:
            tokens.update(signal.evidence_tokens)
        for attribution in attributions:
            tokens.update(token.token_index for token in attribution.tokens)
        return sorted(tokens)

    @staticmethod
    def _summarize(reports: list[SafetyReport]) -> dict[str, Any]:
        return {
            "samples_scanned": len(reports),
            "flagged_count": sum(1 for report in reports if report.flagged),
            "max_risk_score": max((report.risk_score for report in reports), default=0.0),
        }

    def _write_report(self, run_report: RunReport) -> None:
        path = Path(self.config.output.report_path)
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(json.dumps(run_report.to_dict(), indent=2), encoding="utf-8")

    def _cleanup_after_run(self) -> Exception | None:
        first_error: Exception | None = None
        for attributor in self.attributors:
            try:
                attributor.detach()
            except Exception as exc:
                if first_error is None:
                    first_error = exc
        for probe in self.probes:
            try:
                probe.detach()
            except Exception as exc:
                if first_error is None:
                    first_error = exc
        try:
            self.model.remove_hooks()
        except Exception as exc:
            if first_error is None:
                first_error = exc
        return first_error

    def _provide_dataset_to_methods(self, batches: list[Mapping[str, Any]]) -> None:
        for method in [*self.probes, *self.monitors, *self.attributors]:
            set_dataset = getattr(method, "set_dataset", None)
            if callable(set_dataset):
                set_dataset(batches)

    def _filter_dataset_for_methods(
        self,
        batches: list[Mapping[str, Any]],
    ) -> list[Mapping[str, Any]]:
        filtered = batches
        for method in [*self.probes, *self.monitors, *self.attributors]:
            filter_dataset = getattr(method, "filter_dataset", None)
            if callable(filter_dataset):
                filtered = list(cast(Iterable[Mapping[str, Any]], filter_dataset(filtered)))
        return filtered

from_yaml(path) classmethod

Construct a runner from a YAML file.

Source code in src/SafeLens/pipelines/runner.py
50
51
52
53
@classmethod
def from_yaml(cls, path: str | Path) -> PipelineRunner:
    """Construct a runner from a YAML file."""
    return cls(load_pipeline_config(path))

run(dataset=None)

Run the configured safety pipeline and write a JSON report.

Source code in src/SafeLens/pipelines/runner.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def run(self, dataset: Iterable[Mapping[str, Any]] | None = None) -> RunReport:
    """Run the configured safety pipeline and write a JSON report."""
    batches: list[Mapping[str, Any]] = list(
        dataset if dataset is not None else self.config.dataset
    )
    if not batches:
        batches = [{"id": "demo", "text": "This is a benign SafeLens demo sample."}]
    self._provide_dataset_to_methods(batches)
    batches = self._filter_dataset_for_methods(batches)
    primary_error = False
    try:
        try:
            self.setup()
            reports = [self._run_one(index, batch) for index, batch in enumerate(batches)]
            run_report = RunReport(reports=reports, summary=self._summarize(reports))
            self._write_report(run_report)
            return run_report
        except BaseException:
            primary_error = True
            raise
    finally:
        cleanup_error = self._cleanup_after_run()
        if cleanup_error is not None and not primary_error:
            raise cleanup_error

setup()

Load the model and attach configured runtime hooks.

Source code in src/SafeLens/pipelines/runner.py
55
56
57
58
59
60
61
62
63
64
def setup(self) -> None:
    """Load the model and attach configured runtime hooks."""
    self.model.load_model()
    for probe, spec in zip(self.probes, self.config.pipeline.probes, strict=True):
        layers = spec.config.get("layers", [])
        probe.attach(self.model, layers)
    for monitor in self.monitors:
        monitor.start_monitoring(self.model)
    for attributor in self.attributors:
        attributor.attach(self.model)

load_pipeline_config(path)

Load and validate a YAML pipeline config.

Source code in src/SafeLens/pipelines/runner.py
30
31
32
def load_pipeline_config(path: str | Path) -> PipelineConfig:
    """Load and validate a YAML pipeline config."""
    return validate_pipeline_config_file(path)

run_from_config(config_path, dataset=None)

Convenience function for one-shot execution.

Source code in src/SafeLens/pipelines/runner.py
226
227
228
229
230
231
def run_from_config(
    config_path: str | Path,
    dataset: Iterable[Mapping[str, Any]] | None = None,
) -> RunReport:
    """Convenience function for one-shot execution."""
    return PipelineRunner.from_yaml(config_path).run(dataset=dataset)