DSPy
Framework by Stanford for programming with foundation models using optimized prompts.
About
DSPy, short for Declarative Self-improving Python, is a Stanford NLP framework for programming language models instead of prompting them by hand. Developers express an AI system as compositional Python modules whose behavior is declared through signatures, typed specifications of inputs and outputs, rather than brittle prompt strings. DSPy optimizers then compile the pipeline, automatically refining the prompts or even fine-tuning model weights against a metric and a small set of examples, so quality improves systematically rather than through manual trial and error. The approach fits simple classifiers, retrieval-augmented generation pipelines, and agent loops alike, and the same program can be re-compiled when switching to a different model. The project was created by Omar Khattab with collaborators including Matei Zaharia and Christopher Potts, is installable from PyPI, and is open source under the MIT license. Its users are researchers and engineers who want reproducible, optimizable LLM pipelines.
Should you use DSPy?
Pick it when
Use DSPy when you have a metric and some labeled examples and want prompts, or even weights, optimized automatically, for example a RAG or classification pipeline you expect to re-target to different models over time.
Look elsewhere when
The learning curve is steep, and optimization needs an eval set plus compile runs that cost model calls. If you just need reliable typed outputs, Instructor or Pydantic AI is simpler, and DSPy adds no serving runtime or sessions.
Alternatives to DSPy
- Instructor
Validated Pydantic outputs with retries and no compilation step; much simpler, but prompts stay hand-written.
- Guidance
Grammar and regex constrained generation inside Python functions; guarantees format, but does not tune prompts against a metric.
- LangChain
Large integration ecosystem with hand-written prompts and chains; easier to start, no systematic prompt optimization.
Reviews (0)
Leave a Review
No reviews yet. Be the first to review!
Details
- Category
- AI Agents & Orchestration
- Price
- Free
- Platform
- Local/Desktop
- Difficulty
- Advanced (4/5)
- License
- MIT
- Added
- Apr 3, 2026