AI / A CONCEPT NOTE

DSPy

compiling declarative prompt pipelines

~70 sec read

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

  1. Define signature

    declares inputs and outputs: class Qa(dspy.Signature): question -> answer.

  2. Bootstrap optimizer

    dspy.teleprompt.BootstrapFewShot gathers training datasets.

  3. Compile optimizer

    runs evaluations, optimizing prompt instructions and choosing best few-shot examples.

  4. 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.

EXAMPLE 01 · REFERENCE

bootstrap DSPy context

python -c import dspy; lm = dspy.LM('openai/gpt-4o'); dspy.settings.configure(lm=lm)

Explore command anatomy in the CLI lab