# DSPy

> A Python kit. You say what goes in and out, and it writes the prompts.

**Field:** Software Development  
**Category:** AI Engineering  
**Also known as:** DSPy Framework, Stanford DSPy

## Think of it like

It is like a cook who tries a dish many ways and keeps the best.

## Definition

DSPy is a free, open-source Python framework that writes your prompts for you. You declare a task as a signature: what goes in and what comes out. A module decides how to run it, such as a plain answer, step by step reasoning or a tool loop. An optimizer takes your examples and a scoring rule. It then tunes the prompts to score higher. DSPy began at Stanford.

## Example

Jack's team declares email in, event name and date out. The optimizer tries prompt wording on 50 labeled emails and keeps the version that scores best.

## Example code

```python
import dspy

dspy.configure(lm=dspy.LM("openai/gpt-5.4-nano"))
extract = dspy.Predict("email -> event_name, date")
result = extract(email="Lunch with Emma on May 3 at noon")
```

The signature says what goes in and what comes out. DSPy writes the prompt.

## Why it matters

Hand-written prompts often break when you change models. DSPy keeps the task the same and tunes the wording again for the new model.

## Common mistake

Running an optimizer with a weak scoring rule. It tunes toward what the score rewards, even when that is the wrong thing.

## Three principles

- **Signatures.** Inputs and outputs, not wording.
- **Modules.** How the task is run.
- **Optimizers.** Tune prompts from examples.

## Related terms

- [LLM Framework](https://learn.benibix.com/term/development/llm-framework)
- [Prompt Engineering](https://learn.benibix.com/term/ai/prompt-engineering)
- [Chain-of-Thought Prompting](https://learn.benibix.com/term/ai/chain-of-thought-prompting)
- [Few-Shot Prompting](https://learn.benibix.com/term/ai/few-shot-prompting)
- [LLM Evaluation](https://learn.benibix.com/term/ai/llm-evaluation)
- [Pydantic AI](https://learn.benibix.com/term/development/pydantic-ai)
- [Python](https://learn.benibix.com/term/development/python)

![A table of five frameworks, with columns for framework, language and what each is built around. Vercel AI SDK, TypeScript, streaming chat screens. OpenAI Agents SDK, Python and TypeScript, agents handing work to each other. CrewAI, Python, a crew of agents, each with a role. Pydantic AI, Python, answers as typed, checked data. DSPy, Python, tuning prompts from examples. A strip along the bottom says all of them call model APIs underneath.](https://learn.benibix.com/diagrams/dev6-agent-frameworks.svg)

---

Source: https://learn.benibix.com/term/development/dspy
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-20
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
