Favicon of DSPy

DSPy

DSPy is a Python framework from Stanford NLP for programming language models with modular code instead of hand-tuned prompts.

DSPy website screenshot
DSPy GitHub repository preview

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.

Key features

  • Declarative modules: define what each step of your pipeline should do (inputs, outputs, task) instead of writing raw prompt text.
  • Prompt and weight optimization: DSPy includes algorithms (compilers/optimizers) that automatically tune instructions, few-shot demonstrations, and in some setups model weights.
  • Composable pipelines: chain modules together for multi-stage tasks like RAG, multi-hop retrieval, or classification, and DSPy optimizes across the whole pipeline, not just one call.
  • Self-improving loops: pipelines can be recompiled against new data or metrics, letting them adapt rather than needing a prompt rewrite each time.
  • Research-backed approach: the framework's design comes directly from published papers on compiling LM calls, auto-optimized hyperparameters, and reflective prompt evolution (GEPA).
  • Model-agnostic Python code: since pipelines are plain Python, they aren't tied to one prompt format or one specific LM provider's syntax.

Ideal use cases

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.

Installation

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.

Frequently asked questions

Share:

Stars
37.6K
Forks
3.3K
Last commit
23 hours ago
Repository age
4 years
License
MIT
Self-hosted
No
Activity score
85/100
View Repository
Built with:
Ad
Favicon

 

  
 

Similar to DSPy

Favicon

 

  
 
Favicon

 

  
 
Favicon