DSPy: Definition and Examples
DSPy is a Python framework developed by Stanford NLP that allows you to program and automatically optimize language model (LLM) pipelines, replacing manual prompt engineering with a declarative, compiled approach.
Full definition
DSPy (Declarative Self-improving Python) is an open-source framework created by the Stanford NLP team, designed to transform how developers interact with language models. Rather than manually writing fragile and hard-to-maintain prompts, DSPy offers a programmatic approach where you define signatures (expected inputs/outputs) and composable modules, and the framework automatically optimizes the underlying prompts.
The core of DSPy rests on three fundamental concepts: signatures, which describe what a module should do (e.g., "question -> answer"); modules, which encapsulate prompting strategies like ChainOfThought or ReAct; and optimizers (formerly called teleprompters), which automatically adjust prompts and few-shot examples to maximize a user-defined metric.
Concretely, DSPy functions as a compiler for LLMs: the developer writes a declarative Python program, provides a training dataset and an evaluation metric, then the optimizer explores different combinations of prompts, examples, and strategies to find the most performant configuration. This approach eliminates the need to manually tinker with prompt formulations.
The major advantage of DSPy is its modularity and portability. A DSPy pipeline can be transferred from one model to another (e.g., from GPT-4 to Claude or Llama) simply by rerunning optimization, without rewriting prompts. This makes LLM applications more robust, reproducible, and maintainable in the long term.
Etymology
The name DSPy is an acronym for "Declarative Self-improving Python." It pays homage to PyTorch and the idea of compiling natural language programs in the same way neural networks are compiled. The "D" emphasizes the declarative approach (you describe what to do, not how), the "S" stands for self-improvement via automatic optimization, and "Py" anchors it in the Python ecosystem.
Concrete examples
Creating a question-answering system with chain-of-thought reasoning
class QA(dspy.Module):
def init(self):
self.generate = dspy.ChainOfThought('question -> answer')
def forward(self, question):
return self.generate(question=question)
Automatic optimization of an RAG (Retrieval-Augmented Generation) pipeline
teleprompter = dspy.BootstrapFewShot(metric=exact_match)
optimized_rag = teleprompter.compile(rag_pipeline, trainset=train_examples)
Replacing a fragile manual prompt with a declarative signature portable across models
Practical usage
In prompt engineering, DSPy is particularly useful when you need to maintain complex LLM pipelines in production. Instead of fine-tuning each prompt manually, you define your modules and success metrics, then let the optimizer find the best prompts. Adopt DSPy when your prompts become too fragile, you frequently change models, or you need reproducible and measurable results.
Related concepts
FAQ
What is the difference between DSPy and classic prompt engineering?
Do I need machine learning knowledge to use DSPy?
Does DSPy work with all language models?
See also
How to use this prompt
- Copy the prompt with the button above.
- Paste it into ChatGPT, Claude or your favorite AI assistant.
- Replace the bracketed variables with your details, then refine the result.
About Prompt Guide
Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.
More definitions
ElevenLabs: Definition and Examples
ElevenLabs is a company specializing in AI-powered speech synthesis, capable of generating realistic and expressive voices from text.
Embedding: Definition and Examples
An embedding is a numerical representation of text, image, or other data type as a vector of numbers, enabling AI models to measure semantic similarity between items.
Encoder Decoder: Definition and Examples
Neural network architecture composed of two complementary modules: an encoder that compresses the input into an intermediate representation, and a decoder that generates the output from this representation.
European AI Act: Definition and Examples
The European AI Act is the world's first regulatory framework dedicated to artificial intelligence, adopted by the European Union to govern the development,
Existential AI Risk: Definition and Examples
Existential AI risk refers to the possibility that advanced artificial intelligence could cause human extinction or irreversible degradation
F1 Score: Definition and Examples
The F1 Score is an evaluation metric that combines precision and recall into a single value, calculated as their harmonic mean. It is particularly useful for assessing model performance on imbalanced datasets.
Get new prompts every week
Join our newsletter.