gepa-guard 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gepa_guard-0.1.0/LICENSE +21 -0
- gepa_guard-0.1.0/PKG-INFO +311 -0
- gepa_guard-0.1.0/README.md +281 -0
- gepa_guard-0.1.0/pyproject.toml +102 -0
- gepa_guard-0.1.0/pyproject.toml.orig +71 -0
- gepa_guard-0.1.0/src/gepa_guard/__init__.py +23 -0
- gepa_guard-0.1.0/src/gepa_guard/_fingerprint.py +26 -0
- gepa_guard-0.1.0/src/gepa_guard/_scores.py +104 -0
- gepa_guard-0.1.0/src/gepa_guard/_subsample.py +186 -0
- gepa_guard-0.1.0/src/gepa_guard/acceptance.py +312 -0
- gepa_guard-0.1.0/src/gepa_guard/exceptions.py +55 -0
- gepa_guard-0.1.0/src/gepa_guard/floor.py +479 -0
- gepa_guard-0.1.0/src/gepa_guard/py.typed +0 -0
- gepa_guard-0.1.0/src/gepa_guard/records.py +121 -0
- gepa_guard-0.1.0/tests/conftest.py +341 -0
- gepa_guard-0.1.0/tests/integration/conftest.py +243 -0
- gepa_guard-0.1.0/tests/integration/test_gepa_optimize.py +247 -0
- gepa_guard-0.1.0/tests/test_acceptance.py +516 -0
- gepa_guard-0.1.0/tests/test_conformance.py +148 -0
- gepa_guard-0.1.0/tests/test_exceptions.py +35 -0
- gepa_guard-0.1.0/tests/test_fingerprint.py +27 -0
- gepa_guard-0.1.0/tests/test_floor.py +577 -0
- gepa_guard-0.1.0/tests/test_public_api.py +47 -0
- gepa_guard-0.1.0/tests/test_records.py +76 -0
- gepa_guard-0.1.0/tests/test_scores.py +82 -0
- gepa_guard-0.1.0/tests/test_subsample.py +220 -0
gepa_guard-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 League Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gepa-guard
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Non-regression acceptance for GEPA reflective prompt optimization
|
|
5
|
+
Keywords: gepa,prompt optimization,llm,safety,non-regression
|
|
6
|
+
Author: Mia Ojeda, League Inc.
|
|
7
|
+
Author-email: Mia Ojeda <eojeda@league.com>, League Inc. <open-source-devs@league.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Dist: gepa>=0.1.4
|
|
23
|
+
Maintainer: Mia Ojeda, League Inc.
|
|
24
|
+
Maintainer-email: Mia Ojeda <eojeda@league.com>, League Inc. <open-source-devs@league.com>
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Project-URL: Source, https://github.com/LeagueLabs/gepa-guard
|
|
27
|
+
Project-URL: Issues, https://github.com/LeagueLabs/gepa-guard/issues
|
|
28
|
+
Project-URL: Changelog, https://github.com/LeagueLabs/gepa-guard/blob/main/CHANGELOG.md
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# gepa-guard
|
|
32
|
+
|
|
33
|
+
[](https://github.com/LeagueLabs/gepa-guard/actions/workflows/ci.yml)
|
|
34
|
+
[](https://github.com/astral-sh/ruff)
|
|
35
|
+
[](https://github.com/astral-sh/ty)
|
|
36
|
+
[](https://github.com/LeagueLabs/gepa-guard/blob/main/LICENSE)
|
|
37
|
+
|
|
38
|
+
[](https://pypi.org/project/gepa-guard/)
|
|
39
|
+
[](https://pypi.org/project/gepa-guard/)
|
|
40
|
+
|
|
41
|
+
gepa-guard is a non-regression acceptance criterion for [GEPA] reflective
|
|
42
|
+
prompt optimization. It rejects a candidate prompt that fails a protected
|
|
43
|
+
check that its parent or the seed passed.
|
|
44
|
+
|
|
45
|
+
GEPA improves a prompt against one scalar score. A candidate prompt with a
|
|
46
|
+
higher score can lose a rare behavior that must not break, such as the
|
|
47
|
+
escalation of a crisis message. GEPA accepts that candidate because its
|
|
48
|
+
minibatch score went up. gepa-guard keeps the GEPA objective and wraps its
|
|
49
|
+
acceptance criterion with protected checks.
|
|
50
|
+
|
|
51
|
+
## Why it matters
|
|
52
|
+
|
|
53
|
+
In health AI, one wrong answer on a rare case can outweigh a small gain on
|
|
54
|
+
average. A triage prompt that stops escalating a crisis message is not an
|
|
55
|
+
improvement, even when its score goes up. A prompt optimizer must not trade
|
|
56
|
+
that case for a higher average.
|
|
57
|
+
|
|
58
|
+
gepa-guard was developed at League Labs and funded by League Inc. It comes
|
|
59
|
+
from the component that gates prompt revisions for an internal health agent at
|
|
60
|
+
League. gepa-guard turns a silent regression into a rejected proposal with a
|
|
61
|
+
named cause and a record for audit, before a revised prompt reaches
|
|
62
|
+
deployment.
|
|
63
|
+
|
|
64
|
+
## Features
|
|
65
|
+
|
|
66
|
+
With gepa-guard you can:
|
|
67
|
+
|
|
68
|
+
- Reject a candidate that loses a protected pass of its parent in the minibatch.
|
|
69
|
+
- Check candidates against a fixed set of protected rows (the floor).
|
|
70
|
+
- Reject a proposal whose protected scores cannot be read, instead of guessing.
|
|
71
|
+
- Read the failed check and row in the gepa log for each protected rejection.
|
|
72
|
+
- Count every decision in a run record that matches the guarded proposal
|
|
73
|
+
outcomes in the paper.
|
|
74
|
+
- Keep your base criterion, for example `StrictImprovementAcceptance`.
|
|
75
|
+
|
|
76
|
+
## Installation
|
|
77
|
+
|
|
78
|
+
gepa-guard is available via `pip`:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install gepa-guard
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
gepa-guard supports Python 3.11 to 3.15 and gepa 0.1.4 or later.
|
|
85
|
+
|
|
86
|
+
## Basic usage
|
|
87
|
+
|
|
88
|
+
Your gepa adapter must return `objective_scores` in its `EvaluationBatch`.
|
|
89
|
+
This is a list with one mapping per row. Give each protected check a score of
|
|
90
|
+
`1.0` (pass) or `0.0` (fail) on the rows that test it. Leave a check out of a
|
|
91
|
+
row when the row does not test it, or when the row has no score for it, for
|
|
92
|
+
example after an error. The guard also reads `None` as no score, but gepa
|
|
93
|
+
0.1.4 stops the run when a `valset` score is `None`. Leave the check out
|
|
94
|
+
instead.
|
|
95
|
+
|
|
96
|
+
The following example protects an `emergency` check in the minibatch.
|
|
97
|
+
`seed`, `trainset`, `valset`, `adapter` and `reflection_lm` are your usual
|
|
98
|
+
gepa inputs:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
import gepa
|
|
102
|
+
from gepa.strategies.acceptance import StrictImprovementAcceptance
|
|
103
|
+
from gepa_guard import GuardedAcceptance
|
|
104
|
+
|
|
105
|
+
guard = GuardedAcceptance(StrictImprovementAcceptance(), protected=["emergency"])
|
|
106
|
+
|
|
107
|
+
result = gepa.optimize(
|
|
108
|
+
seed_candidate=seed,
|
|
109
|
+
trainset=trainset,
|
|
110
|
+
valset=valset,
|
|
111
|
+
adapter=adapter,
|
|
112
|
+
reflection_lm=reflection_lm,
|
|
113
|
+
acceptance_criterion=guard,
|
|
114
|
+
max_metric_calls=500,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
print(guard.stats.as_dict())
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
gepa logs the reason for each rejection, for example:
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
Iteration 3: rejected: protected regression in the minibatch on emergency at row 1 (data id 11)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Add a protected floor
|
|
127
|
+
|
|
128
|
+
The minibatch holds only a few rows. A floor checks each candidate that
|
|
129
|
+
passes the minibatch check and the base criterion against a fixed set of
|
|
130
|
+
protected rows. Measure the floor with the seed first. The seed defines which
|
|
131
|
+
checks each row must keep.
|
|
132
|
+
|
|
133
|
+
`protected_rows` is a list of rows in the form your adapter takes. Do not use
|
|
134
|
+
rows from your test set.
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from gepa.strategies.acceptance import StrictImprovementAcceptance
|
|
138
|
+
from gepa_guard import FloorConfig, GuardedAcceptance, ProtectedFloor
|
|
139
|
+
|
|
140
|
+
floor = ProtectedFloor.from_adapter(
|
|
141
|
+
adapter, protected_rows, protected=["emergency"], config=FloorConfig(batch_size=8)
|
|
142
|
+
)
|
|
143
|
+
floor.measure(seed)
|
|
144
|
+
|
|
145
|
+
guard = GuardedAcceptance(
|
|
146
|
+
StrictImprovementAcceptance(), protected=["emergency"], floor=floor
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The floor scores its rows in batches of `batch_size` and stops after the
|
|
151
|
+
first batch with a regression. It reports every regressed row of that batch.
|
|
152
|
+
With a `batch_size` of at least the number of floor rows, the floor scores
|
|
153
|
+
every row at once and reports every regressed row. Floor `ids` change this,
|
|
154
|
+
as described below.
|
|
155
|
+
|
|
156
|
+
To score the floor without an adapter, use
|
|
157
|
+
`ProtectedFloor(evaluate, rows, protected=[...])`. `evaluate` takes a sequence
|
|
158
|
+
of rows and a candidate, and returns one objective mapping per row.
|
|
159
|
+
|
|
160
|
+
If you already have the seed scores of the floor rows, use
|
|
161
|
+
`floor.measure_scores(scores)` instead of `floor.measure(seed)`. It takes one
|
|
162
|
+
objective mapping per row and does not score the seed again. The floor then
|
|
163
|
+
protects the same passes that your own seed evaluation shows.
|
|
164
|
+
|
|
165
|
+
When some floor rows are also in your `trainset`, give the floor the gepa data
|
|
166
|
+
id of each row with `ids`. For a list `trainset`, the data id is the position
|
|
167
|
+
of the row in the `trainset`. Do not use positions in the `valset`. A wrong id
|
|
168
|
+
makes the floor use the scores of another row. Use `None` for a row that is
|
|
169
|
+
not in the `trainset`.
|
|
170
|
+
The guard then gives the floor the minibatch scores of the candidate. The floor
|
|
171
|
+
uses them for its rows in the minibatch and scores only the other rows. It
|
|
172
|
+
also scores a row when the minibatch has no score for one of its protected
|
|
173
|
+
checks. The floor checks the rows with minibatch scores before it scores any
|
|
174
|
+
row. When one of them regresses, the floor reports only those rows.
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
floor = ProtectedFloor.from_adapter(
|
|
178
|
+
adapter, protected_rows, protected=["emergency"], ids=[0, 4, None]
|
|
179
|
+
)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## How it works
|
|
183
|
+
|
|
184
|
+
For each proposal, the guard runs these checks in order. The first check that
|
|
185
|
+
fails rejects the proposal.
|
|
186
|
+
|
|
187
|
+
1. **Read**: the minibatch objective scores of the parent and the candidate
|
|
188
|
+
must exist and align row by row. Each protected score must be 0, 1 or
|
|
189
|
+
`None`. If not, the proposal is *unverifiable* and is rejected.
|
|
190
|
+
2. **Minibatch**: the candidate must keep every protected pass of its parent.
|
|
191
|
+
3. **Base**: the base criterion, for example `StrictImprovementAcceptance`,
|
|
192
|
+
must accept.
|
|
193
|
+
4. **Floor**: the candidate must keep every protected pass of the seed on the
|
|
194
|
+
floor rows, when a floor is given.
|
|
195
|
+
|
|
196
|
+
The floor protects only what the seed passed. A check that the seed fails on
|
|
197
|
+
every floor row has no floor case. The minibatch check still protects it. A
|
|
198
|
+
pass that a later candidate gains is protected only in the minibatch.
|
|
199
|
+
|
|
200
|
+
A missing score never counts as a pass. When the candidate has no score for a
|
|
201
|
+
protected pass of its parent, or of the seed on the floor, and has no
|
|
202
|
+
regression, the proposal is *unverifiable* and is rejected. A check that is
|
|
203
|
+
left out or is `None` has no score.
|
|
204
|
+
|
|
205
|
+
After a minibatch rejection, the guard still asks the base criterion. The
|
|
206
|
+
answer does not change the decision. It only counts the rejections that
|
|
207
|
+
override GEPA.
|
|
208
|
+
|
|
209
|
+
## Requirements
|
|
210
|
+
|
|
211
|
+
- **Binary protected scores.** A protected score must be 0, 1 or `None`. Only
|
|
212
|
+
1 is a pass. Any other value makes a minibatch proposal unverifiable. On the
|
|
213
|
+
floor, it raises `ValueError`.
|
|
214
|
+
- **Deterministic scores.** A check that passes and fails at random on the
|
|
215
|
+
same prompt causes false rejections. Make the task model as deterministic
|
|
216
|
+
as you can, for example with temperature 0.
|
|
217
|
+
- **A score for each floor check.** At least one floor row must score each
|
|
218
|
+
protected check. `ProtectedFloor.measure` and `measure_scores` raise
|
|
219
|
+
`ValueError` for a check that no row scores, for example a misspelled
|
|
220
|
+
name. A check that the seed fails on every row is allowed.
|
|
221
|
+
- **A `valset` score for each protected check.** At least one `valset` row
|
|
222
|
+
must score each protected check. The guard raises `ValueError` at its first
|
|
223
|
+
decision when the seed has no `valset` score for a protected name, for
|
|
224
|
+
example a misspelled name. A check that the seed fails on every row is
|
|
225
|
+
allowed. A custom `val_evaluation_policy` can score the seed on only part
|
|
226
|
+
of the `valset`. That part must still score each protected check.
|
|
227
|
+
- **No merges.** gepa accepts merged candidates without the acceptance
|
|
228
|
+
criterion. Keep `use_merge=False`, the default of `gepa.optimize`. With
|
|
229
|
+
`optimize_anything`, leave `GEPAConfig.merge` unset. If the gepa state holds
|
|
230
|
+
a merged candidate, the guard raises `UnguardedMergeError` at its next
|
|
231
|
+
decision.
|
|
232
|
+
- **One guard per run.** The guard keeps state for one run. Calls from several
|
|
233
|
+
threads run one at a time.
|
|
234
|
+
|
|
235
|
+
## Reading the run record
|
|
236
|
+
|
|
237
|
+
`guard.stats` is a `VetoStats`. Its counts include every column of the
|
|
238
|
+
guarded proposal outcomes table in the paper.
|
|
239
|
+
|
|
240
|
+
| Field | Meaning |
|
|
241
|
+
|---|---|
|
|
242
|
+
| `proposals`, `accepted` | Proposals decided and accepted |
|
|
243
|
+
| `vetoed_unverifiable` | Rejected because the protected scores could not be read, or had no score for a protected pass |
|
|
244
|
+
| `vetoed_floor_unverifiable` | The part of `vetoed_unverifiable` where the floor had no score for a protected pass of the seed |
|
|
245
|
+
| `vetoed_subsample` | Rejected for a protected regression in the minibatch |
|
|
246
|
+
| `vetoed_by_base` | Rejected by the base criterion |
|
|
247
|
+
| `vetoed_floor` | Rejected for a protected regression in the floor |
|
|
248
|
+
| `vetoed_by_guard` | `vetoed_subsample` plus `vetoed_floor` |
|
|
249
|
+
| `vetoed_subsample_base_accepted` | Minibatch rejections of proposals that the base criterion accepts. These are the counts in parentheses in the paper |
|
|
250
|
+
| `overrode_base` | `vetoed_subsample_base_accepted` plus `vetoed_floor`. This is the "Overrode GEPA" column |
|
|
251
|
+
| `checked` | Protected passes of the parent compared in the minibatch, per check |
|
|
252
|
+
| `regressed` | Protected regressions found in the minibatch or the floor, per check |
|
|
253
|
+
|
|
254
|
+
`as_dict()` returns every count as a plain dictionary for logs and reports.
|
|
255
|
+
|
|
256
|
+
## Compatibility
|
|
257
|
+
|
|
258
|
+
- gepa 0.1.4 or later. CI tests the oldest supported and the latest release
|
|
259
|
+
with `gepa.optimize`.
|
|
260
|
+
- `optimize_anything` also takes the guard, as
|
|
261
|
+
`EngineConfig(acceptance_criterion=guard)` from `gepa.optimize_anything`.
|
|
262
|
+
Return the protected scores in `side_info["scores"]`.
|
|
263
|
+
- Any base criterion with a `should_accept(proposal, state)` method. gepa's
|
|
264
|
+
own criteria match the `BaseCriterion` protocol.
|
|
265
|
+
|
|
266
|
+
## Citation
|
|
267
|
+
|
|
268
|
+
If you use gepa-guard in your research, please cite the paper:
|
|
269
|
+
|
|
270
|
+
```bibtex
|
|
271
|
+
@inproceedings{barreto-ojeda2026demo,
|
|
272
|
+
title = {Demo: Blocking Silent Safety Regressions in Prompt Optimization for Health {AI}},
|
|
273
|
+
author = {Estefania Barreto-Ojeda and Aiden Arnold},
|
|
274
|
+
booktitle = {The Third Workshop on GenAI for Health: Agentic Systems, Clinical Trust, and Future Potential - Demonstration Paper Track},
|
|
275
|
+
year = {2026},
|
|
276
|
+
url = {https://openreview.net/forum?id=3iBtYkwQSc},
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Contributing
|
|
281
|
+
|
|
282
|
+
Contributions are very welcome!
|
|
283
|
+
|
|
284
|
+
Please read the [Contributing] guide before you open a pull request. It
|
|
285
|
+
describes the development setup with [uv], the checks, the code style and the
|
|
286
|
+
AI assistance policy.
|
|
287
|
+
|
|
288
|
+
To set up a development environment, fork the repository, clone your fork and
|
|
289
|
+
run the tests:
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
git clone https://github.com/<your-username>/gepa-guard.git
|
|
293
|
+
cd gepa-guard
|
|
294
|
+
uv sync
|
|
295
|
+
uv run pytest
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
To report a vulnerability, please see the [Security] policy.
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
Source code included in this project is available in the GitHub repository
|
|
303
|
+
https://github.com/LeagueLabs/gepa-guard under the MIT License (see
|
|
304
|
+
[LICENSE]). The project is linked to a [Code of Conduct].
|
|
305
|
+
|
|
306
|
+
[GEPA]: https://github.com/gepa-ai/gepa
|
|
307
|
+
[uv]: https://docs.astral.sh/uv/
|
|
308
|
+
[Contributing]: https://github.com/LeagueLabs/gepa-guard/blob/main/CONTRIBUTING.md
|
|
309
|
+
[Security]: https://github.com/LeagueLabs/gepa-guard/blob/main/SECURITY.md
|
|
310
|
+
[LICENSE]: https://github.com/LeagueLabs/gepa-guard/blob/main/LICENSE
|
|
311
|
+
[Code of Conduct]: https://github.com/LeagueLabs/gepa-guard/blob/main/CODE_OF_CONDUCT.md
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# gepa-guard
|
|
2
|
+
|
|
3
|
+
[](https://github.com/LeagueLabs/gepa-guard/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/astral-sh/ruff)
|
|
5
|
+
[](https://github.com/astral-sh/ty)
|
|
6
|
+
[](https://github.com/LeagueLabs/gepa-guard/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
[](https://pypi.org/project/gepa-guard/)
|
|
9
|
+
[](https://pypi.org/project/gepa-guard/)
|
|
10
|
+
|
|
11
|
+
gepa-guard is a non-regression acceptance criterion for [GEPA] reflective
|
|
12
|
+
prompt optimization. It rejects a candidate prompt that fails a protected
|
|
13
|
+
check that its parent or the seed passed.
|
|
14
|
+
|
|
15
|
+
GEPA improves a prompt against one scalar score. A candidate prompt with a
|
|
16
|
+
higher score can lose a rare behavior that must not break, such as the
|
|
17
|
+
escalation of a crisis message. GEPA accepts that candidate because its
|
|
18
|
+
minibatch score went up. gepa-guard keeps the GEPA objective and wraps its
|
|
19
|
+
acceptance criterion with protected checks.
|
|
20
|
+
|
|
21
|
+
## Why it matters
|
|
22
|
+
|
|
23
|
+
In health AI, one wrong answer on a rare case can outweigh a small gain on
|
|
24
|
+
average. A triage prompt that stops escalating a crisis message is not an
|
|
25
|
+
improvement, even when its score goes up. A prompt optimizer must not trade
|
|
26
|
+
that case for a higher average.
|
|
27
|
+
|
|
28
|
+
gepa-guard was developed at League Labs and funded by League Inc. It comes
|
|
29
|
+
from the component that gates prompt revisions for an internal health agent at
|
|
30
|
+
League. gepa-guard turns a silent regression into a rejected proposal with a
|
|
31
|
+
named cause and a record for audit, before a revised prompt reaches
|
|
32
|
+
deployment.
|
|
33
|
+
|
|
34
|
+
## Features
|
|
35
|
+
|
|
36
|
+
With gepa-guard you can:
|
|
37
|
+
|
|
38
|
+
- Reject a candidate that loses a protected pass of its parent in the minibatch.
|
|
39
|
+
- Check candidates against a fixed set of protected rows (the floor).
|
|
40
|
+
- Reject a proposal whose protected scores cannot be read, instead of guessing.
|
|
41
|
+
- Read the failed check and row in the gepa log for each protected rejection.
|
|
42
|
+
- Count every decision in a run record that matches the guarded proposal
|
|
43
|
+
outcomes in the paper.
|
|
44
|
+
- Keep your base criterion, for example `StrictImprovementAcceptance`.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
gepa-guard is available via `pip`:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install gepa-guard
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
gepa-guard supports Python 3.11 to 3.15 and gepa 0.1.4 or later.
|
|
55
|
+
|
|
56
|
+
## Basic usage
|
|
57
|
+
|
|
58
|
+
Your gepa adapter must return `objective_scores` in its `EvaluationBatch`.
|
|
59
|
+
This is a list with one mapping per row. Give each protected check a score of
|
|
60
|
+
`1.0` (pass) or `0.0` (fail) on the rows that test it. Leave a check out of a
|
|
61
|
+
row when the row does not test it, or when the row has no score for it, for
|
|
62
|
+
example after an error. The guard also reads `None` as no score, but gepa
|
|
63
|
+
0.1.4 stops the run when a `valset` score is `None`. Leave the check out
|
|
64
|
+
instead.
|
|
65
|
+
|
|
66
|
+
The following example protects an `emergency` check in the minibatch.
|
|
67
|
+
`seed`, `trainset`, `valset`, `adapter` and `reflection_lm` are your usual
|
|
68
|
+
gepa inputs:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
import gepa
|
|
72
|
+
from gepa.strategies.acceptance import StrictImprovementAcceptance
|
|
73
|
+
from gepa_guard import GuardedAcceptance
|
|
74
|
+
|
|
75
|
+
guard = GuardedAcceptance(StrictImprovementAcceptance(), protected=["emergency"])
|
|
76
|
+
|
|
77
|
+
result = gepa.optimize(
|
|
78
|
+
seed_candidate=seed,
|
|
79
|
+
trainset=trainset,
|
|
80
|
+
valset=valset,
|
|
81
|
+
adapter=adapter,
|
|
82
|
+
reflection_lm=reflection_lm,
|
|
83
|
+
acceptance_criterion=guard,
|
|
84
|
+
max_metric_calls=500,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
print(guard.stats.as_dict())
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
gepa logs the reason for each rejection, for example:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
Iteration 3: rejected: protected regression in the minibatch on emergency at row 1 (data id 11)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Add a protected floor
|
|
97
|
+
|
|
98
|
+
The minibatch holds only a few rows. A floor checks each candidate that
|
|
99
|
+
passes the minibatch check and the base criterion against a fixed set of
|
|
100
|
+
protected rows. Measure the floor with the seed first. The seed defines which
|
|
101
|
+
checks each row must keep.
|
|
102
|
+
|
|
103
|
+
`protected_rows` is a list of rows in the form your adapter takes. Do not use
|
|
104
|
+
rows from your test set.
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from gepa.strategies.acceptance import StrictImprovementAcceptance
|
|
108
|
+
from gepa_guard import FloorConfig, GuardedAcceptance, ProtectedFloor
|
|
109
|
+
|
|
110
|
+
floor = ProtectedFloor.from_adapter(
|
|
111
|
+
adapter, protected_rows, protected=["emergency"], config=FloorConfig(batch_size=8)
|
|
112
|
+
)
|
|
113
|
+
floor.measure(seed)
|
|
114
|
+
|
|
115
|
+
guard = GuardedAcceptance(
|
|
116
|
+
StrictImprovementAcceptance(), protected=["emergency"], floor=floor
|
|
117
|
+
)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The floor scores its rows in batches of `batch_size` and stops after the
|
|
121
|
+
first batch with a regression. It reports every regressed row of that batch.
|
|
122
|
+
With a `batch_size` of at least the number of floor rows, the floor scores
|
|
123
|
+
every row at once and reports every regressed row. Floor `ids` change this,
|
|
124
|
+
as described below.
|
|
125
|
+
|
|
126
|
+
To score the floor without an adapter, use
|
|
127
|
+
`ProtectedFloor(evaluate, rows, protected=[...])`. `evaluate` takes a sequence
|
|
128
|
+
of rows and a candidate, and returns one objective mapping per row.
|
|
129
|
+
|
|
130
|
+
If you already have the seed scores of the floor rows, use
|
|
131
|
+
`floor.measure_scores(scores)` instead of `floor.measure(seed)`. It takes one
|
|
132
|
+
objective mapping per row and does not score the seed again. The floor then
|
|
133
|
+
protects the same passes that your own seed evaluation shows.
|
|
134
|
+
|
|
135
|
+
When some floor rows are also in your `trainset`, give the floor the gepa data
|
|
136
|
+
id of each row with `ids`. For a list `trainset`, the data id is the position
|
|
137
|
+
of the row in the `trainset`. Do not use positions in the `valset`. A wrong id
|
|
138
|
+
makes the floor use the scores of another row. Use `None` for a row that is
|
|
139
|
+
not in the `trainset`.
|
|
140
|
+
The guard then gives the floor the minibatch scores of the candidate. The floor
|
|
141
|
+
uses them for its rows in the minibatch and scores only the other rows. It
|
|
142
|
+
also scores a row when the minibatch has no score for one of its protected
|
|
143
|
+
checks. The floor checks the rows with minibatch scores before it scores any
|
|
144
|
+
row. When one of them regresses, the floor reports only those rows.
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
floor = ProtectedFloor.from_adapter(
|
|
148
|
+
adapter, protected_rows, protected=["emergency"], ids=[0, 4, None]
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## How it works
|
|
153
|
+
|
|
154
|
+
For each proposal, the guard runs these checks in order. The first check that
|
|
155
|
+
fails rejects the proposal.
|
|
156
|
+
|
|
157
|
+
1. **Read**: the minibatch objective scores of the parent and the candidate
|
|
158
|
+
must exist and align row by row. Each protected score must be 0, 1 or
|
|
159
|
+
`None`. If not, the proposal is *unverifiable* and is rejected.
|
|
160
|
+
2. **Minibatch**: the candidate must keep every protected pass of its parent.
|
|
161
|
+
3. **Base**: the base criterion, for example `StrictImprovementAcceptance`,
|
|
162
|
+
must accept.
|
|
163
|
+
4. **Floor**: the candidate must keep every protected pass of the seed on the
|
|
164
|
+
floor rows, when a floor is given.
|
|
165
|
+
|
|
166
|
+
The floor protects only what the seed passed. A check that the seed fails on
|
|
167
|
+
every floor row has no floor case. The minibatch check still protects it. A
|
|
168
|
+
pass that a later candidate gains is protected only in the minibatch.
|
|
169
|
+
|
|
170
|
+
A missing score never counts as a pass. When the candidate has no score for a
|
|
171
|
+
protected pass of its parent, or of the seed on the floor, and has no
|
|
172
|
+
regression, the proposal is *unverifiable* and is rejected. A check that is
|
|
173
|
+
left out or is `None` has no score.
|
|
174
|
+
|
|
175
|
+
After a minibatch rejection, the guard still asks the base criterion. The
|
|
176
|
+
answer does not change the decision. It only counts the rejections that
|
|
177
|
+
override GEPA.
|
|
178
|
+
|
|
179
|
+
## Requirements
|
|
180
|
+
|
|
181
|
+
- **Binary protected scores.** A protected score must be 0, 1 or `None`. Only
|
|
182
|
+
1 is a pass. Any other value makes a minibatch proposal unverifiable. On the
|
|
183
|
+
floor, it raises `ValueError`.
|
|
184
|
+
- **Deterministic scores.** A check that passes and fails at random on the
|
|
185
|
+
same prompt causes false rejections. Make the task model as deterministic
|
|
186
|
+
as you can, for example with temperature 0.
|
|
187
|
+
- **A score for each floor check.** At least one floor row must score each
|
|
188
|
+
protected check. `ProtectedFloor.measure` and `measure_scores` raise
|
|
189
|
+
`ValueError` for a check that no row scores, for example a misspelled
|
|
190
|
+
name. A check that the seed fails on every row is allowed.
|
|
191
|
+
- **A `valset` score for each protected check.** At least one `valset` row
|
|
192
|
+
must score each protected check. The guard raises `ValueError` at its first
|
|
193
|
+
decision when the seed has no `valset` score for a protected name, for
|
|
194
|
+
example a misspelled name. A check that the seed fails on every row is
|
|
195
|
+
allowed. A custom `val_evaluation_policy` can score the seed on only part
|
|
196
|
+
of the `valset`. That part must still score each protected check.
|
|
197
|
+
- **No merges.** gepa accepts merged candidates without the acceptance
|
|
198
|
+
criterion. Keep `use_merge=False`, the default of `gepa.optimize`. With
|
|
199
|
+
`optimize_anything`, leave `GEPAConfig.merge` unset. If the gepa state holds
|
|
200
|
+
a merged candidate, the guard raises `UnguardedMergeError` at its next
|
|
201
|
+
decision.
|
|
202
|
+
- **One guard per run.** The guard keeps state for one run. Calls from several
|
|
203
|
+
threads run one at a time.
|
|
204
|
+
|
|
205
|
+
## Reading the run record
|
|
206
|
+
|
|
207
|
+
`guard.stats` is a `VetoStats`. Its counts include every column of the
|
|
208
|
+
guarded proposal outcomes table in the paper.
|
|
209
|
+
|
|
210
|
+
| Field | Meaning |
|
|
211
|
+
|---|---|
|
|
212
|
+
| `proposals`, `accepted` | Proposals decided and accepted |
|
|
213
|
+
| `vetoed_unverifiable` | Rejected because the protected scores could not be read, or had no score for a protected pass |
|
|
214
|
+
| `vetoed_floor_unverifiable` | The part of `vetoed_unverifiable` where the floor had no score for a protected pass of the seed |
|
|
215
|
+
| `vetoed_subsample` | Rejected for a protected regression in the minibatch |
|
|
216
|
+
| `vetoed_by_base` | Rejected by the base criterion |
|
|
217
|
+
| `vetoed_floor` | Rejected for a protected regression in the floor |
|
|
218
|
+
| `vetoed_by_guard` | `vetoed_subsample` plus `vetoed_floor` |
|
|
219
|
+
| `vetoed_subsample_base_accepted` | Minibatch rejections of proposals that the base criterion accepts. These are the counts in parentheses in the paper |
|
|
220
|
+
| `overrode_base` | `vetoed_subsample_base_accepted` plus `vetoed_floor`. This is the "Overrode GEPA" column |
|
|
221
|
+
| `checked` | Protected passes of the parent compared in the minibatch, per check |
|
|
222
|
+
| `regressed` | Protected regressions found in the minibatch or the floor, per check |
|
|
223
|
+
|
|
224
|
+
`as_dict()` returns every count as a plain dictionary for logs and reports.
|
|
225
|
+
|
|
226
|
+
## Compatibility
|
|
227
|
+
|
|
228
|
+
- gepa 0.1.4 or later. CI tests the oldest supported and the latest release
|
|
229
|
+
with `gepa.optimize`.
|
|
230
|
+
- `optimize_anything` also takes the guard, as
|
|
231
|
+
`EngineConfig(acceptance_criterion=guard)` from `gepa.optimize_anything`.
|
|
232
|
+
Return the protected scores in `side_info["scores"]`.
|
|
233
|
+
- Any base criterion with a `should_accept(proposal, state)` method. gepa's
|
|
234
|
+
own criteria match the `BaseCriterion` protocol.
|
|
235
|
+
|
|
236
|
+
## Citation
|
|
237
|
+
|
|
238
|
+
If you use gepa-guard in your research, please cite the paper:
|
|
239
|
+
|
|
240
|
+
```bibtex
|
|
241
|
+
@inproceedings{barreto-ojeda2026demo,
|
|
242
|
+
title = {Demo: Blocking Silent Safety Regressions in Prompt Optimization for Health {AI}},
|
|
243
|
+
author = {Estefania Barreto-Ojeda and Aiden Arnold},
|
|
244
|
+
booktitle = {The Third Workshop on GenAI for Health: Agentic Systems, Clinical Trust, and Future Potential - Demonstration Paper Track},
|
|
245
|
+
year = {2026},
|
|
246
|
+
url = {https://openreview.net/forum?id=3iBtYkwQSc},
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Contributing
|
|
251
|
+
|
|
252
|
+
Contributions are very welcome!
|
|
253
|
+
|
|
254
|
+
Please read the [Contributing] guide before you open a pull request. It
|
|
255
|
+
describes the development setup with [uv], the checks, the code style and the
|
|
256
|
+
AI assistance policy.
|
|
257
|
+
|
|
258
|
+
To set up a development environment, fork the repository, clone your fork and
|
|
259
|
+
run the tests:
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
git clone https://github.com/<your-username>/gepa-guard.git
|
|
263
|
+
cd gepa-guard
|
|
264
|
+
uv sync
|
|
265
|
+
uv run pytest
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
To report a vulnerability, please see the [Security] policy.
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
Source code included in this project is available in the GitHub repository
|
|
273
|
+
https://github.com/LeagueLabs/gepa-guard under the MIT License (see
|
|
274
|
+
[LICENSE]). The project is linked to a [Code of Conduct].
|
|
275
|
+
|
|
276
|
+
[GEPA]: https://github.com/gepa-ai/gepa
|
|
277
|
+
[uv]: https://docs.astral.sh/uv/
|
|
278
|
+
[Contributing]: https://github.com/LeagueLabs/gepa-guard/blob/main/CONTRIBUTING.md
|
|
279
|
+
[Security]: https://github.com/LeagueLabs/gepa-guard/blob/main/SECURITY.md
|
|
280
|
+
[LICENSE]: https://github.com/LeagueLabs/gepa-guard/blob/main/LICENSE
|
|
281
|
+
[Code of Conduct]: https://github.com/LeagueLabs/gepa-guard/blob/main/CODE_OF_CONDUCT.md
|