P

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

Prompt EngineeringFew-Shot LearningChain of ThoughtRAG (Retrieval-Augmented Generation)

FAQ

What is the difference between DSPy and classic prompt engineering?
Classic prompt engineering involves manually writing instructions for an LLM and adjusting them through trial and error. DSPy automates this process: you define what the model should accomplish (signature) and a success metric, then the framework automatically optimizes prompts, few-shot examples, and reasoning strategies. This is comparable to the difference between coding a neural network by hand and using PyTorch with an optimizer.
Do I need machine learning knowledge to use DSPy?
No, DSPy is designed to be accessible to Python developers without advanced ML expertise. The basic concepts (signatures, modules, optimizers) are intuitive. However, an understanding of prompting principles and how LLMs work helps in designing better pipelines and choosing the right evaluation metrics.
Does DSPy work with all language models?
Yes, DSPy is compatible with most major language models, including OpenAI models (GPT-4, GPT-4o), Anthropic (Claude), open-source models via Ollama or HuggingFace, and many others through adapters. One of DSPy's major advantages is this portability: a pipeline optimized for one model can be re-optimized for another without rewriting code.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. 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

Get new prompts every week

Join our newsletter.