DSPy
Program — don't prompt — language models: declarative Python modules with compiler-driven optimization.
About
DSPy (Declarative Self-improving Python, from Stanford NLP) replaces hand-crafted prompts with typed Signatures and composable modules (Predict, ChainOfThought, ReAct) that a compiler optimizes automatically. You declare inputs/outputs and supply a metric plus training examples; optimizers like GEPA and MIPROv2 search for the best instructions and few-shot examples for you.
Because prompts become compiled artifacts, programs are portable across models — recompile for the new model instead of rewriting prompts. It is used for classifiers, RAG pipelines, and agent loops, with evaluation-driven iteration as the core workflow.
What it can do
- • Typed Signatures: declare tasks as inputs/outputs instead of prompt strings
- • Composable modules: Predict, ChainOfThought, ReAct, ProgramOfThought, plus custom dspy.Module pipelines
- • Automatic optimizers: GEPA, MIPROv2 and others that tune instructions and few-shot demos against your metric
- • Built-in evaluation: metrics, assertions, and saved compiled artifacts (JSON) for versioning
- • Model portability: recompile the same program for a different LM without rewriting prompts
- • Multimodal support including dspy.Image inputs and PythonInterpreter tooling
- • Works with OpenAI, Anthropic, Google, and local models via a unified dspy.LM interface
Setup
- Install:
pip install dspy(extras likedspy[openai]ordspy[all]pull provider SDKs) - Configure your LM (needs the provider API key in env):
import dspy; lm = dspy.LM("openai/gpt-4o-mini"); dspy.settings.configure(lm=lm) - Declare a signature:
class QA(dspy.Signature): """Answer questions with short factual answers."""; question = dspy.InputField(); answer = dspy.OutputField() - Run a module:
qa = dspy.Predict(QA)(ordspy.ChainOfThought(QA)for reasoning);qa(question="What is the capital of France?") - Optimize against evals: build a trainset and metric, then
optimizer = dspy.GEPA(metric=accuracy, auto="medium"); optimized = optimizer.compile(qa, trainset)andoptimized.save("qa_v2.json")
Prerequisites
Python 3.9+; at least one LLM provider API key; a labeled trainset and a metric function if you want to use the optimizers (compilation runs cost real API calls).
Use cases
Works with
Trust score — 9/10
Scored 9/10 (verified tier, metrics-v1). Evidence: 38,601 GitHub stars (log-scaled vs catalog peers); blended with ~5,312,050 monthly package downloads; last push 0d ago; 14 releases/12mo; 811 open issues. Manual review: steep learning curve; optimizer API-budget costs; '10-40% lift' claims third-party. License: MIT.
★ 38,601 GitHub stars
How the trust score is built
Full methodology →- Adoption · stars/downloads, log-scaled vs peers
- 8.9/10
- Maintenance · recency, releases, issue load
- 9.2/10
- Openness · license + docs
- 10.0/10
- Manual review · sanity read + red-flag check
- 9.0/10
- · 38,601 GitHub stars (log-scaled vs catalog peers)
- · blended with ~5,312,050 monthly package downloads
- · last push 0d ago; 14 releases/12mo; 811 open issues
- · manual review: steep learning curve; optimizer API-budget costs; '10-40% lift' claims third-party
Sources
Every fact above was checked against these sources. Where a claim comes from the provider and couldn't be independently verified, the text says so.