DSPy is a Python framework for building AI systems out of language model calls without hand-crafting prompts. Instead of writing and tweaking prompt strings, you write compositional Python code, and DSPy handles optimizing the prompts and weights behind it. It's built for developers and researchers who build classifiers, retrieval-augmented generation (RAG) pipelines, or agent loops on top of LLMs and want a repeatable, code-first way to improve them.
The core idea, from the team's research (Demonstrate-Search-Predict, then DSPy itself, then follow-up work on prompt/weight optimization and GEPA), is that LM pipelines should be compiled the way programs are compiled, not hand-tuned through trial and error in a prompt playground. You define modules and signatures declaratively, then let DSPy's optimizers search for the instructions and demonstrations that make the pipeline perform well on your data.
DSPy fits well if you're building a multi-step LM pipeline (retrieval + generation, multi-hop question answering, agent loops with tool calls) and want a systematic way to tune it as your data or model changes. It's also a good fit for extreme multi-label classification, self-refining pipelines with assertions/constraints, or research projects that need reproducible prompt optimization rather than manual tweaking.
It's less of a fit if you just need a single one-off prompt for a simple task; writing a DSPy program for that adds overhead without much payoff. It's also not the right tool if you want a no-code prompt UI or a hosted API service. DSPy is a Python library you integrate into your own codebase, so you need to be comfortable writing Python and running your own optimization/compilation steps against real evaluation data.
Install the latest stable release from PyPI:
pip install dspy
To install the latest development version directly from the main branch:
pip install git+https://github.com/stanfordnlp/dspy.git
After installing, the framework and its usage patterns (defining signatures, modules, and running optimizers) are documented on the official docs site rather than in the repo's README, so check there for tutorials and API details before writing your first pipeline.