Guide · Open source
How to use DSPy: install it and build your first program
Updated · written with AI from the project's own documentation and checked against our catalogue data
DSPy is an open-source Python framework from Stanford NLP for building AI systems out of modular code instead of long hand-written prompts. This guide is for Python developers who build language model pipelines such as RAG or multi-step agents. After reading it you will know what DSPy is for, how to install it, and when it is worth using.
Quick start
- 1Check Python is available. DSPy is a Python package, so you need Python installed before anything else.
- 2Install the package. Run pip install dspy to install the released version from PyPI.
- 3Read the official docs. Open dspy.ai for the framework guide and API details, since the README points there for usage.
- 4Write modular code. Describe each step of your pipeline as Python code instead of one long prompt.
- 5Define a quality measure. Decide how you will score outputs so optimization has a target to improve.
- 6Optimize and measure. Use DSPy's optimization algorithms to tune prompts and weights, then compare results against your measure.
What DSPy is and when to use it
DSPy stands for Declarative Self-improving Python. Instead of writing brittle prompts by hand, you write compositional Python code that describes what each step should do, and DSPy provides algorithms for optimizing the prompts and model weights behind those steps.
The README says it applies to simple classifiers, RAG pipelines, and agent loops.
- Python developers building LM-based pipelines such as RAG or multi-step agents
- Teams that want to improve prompts with measurable optimization instead of manual tweaking
- Researchers experimenting with prompt and weight optimization methods
- People who already have a way to measure output quality and want to tune against it
Install DSPy with pip
DSPy is installed from PyPI with pip. You need Python. The README gives this command for the released package.
To try the latest code from the main branch, the README gives a second command that installs directly from the GitHub repository.
pip install dspyUnderstand the main idea: program, then optimize
You build a system as Python modules. The framework's job is to let you iterate quickly on the structure, and its optimizers can then tune the prompts and weights used by each module.
Optimization needs a way to judge output quality. If you have no metric for your task, the optimizers have nothing to tune against, so DSPy fits best when you can measure results.
Check the LM setup before you run code
The README notes that the current LM integration uses bundled native lm15 engines, with no LiteLLM runtime dependency or fallback. It also points to an LM migration guide for breaking changes to provider options, custom LMs, text completions, and embeddings.
If you are upgrading an older project, read that guide before changing your code.
Where to find full documentation and help
The README is short on usage details. It points to the official DSPy Docs at dspy.ai for the framework itself, and to the project's Discord server for community help and contributions.
The README only shows Python code and pip installation, so plan to learn the API from the documentation.
Know when DSPy is not the right tool
DSPy is a programming framework, not a no-code tool, and it adds structure that is unnecessary for simple use. If you only need a one-off chat prompt, a plain prompt is simpler.
Non-Python users are not the target audience, since the README only covers Python.
- One-off chat prompts
- No-code workflows
- Non-Python projects
What people use it for
- Building a RAG pipeline that retrieves documents and answers questions from them
- Creating a simple classifier whose prompts are tuned against labelled examples
- Running multi-step agent loops where each step is a separate Python module
- Experimenting with prompt and weight optimization methods as a researcher
Frequently asked questions
How do I install DSPy?
Run pip install dspy. The README gives this as the install method from PyPI. To try the latest code from main, it gives a separate pip command that installs from the GitHub repository.
What do I need to run DSPy?
The README lists Python as the prerequisite and installs the package with pip. It does not list other requirements, so check the official docs at dspy.ai for details.
Is DSPy free to use?
DSPy is released under the MIT licence, which is a permissive open-source licence. Check the licence file in the repository for the full terms.
What is DSPy used for?
According to the README, DSPy is used for building simple classifiers, RAG pipelines, and agent loops. It lets you write modular Python code and optimize the prompts and weights behind each step.
What are the alternatives to DSPy?
Projects comparable to DSPy include LangChain, LlamaIndex, Guidance, and Outlines. Pick based on whether you need optimization, retrieval tooling, or constrained output.
Does DSPy work without Python?
The README only shows Python code and pip installation, so DSPy is intended for Python users. It is not a no-code tool.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
DSPy suits Python developers who build LM pipelines and can measure output quality, because its value is in optimizing against that measure. If you only need a single chat prompt, or you want a no-code tool, pick something simpler.
Install options & downloads