PromptsApr 8, 2026·2 min read

DSPy — Programming Foundation Models Declaratively

Replace hand-written prompts with modular programs. DSPy compiles declarative AI pipelines into optimized prompts automatically, boosting reliability and performance.

TL;DR
DSPy lets you define AI pipelines declaratively and compiles them into optimized prompts, replacing manual prompt engineering.
§01

What it is

DSPy is a framework for programming (not prompting) language models. Instead of tweaking prompt strings, you define what your AI pipeline should do declaratively using signatures and modules, then DSPy optimizes the prompts automatically through compilation. It treats LLM calls as optimizable modules, similar to how PyTorch treats neural network layers.

DSPy is created at Stanford NLP and targets AI engineers building reliable LLM pipelines who want systematic optimization over manual prompt engineering.

§02

How it saves time or tokens

Manual prompt engineering is trial and error. DSPy replaces this with automatic compilation that optimizes prompts based on evaluation metrics. The optimizer tries different prompt strategies and selects the one that scores highest on your test set. This produces better prompts in less time, and the resulting prompts are often more token-efficient because the optimizer removes unnecessary instructions.

§03

How to use

  1. Install DSPy:
pip install dspy
  1. Configure a language model:
import dspy

lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)
  1. Define a signature and module:
qa = dspy.ChainOfThought('question -> answer')
result = qa(question='What is the capital of France?')
print(result.answer)
§04

Example

Building a fact-checking pipeline with compilation:

import dspy

class FactCheck(dspy.Signature):
    claim = dspy.InputField(desc='A factual claim to verify')
    evidence = dspy.OutputField(desc='Supporting or refuting evidence')
    verdict = dspy.OutputField(desc='True, False, or Uncertain')

class FactChecker(dspy.Module):
    def __init__(self):
        self.check = dspy.ChainOfThought(FactCheck)

    def forward(self, claim):
        return self.check(claim=claim)

# Compile with training examples
from dspy.teleprompt import BootstrapFewShot

optimizer = BootstrapFewShot(metric=your_metric)
compiled_checker = optimizer.compile(FactChecker(), trainset=train_data)

# Use the optimized pipeline
result = compiled_checker(claim='The Earth is flat')
print(result.verdict)
§05

Related on TokRepo

§06

Common pitfalls

  • Compilation requires a training set with labeled examples; without good examples, the optimizer cannot improve over the default prompt
  • DSPy adds abstraction overhead; for simple one-shot tasks, direct prompting is simpler and equally effective
  • The optimizer runs multiple LLM calls during compilation, which can be expensive; start with a small training set and a cheaper model

Frequently Asked Questions

What models does DSPy support?+

DSPy works with OpenAI (GPT-4, GPT-4o), Anthropic Claude, Google Gemini, and local models via Ollama or vLLM. The dspy.LM class accepts model identifiers in the format provider/model-name. Any model with a chat or completion API is compatible.

What is a DSPy Signature?+

A Signature defines the input and output fields of an LLM call. It can be a simple string like 'question -> answer' or a class with typed InputField and OutputField attributes. Signatures describe what the module does without specifying how.

How does compilation work?+

Compilation runs an optimizer (like BootstrapFewShot or MIPRO) that tries different prompt strategies on your training data. It evaluates each strategy using your metric function and selects the one that scores highest. The result is an optimized module with better prompts.

Can I use DSPy with RAG pipelines?+

Yes. DSPy has built-in retrieval modules that integrate with search engines and vector stores. You can compose retrieval and generation modules into a single optimizable pipeline, and the compiler optimizes both the retrieval query and the generation prompt.

How is DSPy different from LangChain?+

LangChain provides building blocks for LLM applications (chains, tools, memory). DSPy focuses on automatic prompt optimization through compilation. They serve different needs: LangChain for application architecture, DSPy for systematic prompt engineering. They can be used together.

Citations (3)
  • DSPy GitHub— DSPy is a framework for programming foundation models
  • DSPy Paper— Created at Stanford NLP for systematic prompt optimization
  • DSPy Docs— Compilation optimizes prompts automatically
🙏

Source & Thanks

Created by Stanford NLP. Licensed under MIT.

stanfordnlp/dspy — 22k+ stars

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.