The framework for programming, not prompting, language models
open sourceMITself-hostablefree
DSPy (Declarative Self-improving Python) is a Python framework from Stanford NLP for building AI systems out of modular code instead of hand-written prompts. You declare what each step takes and returns as a signature, compose modules (chain of thought, ReAct agents, RAG and your own), and let DSPy's optimizers, such as GEPA and the BootstrapFewShot family, tune the prompts and few-shot examples, or fine-tune the weights, against a metric you define. It covers classifiers, extraction, RAG pipelines and agent loops, with tools and MCP, adapters that turn signatures into prompts, caching, streaming and async.
Problems it solves
- my LLM prompts are brittle and break whenever I change models
- automatically optimize the prompts and few-shot examples in my LLM pipeline against a metric
- write LLM pipelines as composable Python modules instead of long prompt strings
- improve a RAG pipeline's answer quality without hand-tuning every prompt
- build a ReAct agent with tools in Python and tune it on my own examples
- fine-tune a smaller model to match my LLM program's outputs
- get structured typed outputs from an LLM by declaring inputs and outputs
- evaluate an LLM program on a dataset and compare optimizers
Features
- Signatures declare a step's inputs and outputs instead of a prompt
- Composable modules: Predict, ChainOfThought, ReAct and custom modules
- Optimizers including GEPA and the BootstrapFewShot family tune prompts and examples
- Fine-tuning and experimental RL optimization of weights
- Metrics and evaluation utilities
- Tools and MCP support for agents
- Adapters (Chat, JSON, XML) that turn signatures into prompts
- Caching, streaming, async and saving and loading programs
- Observability and optimizer tracking
Alternatives
The same job
These do what DSPy does.
- LlamaIndex A Python toolkit for building LLM-powered agents over your data
- LangChain A framework for building agents and LLM-powered applications
- Haystack Open-source AI orchestration framework for building production-ready LLM applications in Python
