The Problem With Manual Prompting
Every time you change your underlying model, dataset, or pipeline, your hand-crafted prompts break. Prompt engineering is a fragile process: you tune prompts for GPT-4o and they degrade on Claude. You add a retrieval step and your answer extraction prompt needs rewriting.
DSPy (Declarative Self-improving Python) from Stanford NLP solves this by separating what you want from how to prompt for it. You write signatures — typed input/output specifications — and DSPy's optimizers find the prompts and few-shot examples automatically.
Signatures Instead of Prompt Strings
A DSPy signature declares the transformation you want:
import dspy
class SentimentClassifier(dspy.Signature):
"""Classify the sentiment of a product review."""
review: str = dspy.InputField()
sentiment: Literal["positive", "negative", "neutral"] = dspy.OutputField()
confidence: float = dspy.OutputField(desc="confidence score between 0 and 1")
No prompt string. DSPy generates (and optimizes) the actual prompt internally.
Built-in Modules
DSPy provides composable modules for common reasoning patterns:
# Simple prediction
classify = dspy.Predict(SentimentClassifier)
# Chain-of-thought reasoning
classify_cot = dspy.ChainOfThought(SentimentClassifier)
# ReAct agent with tools
agent = dspy.ReAct(SentimentClassifier, tools=[search_tool, calculator])
Optimizers
This is where DSPy diverges most from LangChain. Instead of manually writing few-shot examples, you give DSPy a small labeled dataset and it finds the best examples automatically:
from dspy.teleprompt import BootstrapFewShot
optimizer = BootstrapFewShot(metric=exact_match_metric, max_bootstrapped_demos=4)
optimized_classify = optimizer.compile(classify_cot, trainset=trainset)
MIPRO (Multi-prompt Instruction PRoposal Optimizer) goes further, generating and scoring candidate instruction texts in addition to few-shot examples.
Compiled Prompts
After optimization, DSPy produces a "compiled" program. The compiled state is a JSON file you can save and reload — it contains the optimized instructions and few-shot examples discovered during compilation. Switching models means recompiling, not rewriting prompts by hand.
DSPy vs LangChain
LangChain gives you tools to build LLM pipelines. DSPy gives you tools to optimize them. The two aren't mutually exclusive: some teams use LangChain for orchestration and DSPy for prompt optimization within individual steps. For research and evaluation-driven workflows, DSPy's automatic optimization is a significant advantage.