All Tools
DSPy logo

DSPy

DSPy is Stanford's open-source Python framework that replaces brittle prompt engineering with compiled, optimised LLM programs evaluated against measurable objectives.

What is DSPy?

DSPy is an open-source Python framework from Hazy Research at Stanford for building language model pipelines as structured programs rather than hand-crafted prompts. Instead of writing and maintaining prompt text manually, you define what data flows in and out of each step — then an optimizer automatically finds the instructions and examples that make your pipeline perform best against a metric you choose. DSPy is used in production by AI engineering teams at companies including Cursor, Mistral, and Databricks, and has over 25,000 GitHub stars.

How DSPy works

The core idea in DSPy is to separate what you want from how you ask for it. Traditional prompt engineering couples both together: you write a prompt that simultaneously specifies the task and the wording that gets the model to perform it. DSPy separates them through three abstractions.

Signatures define the task interface. A signature is a typed declaration of inputs and outputs, like question -> answer or context, question -> reasoning, answer. You write no prompt text. DSPy reads the field names and types and infers the task from them.

Modules are the building blocks of pipelines. Each module encapsulates one reasoning strategy: dspy.Predict calls the LLM directly, dspy.ChainOfThought asks the model to reason step by step before answering, and dspy.ReAct lets the model call external tools and iterate. You compose modules into programs using standard Python control flow — loops, conditionals, function calls. Nothing domain-specific required.

Optimizers (formerly called teleprompters) are where DSPy diverges from every other LLM framework. Once you have a program and a metric function that scores outputs, you run an optimizer over a small labelled dataset. The optimizer tries different instruction phrasings and few-shot example sets, evaluates each variant against your metric, and returns a compiled version of your program with the best-performing configuration baked in. Common optimizers include BootstrapFewShot (selects high-scoring examples automatically) and MIPRO (uses a separate LLM to propose and refine instruction candidates). The compiled program runs without any further optimization overhead — it is just a Python object with better prompts inside.

This compile-then-run model means your pipeline improves as your dataset grows, without manual prompt rewrites. It also means switching models is safer: you recompile against the new model rather than hoping your hand-tuned prompts still work.

What you can build with DSPy

Retrieval-augmented generation (RAG) pipelines that actually get evaluated. Build a Retrieve-then-ChainOfThought pipeline over your document store, define a correctness metric, and let DSPy optimize the retrieval query and the reasoning prompt together against real questions. Most teams find a meaningful accuracy lift over their hand-written baseline after the first optimization run.

Structured extraction systems for documents, emails, or support tickets. Define a signature with typed output fields (account number, issue category, sentiment) and let DSPy find the prompting strategy that maximises field-level accuracy on your labelled examples. More reliable than prompt-engineering extraction manually and easier to re-optimize when the document format changes.

Multi-hop reasoning agents that break complex questions into sub-queries. Use dspy.ReAct to give the model access to a search tool, then optimize the agent’s search strategy against an answer accuracy metric. DSPy handles the loop between tool calls and reasoning without custom orchestration code.

Classification pipelines for content moderation, ticket routing, or lead scoring. Define a classify signature with your label set, write a scoring function, and run an optimizer over your training examples. Because DSPy treats the model and the prompt as co-optimizable, you often get better performance than fine-tuning on small datasets.

Evaluation-driven chatbots for customer support or internal knowledge retrieval. Build a full conversation pipeline with a DSPy program, instrument it with a quality metric (faithfulness, relevance, or task completion), and run optimization weekly as your product evolves. Your bot gets systematically better rather than drifting as models update.

Is DSPy free?

DSPy is fully open-source under the Apache 2.0 license. There is no commercial version, no managed service, and no usage fee. The only costs associated with DSPy come from the LLM API calls your programs make, which are billed directly by your model provider.

How to get started with DSPy

Install with pip install dspy. Configure your LLM provider with dspy.configure(lm=dspy.LM("openai/gpt-4o")) or any supported model string. Write a Signature class defining your task’s inputs and outputs, wrap it in a Module, write a metric function, run a DSPy optimizer over a small training set, and save the compiled result. The DSPy documentation includes a complete quick-start tutorial that walks through a RAG pipeline from scratch in under an hour.

Learning DSPy — along with LangGraph, CrewAI, and the rest of the agentic AI stack — is the core curriculum of the BuildrLabs Agentic AI Bootcamp. If you want to build production AI systems rather than piece things together from tutorials, that’s the faster path.

Key Features

  • Typed Signatures that declare task inputs and outputs without writing prompt text
  • Modules that wrap LLM calls into reusable, composable units (ChainOfThought, ReAct, Predict)
  • Optimizers that automatically find better prompts and few-shot examples against your metric
  • Model-agnostic: works with OpenAI, Anthropic, Gemini, Mistral, Ollama, and any LiteLLM-supported provider
  • Built-in retrievers for RAG with Pinecone, Weaviate, Chroma, Qdrant, and other vector stores
  • Evaluation framework for scoring and comparing pipeline variants programmatically

FAQ

Is DSPy free to use? +

Yes. DSPy is fully open-source under the Apache 2.0 license. You can use it in production, commercial projects, and private forks without paying anything. The only costs you incur are the LLM API calls you make — DSPy itself has no licensing fee and no hosted tier.

How is DSPy different from LangChain? +

LangChain focuses on composing LLM calls into chains and providing integrations with dozens of external services. DSPy focuses on automatically optimising those calls against a metric you define. With LangChain, you still write and maintain prompt text manually. With DSPy, you write a signature (what goes in, what comes out) and let an optimizer find the best way to achieve it. The two frameworks are not mutually exclusive — some teams use both.

What kinds of teams use DSPy? +

DSPy is used primarily by AI engineers and ML researchers who build pipelines where output quality matters and needs to be measured. Production teams at Cursor, Mistral, and Databricks have used it for systematic prompt engineering at scale. It is well suited to teams who run evaluations regularly and want to improve model performance without manually rewriting prompts every time a model is updated.

Explore Similar AI Tools

Newsletter

The Twice-Monthly AI Briefing

Updates from the AI world — what shipped, what we’re using in production, and what’s worth your attention. Two emails a month, no spam.