# DSPy

> Program language models with composable modules instead of brittle prompts

- Category: App Orchestration / Prompt Programming
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/stanfordnlp/dspy
- Website: https://dspy.ai
- Canonical page: https://ai-tldr.dev/tools/dspy/

## In short

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 sits in the Prompt Programming category of the App Orchestration tools and is open-source, written in Python and licensed Apache-2.0.

## 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.

1. Install DSPy

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

```bash
pip install dspy
```

2. Configure a language model

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

```python
import dspy

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

3. Write and run a signature

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

```python
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)
```

## 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
