DSPy

DSPy

Program LLMs instead of prompting

Description

Anyone who has shipped an LLM feature knows the drill: you finally get a prompt stable, switch models, and it falls apart. Chain three or four steps together and every change means hand-tuning each prompt again, with no one sure which sentence is doing the work. DSPy turns this back into programming. You declare what goes in and what comes out as a Python Signature, compose them like functions, and let the framework write and optimize the prompts.

The real payoff is the optimizers. Give them a small set of examples and a metric and they search for better instructions and demonstrations, or even fine-tune weights, so the same program re-tunes itself when you swap models. It comes from Stanford NLP and runs in production at Databricks, Shopify and Dropbox.

Features



Declarative signatures: Write a class with InputField and OutputField, for example pulling an event name and date out of an email, and get back a Prediction object with those fields. No parsing code.

Composable modules: Predict, ChainOfThought, ReAct and friends snap together into RAG pipelines, classifiers or agent loops, and each piece can be swapped and tested on its own.

Automatic prompt optimization: Built-in optimizers such as GEPA and MIPRO rewrite instructions and pick demonstrations against your metric, instead of trial and error by feel.

Weight fine-tuning: The same program can optimize its prompts or distill into a smaller model's weights, and the two work better together.

One-line model swaps: Change the string in dspy.LM("openai/...") to move to another provider or a local model without touching the rest of the code.

Tools and MCP: Hand functions and MCP tools to a ReAct agent, with a built-in Python interpreter for tasks that need actual computation.

Observability: MLflow tracing records every call, so you can see which step in a pipeline went wrong.