# DSPy

> Canonical: https://skillalmanac.com/glossary/dspy | Last verified: 2026-11-24 | Index status: not offered to search engines

DSPy is a Python framework for building language-model programs. You declare each task's inputs and outputs, and optimizers tune the prompts against a metric you define, instead of you writing the prompts by hand.

Also called: dspy framework, dspy stanford, dspy python, DSPy library.

## In plain language

Most people steer a language model by writing and rewriting a prompt until it works, and the prompt often breaks when the model changes. DSPy is a Python toolkit that asks you to describe the job instead: what goes in, what should come out, and how to score a good answer. It then tries prompt variations and keeps the better ones. You still need example data and a score that reflects what you care about, and tuning costs model calls. A poor score function gives you a program that is tuned to the wrong thing.

## Technical definition

DSPy's documentation describes three ideas. Signatures declare typed inputs and outputs for a task. Modules set the strategy for running a signature, for example direct completion, step-by-step reasoning or a tool-using loop, behind the same interface. Optimizers take examples and a metric and tune the program against it. The paper by Khattab et al. (arXiv, submitted 5 October 2023) presents DSPy as a programming model that treats language-model pipelines as text-transformation graphs and uses a compiler to optimise a pipeline for a given metric.

## How it shows up when you use agents

DSPy is a library, not a coding agent. You meet it when someone wants a language-model pipeline, such as question answering over documents, to improve by measurement instead of by editing prompt text. In a coding-agent session it shows up as a Python dependency you or the agent installs with pip install -U dspy, per dspy.ai.

- **The metric is the specification.** The optimizer pushes the program toward whatever the score function rewards. If the metric is weak, the tuned prompts follow it. Write the metric first and test it on a few cases by hand.
- **Data comes first.** Optimizers need examples. A small, clean, held-out test set is the safer habit, and examples used for tuning should not appear in the test set.
- **Tuning is not free.** IBM describes the method as the model proposing prompt variations that are tested against your metric, which implies several model calls per run. Check your provider's pricing and [rate limits](/glossary/rate-limit) before a large run.
- **Prompts still exist.** DSPy generates them; you can read them. Inspect what it produced before you ship it.

Next action: try it on one narrow task with a small set of labelled examples and compare the result with your hand-written prompt on the same held-out set. See [Prompt engineering](/glossary/prompt-engineering).

## Common confusions

- **DSPy versus prompt engineering.** DSPy automates part of the work by searching over prompts against a metric. It does not remove the need to define the task and the score.
- **DSPy versus LangChain.** Both are used to build language-model applications, and DSPy's main idea is optimizing programs against a metric. See [LangChain](/glossary/langchain) for its own scope.
- **Optimizing versus fine-tuning.** The paper says DSPy modules can learn to apply prompting, finetuning, augmentation and reasoning techniques, so some options change model weights and others change only prompts. Read the optimizer's documentation to see which one you are using. See [Fine-tuning](/glossary/fine-tuning).
- **A signature versus a prompt.** A signature declares inputs and outputs; the prompt text is produced from it.

## Worked examples in this catalogue

### [prompt-writing](https://skillalmanac.com/skills/skill-almanac/prompt-writing)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill writes and lints LLM prompts with a fixed structure, injection rules and a numeric test plan.

Why it exercises the concept: This is the hand-written route that DSPy tries to replace with declared tasks and an optimizer. Having both in view shows what moves from prompt text into code and metrics.

### [llm-eval-test-sets](https://skillalmanac.com/skills/skill-almanac/llm-eval-test-sets)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill builds and validates an eval dataset for any LLM feature, with a checker for duplicates, missing graders, imbalance and leakage.

Why it exercises the concept: DSPy optimizers need examples and a scoring function. A clean eval set is the kind of input they rely on, and leakage between tuning and test data would distort the result.

### [Prompt Engineering for Agents vs Chat Models](https://skillalmanac.com/learn/prompt-engineering-guide-for-agents)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The guide covers prompt engineering for coding agents and agent runs.

Why it exercises the concept: It shows the manual practice that a programmatic approach such as DSPy aims to automate, so you can judge when hand-tuning is enough.

## Related terms

- [Prompt engineering](https://skillalmanac.com/glossary/prompt-engineering.md)
- [Prompt template](https://skillalmanac.com/glossary/prompt-template.md)
- [Few-shot prompting](https://skillalmanac.com/glossary/few-shot-prompting.md)
- [Chain of thought](https://skillalmanac.com/glossary/chain-of-thought.md)
- [Retrieval-augmented generation (RAG)](https://skillalmanac.com/glossary/retrieval-augmented-generation.md)

## Sources

- [DSPy](https://dspy.ai/current/), DSPy project (Stanford NLP) (accessed 2026-11-24)
- [DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines](https://arxiv.org/abs/2310.03714), arXiv (Khattab et al.) (accessed 2026-11-24)
- [What is DSPy?](https://www.ibm.com/think/topics/dspy), IBM (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-24. [Glossary index](https://skillalmanac.com/glossary.md).
