DSPy
Stanford framework that replaces prompt engineering with optimisers. Define your program logic, then DSPy compiles it into optimised prompts and fine-tunes using your examples.
Install
$pip install dspy About
DSPy replaces hand-written prompts with typed signatures. A signature like "question -> answer" compiles into a prompt, and an optimiser automatically improves it by running your evaluation metric on training examples.
This makes LLM programs more robust — instead of brittle string prompts, you have Python modules with defined interfaces that can be optimised, composed, and tested systematically.
Getting Started
pip install dspy
import dspy
lm = dspy.LM("anthropic/claude-sonnet-4-5", api_key="...")
dspy.configure(lm=lm)
class QA(dspy.Signature):
"""Answer questions with short factual answers."""
question: str = dspy.InputField()
answer: str = dspy.OutputField()
qa = dspy.ChainOfThought(QA)
result = qa(question="What is the capital of France?")
print(result.answer)Features
References & Links
Project Info
Topics
Ready to try DSPy?
Free and open source. Install in one command, works with Claude Code, Cursor, Windsurf, and more.
$pip install dspy