<< All versions

Skill v1.0.0

currentAutomated scan100/100
hung-phan/ml-skills/evaluation-patterns
──Details
PublishedOctober 24, 2026 at 06:46 PM
Content Hashsha256:03309655a64530db...
Git SHAbc99221ed0f7
──Files
Files (1 file, 4.0 KB)
SKILL.md4.0 KBactive
SKILL.md · 116 lines · 4.0 KB

version: "1.0.0" name: dspy-evaluation-patterns description: Test case and evaluation harness design for DSPy optimization. Use when constructing dspy.Example datasets, designing LLM-as-judge metrics, or structuring train/val splits.


Evaluation Patterns

Why This Exists

Problem: Knowing whether your DSPy program improved after optimization requires evaluating on a held-out devset with the right metric — without this you're flying blind and may be overfit to the trainset or regressed on edge cases.

Key insight: dspy.Example separates what the module receives (inputs) from what the metric compares against (gold labels) using with_inputs(), making it the atomic unit for both training and evaluation.

Reach for this when: You need to construct a trainset or devset for DSPy, design an LLM-as-judge metric, or run a before/after comparison to validate an optimizer's effect.

Evaluation Approach Comparison

ApproachMetric typeCostWhen to use
Exact match / token F1bool or floatCheapFactoid QA, entity extraction
Rule-based compositefloatCheapFormat + content checks
LLM-as-judgefloatMediumOpen-ended generation, semantic quality
Multi-dimensional LLM judgePrediction(score, feedback)MediumGEPA optimization

dspy.Example Construction

python
example = dspy.Example(
query="Play something like Radiohead",
context="User wants music similar to Radiohead's style",
).with_inputs("query") # fields in with_inputs() = module input; rest = gold labels

Key rule: with_inputs() separates what the module sees (input) from what metrics compare against (gold).

Test Case Taxonomy

Single-turn

python
dspy.Example(query="Play Taylor Swift", context="Explicit artist request").with_inputs("query")

Multi-turn

python
dspy.Example(
query="play more like this",
history="Now playing: 'Blinding Lights' by The Weeknd",
context="Wants similar synth-pop continuation",
).with_inputs("query", "history")

Edge cases

python
# Misspelling
dspy.Example(query="play biyonsay", context="Beyoncé request (misspelled)").with_inputs("query")
# Negation
dspy.Example(query="anything but country", context="Exclude country genre").with_inputs("query")
# Vague
dspy.Example(query="play something good", context="No preference, use history").with_inputs("query")

LLM-as-Judge Pattern

python
class JudgeSignature(dspy.Signature):
gold_context: str = dspy.InputField()
predicted_context: str = dspy.InputField()
reasoning: str = dspy.OutputField()
score: int = dspy.OutputField(desc="1-10")
def metric(example, pred, trace=None):
result = dspy.ChainOfThought(JudgeSignature)(
gold_context=example.context, predicted_context=pred.context
)
score = float(result.score) / 10.0
if trace is not None:
return score >= 0.7
return score

Multi-Dimensional Scoring

python
def metric(example, pred, trace=None):
score = (
0.4 * intent_accuracy(example, pred) +
0.3 * completeness(example, pred) +
0.2 * specificity(pred) +
0.1 * conciseness(pred)
) / 10.0
return score >= 0.6 if trace else score

Evaluate Harness

python
evaluator = dspy.Evaluate(devset=valset, metric=metric, num_threads=8, failure_score=0.0)
result = evaluator(program) # returns dspy.EvaluationResult
score = result.score # float percentage, e.g. 67.30; result.results is list of (example, prediction, score) tuples

Gotchas

  • Minimum 6-8 examples for MIPROv2 — fewer and bootstrapping fails
  • 20 diverse cases > 50 similar cases
  • Gold context should be concise — judges compare semantics not wording
  • Use a stronger model as judge than the target model
  • with_inputs() is required — without it DSPy doesn't know input vs gold

References

All versions