DSPy (Declarative Self-improving Python) is a Stanford-developed framework that treats prompts as compilable programs rather than brittle strings, enabling systematic optimization of LLM pipelines through algorithms instead of manual prompt engineering. Unlike traditional orchestration tools like LangChain that focus on integration patterns, DSPy emphasizes automatic prompt and weight optimization, abstracting LMs into modular, composable building blocks with signatures (input-output specs), modules (reasoning strategies), and teleprompters (optimizers) that compile programs into optimized prompts and few-shot examples tailored to specific tasks and metrics. The central insight: treating prompting as a compilation problem—writing declarative task specifications and letting DSPy's compilers search over prompt variations, instruction phrasing, and example selections to maximize objective metrics—fundamentally shifts LLM development from manual trial-and-error to data-driven, reproducible engineering.
What This Cheat Sheet Covers
This topic spans 20 focused tables and 126 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core DSPy Abstractions
DSPy's three foundational primitives—signatures, modules, and optimizers—define a programming model where you declare what your system should accomplish, choose how to structure reasoning, and compile the program into optimal prompts. Understanding these building blocks is essential: signatures specify task semantics, modules provide reusable reasoning patterns, and optimizers automate the search for high-performing configurations.
| Concept | Example | Description |
|---|---|---|
"question -> answer"class QA(dspy.Signature): question = dspy.InputField() answer = dspy.OutputField() | Declarative input-output specification for LM behavior; can be a short string or a class with typed fields and descriptions. Acts as the contract between your program and the LM. | |
dspy.Predict(QA)dspy.ChainOfThought(QA)dspy.ReAct(tools=[...]) | Reusable reasoning strategy that wraps a signature; abstracts prompting techniques (e.g., chain-of-thought, ReAct) into parameterized components. All modules are subclasses of dspy.Module. | |
teleprompter = dspy.BootstrapFewShot()compiled = teleprompter.compile( program, trainset=data, metric=accuracy) | Algorithm that tunes prompts and/or LM weights to maximize a metric; searches over instruction phrasing, few-shot examples, and reasoning steps. Historically called "teleprompters." | |
dspy.Example( question="What is 2+2?", answer="4").with_inputs("question") | Data container for train/dev/test sets; wraps input-output pairs with optional .with_inputs() to mark which fields are inputs vs labels. |