Framework for programming rather than prompting language models. Compiles LM calls into self-improving pipelines by tuning prompts and/or weights to maximize user-defined metrics. Use when building classifiers, RAG pipelines, agents, or any multi-stage LM program requiring automated prompt engineering, few-shot bootstrapping, or model fine-tuning driven by evaluation metrics.
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Framework for programming rather than prompting language models. Compiles LM calls into self-improving pipelines by tuning prompts and/or weights to maximize user-defined metrics. Use when building classifiers, RAG pipelines, agents, or any multi-stage LM program requiring automated prompt engineering, few-shot bootstrapping, or model fine-tuning driven by evaluation metrics.
DSPy (Declarative Self-improving Python) is the framework for programming—rather than prompting—language models. Instead of writing brittle, hand-crafted prompts, you write compositional Python code using declarative modules and let DSPy's optimizers teach your language model to deliver high-quality outputs.
DSPy provides three core abstractions:
Signatures — Declarative specifications of input/output behavior that tell the LM what to do without specifying how.
Modules — Building blocks that abstract prompting techniques (chain-of-thought, ReAct, program-of-thought) and can be composed into larger programs.
Optimizers (formerly Teleprompters) — Algorithms that tune prompts and/or LM weights to maximize user-defined metrics like accuracy.
When to Use
Building classifiers that need automated prompt optimization instead of manual tuning
Creating RAG pipelines where retrieval and generation steps benefit from compiled prompts
Implementing agent loops (ReAct, ProgramOfThought) with tools and self-correction
Any multi-stage LM program where you want to optimize few-shot examples, instructions, or model weights driven by evaluation metrics
Fine-tuning small LMs on task-specific data using DSPy's BootstrapFinetune optimizer
Enforcing computational constraints on LM outputs with DSPy Assertions
Building typed LM programs with TypedPredictor for Pydantic-compatible structured outputs
response = qa(question="How many floors are in the castle?")
print('GPT-3.5:', response.answer)
gpt4 = dspy.OpenAI(model='gpt-4-1106-preview', max_tokens=300)
with dspy.context(lm=gpt4):
response = qa(question="How many floors are in the castle?")
print('GPT-4:', response.answer)
Generating multiple completions: Use n=5 in the module constructor or pass config=dict(n=5) when invoking. Access via response.completions.answer.
Inspecting LM history: Call lm.inspect_history(n=3) after running a program to see the last N prompts/responses.
Retrieval Models
Configure a retrieval model for dspy.Retrieve to use:
colbertv2 = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')
dspy.configure(rm=colbertv2)
retriever = dspy.Retrieve(k=3)
passages = retriever("When was the first FIFA World Cup?").passages
Custom RM clients can inherit from dspy.Retrieve and implement a forward method returning dspy.Prediction(passages=...).
Optimizers (Teleprompters)
An Optimizer tunes program parameters to maximize your metric. Import from dspy.teleprompt: