AI / A CONCEPT NOTE
DSPy
compiling declarative prompt pipelines
Overview · mechanism
pitfall · examples
01 / THE SHORT VERSION
The idea in a few sentences.
A framework by Stanford that replaces fragile manual prompt engineering by treating prompts as code programs, dynamically optimizing instructions and examples based on evaluations.
02 / FOLLOW THE MECHANISM
How prompt compilation flows
Define signature
declares inputs and outputs: class Qa(dspy.Signature): question -> answer.
Bootstrap optimizer
dspy.teleprompt.BootstrapFewShot gathers training datasets.
Compile optimizer
runs evaluations, optimizing prompt instructions and choosing best few-shot examples.
Save program
saves optimized module weights configuration to disk for runtime execution.
04 / COMMAND NOTES
Read the command, then the result.
Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.
bootstrap DSPy context
python -c import dspy; lm = dspy.LM('openai/gpt-4o'); dspy.settings.configure(lm=lm)05 / CHECK YOURSELF
Could you explain DSPy to a teammate?
Try it out loud in two sentences: what it is, and the one detail that changes the picture. If you stall, the gap is the part to reread.