DSPy
by Stanford NLP and the DSPy community · Framework · also known as dspy-ai, Declarative Self-improving Python
Python framework for programming language-model pipelines with typed signatures and modules, then optimizing prompts and weights automatically against a metric
Overview
DSPy is an MIT-licensed Python framework that started at Stanford NLP in 2022 and treats LLM pipelines as programs to compile rather than prompts to hand-tune. A signature declares a task's typed inputs and outputs; modules such as Predict, ChainOfThought and ReAct decide how the signature is executed, including tool use; and optimizers (MIPROv2, GEPA and others) rewrite instructions, select few-shot demonstrations or fine-tune weights to maximize a user-defined metric on example data. Models are addressed through provider-prefixed names, so the same program can target hosted APIs or local runtimes.
The approach suits research groups and teams that need measurable, reproducible pipelines: classification, extraction, retrieval-augmented QA or multi-stage reasoning where a labeled development set exists. Its limits are that optimization consumes many model calls, that results depend on the quality of the metric and data, and that the compiled prompts are less transparent than hand-written ones. The project is at version 3.x and remains actively developed with a lineage of published papers.
Capabilities
- Typed signatures for task specification
- Modules: Predict, ChainOfThought, ReAct and custom compositions
- Optimizers such as MIPROv2 and GEPA for prompts and weights
- Metric-driven evaluation and compilation
- Provider-agnostic model addressing
Typical workflows
- Compile a classification pipeline against a labeled dev set
- Optimize a RAG program's instructions and demonstrations
- Compare optimizers on the same metric
Strengths
- Systematic alternative to manual prompt engineering
- Research-backed optimizers
- MIT license and active community
Limitations
- Optimization is model-call intensive
- Requires labeled examples and a good metric
- Compiled prompts are harder to audit
Prerequisites
- Python 3.10+
- A model provider or local runtime
Who it is for
- NLP researchers
- Engineers building measurable LLM pipelines
Alternatives & comparison
- Instructor
Python library for extracting validated structured data from LLM responses using Pydantic models, with automatic retries, streaming partials and support for many providers
Open sourceOpen source - LangChain
Open-source Python and JavaScript framework whose create_agent harness builds tool-using agents on the LangGraph runtime with swappable model providers and middleware
Open sourceOpen source - promptfoo
Open-source, local-first CLI and library for evaluating prompts, models, RAG pipelines and agents, with automated red teaming and CI integration
MixedOpen source - Pydantic AI
Type-safe Python agent framework from the Pydantic team with validated structured outputs, dependency injection, swappable model providers, MCP, durable execution and evals
Open sourceOpen source - Ragas
Open-source Python library for evaluating LLM and RAG applications with LLM-driven and custom metrics, experiments, dataset management and result tracking
Open sourceOpen source
Sources & verification
Primary sources consulted for this record. Claims are tied to the fields each source supports; the automated link checker re-verifies every URL on a schedule.
- Licensehttps://github.com/stanfordnlp/dspy/blob/main/LICENSEchecked Nov 1, 2026
Supports: license_spdx, source_model
- Official sitechecked Nov 1, 2026
- Repositoryhttps://github.com/stanfordnlp/dspychecked Nov 1, 2026
Supports: license_spdx, languages
- Confidence
- High
- Reviewed
- Nov 1, 2026
- Links
- Verified · Nov 2, 2026
Spotted an error?
More in Prompting, evaluation & observability
Langfuse
Langfuse (part of ClickHouse, Inc. since January 2026) · Prompting, evaluation & observability
Open-source LLM observability and evaluation platform: tracing, LLM-as-judge and human evals, prompt management, datasets and cost dashboards, self-hostable or as Langfuse Cloud
LiteLLM
BerriAI · Prompting, evaluation & observability
Open-source Python SDK and self-hosted AI gateway that calls 100+ LLM providers in the OpenAI format, with routing, fallbacks, virtual keys, budgets, spend tracking and logging callbacks
OpenRouter
OpenRouter, Inc. · Model providers & APIs
Unified API gateway to hundreds of models from many providers with automatic fallbacks, credit-based pass-through billing, BYOK, free models and data-policy routing
LangSmith
LangChain, Inc. · Prompting, evaluation & observability
LangChain's hosted platform for tracing, evaluating and deploying LLM applications and agents, with prompt tooling, an LLM gateway and cloud, hybrid or self-hosted options
Weights & Biases
CoreWeave · Training & fine-tuning
ML experiment tracking, sweeps, artifacts and model registry plus Weave for LLM tracing and evaluation, now sold by CoreWeave within its Forge platform with a free academic tier
Ragas
Vibrant Labs (formerly Exploding Gradients) · Prompting, evaluation & observability
Open-source Python library for evaluating LLM and RAG applications with LLM-driven and custom metrics, experiments, dataset management and result tracking