@gaussia/sdk/prompt-optimizer subpath improves a system prompt against a dataset using GEPA (Generative Evolutionary Prompt Adaptation). Each iteration:
- runs the current prompt over the dataset (the executor),
- scores each answer (the evaluator),
- collects answers scoring below
failureThresholdas failing examples, - asks the model for improved candidate prompts driven by those failures,
- keeps the best candidate only if it strictly improves the mean score,
- repeats until nothing fails, nothing improves, or the iteration budget is spent.
LanguageModel interface — it has no idea which provider you use.
Optimize a prompt
run takes a Retriever class, the config passed to that retriever’s constructor, and the options. A small inline retriever that returns a fixed Dataset[] is often all you need.
Options
LanguageModel
required
Used for candidate generation, and for the default judge when no
evaluator is given.string
required
The starting system prompt to improve.
string
required
What a good answer must do. Also the default judge’s criteria.
Executor
How answers are produced. Omit to call the model directly; supply one to optimize a real pipeline.
Evaluator
How answers are scored. Omit for the built-in LLM judge; supply one to grade with a rubric or with code.
number
default:"5"
Maximum optimization rounds.
number
default:"3"
Candidate prompts generated per round.
number
default:"0.6"
Score (in
[0,1]) below which an answer counts as failing and drives candidate generation.number
default:"1"
Parallel evaluation chains. Results are gathered in input order, so the output is identical for any value — only faster.
1 matches the reference behavior exactly.number
default:"2"
Retries a transient malformed candidate response before throwing
OptimizerError.(event: ProgressEvent) => void
Called once per executed round with
{ iteration, bestScore, failing }.Logger
Optional logger for diagnostics.
The two seams
Two function contracts let you adapt the optimizer to your system without subclassing.Evaluator — how answers are scored
[0,1], but a custom evaluator owns its range. Supplying one fully replaces the default — the built-in LLM judge is never constructed.
- Default LLM judge
- LLM judge with a rubric
- Deterministic evaluator (code)
Omit
evaluator. The built-in judge scores against your objective.Executor — how answers are produced
Watch it run
onProgress fires once per executed round. Use it for live feedback.
If
onProgress fires zero or one time, GEPA converged early — the seed already passed, or no candidate improved. Raise failureThreshold and the iteration budget when you want more rounds to observe. Output is non-deterministic against a real model.The result
run returns a validated OptimizationResult:
string
The best prompt found (the seed if nothing improved).
number
Mean score of the seed prompt.
number
Best mean score reached.
number
Rounds actually executed (
0 if the seed passed immediately).number
Number of evaluation examples.
IterationResult[]
Per-round detail:
iteration, bestPrompt, bestScore, candidates (each { prompt, score }), and failingExamples (each { query, context, expected, actual, score }).Inspect the trajectory
Errors and resilience
- A run that needs the model to produce candidates may hit a malformed response;
candidateRetries(default 2) retries transient failures before throwingOptimizerError. Smaller models fail this more often — retry or use a stronger model. - The result schemas (
OptimizationResult,IterationResult,CandidateResult,FailingExample) come from@gaussia/sdk/schemasand are validated beforerunreturns.