FDEInterviews logo
ICLR 2024

DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines

The 2023 Stanford paper that treats an LLM pipeline as a program: declare what each call does, give a metric and a few examples, and let a compiler write the prompts.

Breakdown by FDE Interviews · 12 min read · Updated

About the original · 2023
AUTHORS
Khattab, Singhvi, Maheshwari, Zhang, et al.
FROM
Stanford University, UC Berkeley, and others
PUBLISHED
Oct 2023 · ICLR 2024
CITED BY
1,112Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 20% of this breakdown.

01

The paper in one minute

By 2023, serious LLM applications were pipelines: rewrite the question, retrieve, reason, check, answer. Each stage ran on a prompt someone had written and rewritten by hand until it worked. The authors call these "lengthy strings discovered via trial and error". Swap the model, add a stage or change the data, and the strings had to be rediscovered.

DSPy, from Stanford, proposes treating the pipeline like a program and the prompts like compiled output. You declare what each language model call should do with a signature, such as "context, question -> search_query". You compose those calls in ordinary Python modules, the way you would compose layers in a neural network. Then you give a compiler a small training set and a metric. The compiler runs your program on the training inputs, keeps the runs that score well, and uses their intermediate steps as few-shot demonstrations for every module. It can also search over which demonstrations to use, ensemble programs, or fine-tune a small model to imitate a large one.

In two case studies, math word problems and multi-hop question answering, programs of a few lines compiled in minutes to tens of minutes. They beat standard few-shot prompting by large margins and matched or beat demonstrations written by experts. Compiling also made a 13-billion-parameter open model competitive with GPT-3.5, and produced a fine-tuned 770-million-parameter T5 model that came close.

The broader idea outlived the framework's specific API: a prompt is a parameter to optimize against an evaluation, not a piece of craft to protect.

THE IDEA, VISUALLY
A PIPELINE AS A PROGRAM, NOT A PROMPT generate_query = ChainOfThought("context, question -> search_query") generate_answer = ChainOfThought("context, question -> answer") Question Query Retrieve Query Retrieve Answer two hops of retrieval, three LM calls, no hand-written prompt 200 examples questions + answers a metric exact match Compiler runs the program, keeps traces that pass, picks demos Optimizers bootstrap their own few-shot examples, search over them, and can fine-tune a small model. GSM8K TEST ACCURACY Table 1, before and after compiling 25.2 81.6 GPT-3.5 9.4 46.9 Llama2-13b-chat zero-shot, one call best compiled program
Prompts as compiled output. You write modules and a metric; the compiler runs the pipeline on a few examples and keeps the demonstrations that make it score well.
Research notes: the problem, approach and results
25% → 82%

GPT-3.5 on GSM8K, from one zero-shot call to the best compiled program (test set)

200examples

of training data per task, with labels only for final answers

770Mparams

T5-Large fine-tuned by the compiler: 39.3% on HotPotQA dev, against 34.3% for GPT-3.5 with plain few-shot prompting

THE PROBLEM

LLM pipelines were built from long prompt strings found by trial and error. They broke when the model, the data or a stage changed, and nobody could say why they worked.

THE IDEA

Write the pipeline as code with typed modules (inputs to outputs), then let an optimizer bootstrap demonstrations and tune each module against a metric.

THE RESULT

Short programs compiled in minutes beat standard few-shot prompting and expert-written demonstrations, and made a 13B open model competitive with GPT-3.5.

02

Why it matters to an FDE

On a customer project, prompts are where maintenance goes to die. Five engineers edit one template over three months, nobody remembers why a sentence is there, and a model upgrade silently breaks it. This paper gives a disciplined alternative even if you never use the library.

Separate the what from the how. A signature says what a step takes and produces. The prompt text, examples and reasoning style are implementation details that can be regenerated. Keeping the two apart is what lets you switch models without rewriting everything by hand.

The metric becomes the specification. A compiler optimizes exactly what you measure. The paper shows it: on math problems, the simple "answer directly" program learned to reason inside its answer field, because the metric only extracted the final number. That was allowed, and it helped. In a business setting the same pressure can produce outputs that pass the metric and fail the user. Writing the metric carefully is now the main design task.

Hold out a test set. The compiler picks what scores best on a development set, which overfits it. GPT-3.5's best math program scored 80.3% on development examples and 72.9% on the untouched test set. If you optimize prompts against an eval, you need a second eval the optimizer never saw.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 49 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦3 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The ideas to keep, the three abstractions, the results by program and the misreadings to avoid

    Free account
  2. 04How it works

    Signatures, modules, bootstrapping, random search and distillation, with the paper's own programs

    Premium
  3. 05The numbers that matter

    Both case studies in full, the dev-to-test gap, and the small-model result

    Premium
  4. 06Try it yourself

    A miniature demo-selection optimizer that shows why you need a held-out test set

    Premium
  5. 07Where it breaks

    What the paper leaves open, and the problems teams hit adopting the approach

    Premium
  6. 08Using it on a deployment

    Bringing compile-against-a-metric discipline to a customer pipeline, with or without the library

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →