AI/TLDR

DSPy

Program language models with composable modules instead of brittle prompts

Overview

DSPy (Declarative Self-improving Python) is a framework from Stanford for building AI systems by writing code instead of hand-tuning prompt strings. You describe each task as a typed signature with inputs and outputs, compose those into modules, and let DSPy handle the underlying calls to the language model.

It is aimed at developers who are tired of brittle, copy-pasted prompts and want their LLM pipelines to be testable and maintainable. The same program can run against different models, and DSPy includes optimizers that tune the prompts and weights for you based on examples and a metric.

As an agent framework, DSPy covers a wide range: simple classifiers, multi-step RAG pipelines, and agent loops. The building blocks stay the same, so you scale up complexity by composing modules rather than rewriting prompts.

What it does

  • Define tasks as typed signatures (input and output fields) instead of raw prompt text
  • Compose reusable modules like dspy.Predict to build classifiers, RAG, and agent loops
  • Optimizers automatically tune prompts and weights against your examples and metric
  • Swap the underlying language model without rewriting your program logic
  • Backed by published research, including the GEPA and DSPy compiling papers
  • Active community with documentation at dspy.ai and a Discord server

Getting started

Install DSPy from PyPI, point it at a language model, and run a minimal program built from a typed signature.

Install DSPy

Install the package from PyPI. To get the latest unreleased changes, install directly from the main branch.

bashbash
pip install dspy

Configure a language model

Create an LM and set it as the default. Use the model identifier for whichever provider you have configured.

pythonpython
import dspy

lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)

Write and run a signature

Declare the task as a signature with input and output fields, wrap it in a module, and call it.

pythonpython
class ExtractEvent(dspy.Signature):
    """Extract event details from an email."""
    email: str = dspy.InputField()
    event_name: str = dspy.OutputField()
    date: str = dspy.OutputField()

extract = dspy.Predict(ExtractEvent)
result = extract(email="Your email text here")
print(result)

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • Build a RAG pipeline where retrieval and generation are modular and individually testable
  • Replace fragile hand-written prompts with typed signatures you can version and refactor
  • Automatically optimize a multi-step LLM program's prompts against a metric and examples
  • Prototype an agent loop and reuse the same code across different language models

How DSPy compares

DSPy alongside other open-source prompt programming tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
DSPy★ 35.8kProgram language models with composable modules instead of brittle prompts
ell★ 5.9kA Python library that treats prompts as versioned functions, with tooling to track, visualize, and iterate on them as code.
GEPA★ 5.5kA reflective, evolutionary optimizer that improves prompts and other text components of a system using language-model feedback.
LMQL★ 4.2kA query language for LLMs that mixes Python control flow with prompts and constraints to script multi-step generation.
AdalFlow★ 4.2kA PyTorch-like library for building and auto-optimizing LLM pipelines, tuning prompts across the components of a task.
TextGrad★ 3.6kA library that optimizes prompts and other text variables using textual gradients, applying a backpropagation-like loop driven by LLM feedback.
Mirascope★ 1.5kA lightweight Python toolkit for writing LLM calls as typed functions with prompt templates, chaining, and a single interface across providers.