DSPy: Stop Prompting, Start Programming - Declarative LLM Pipelines
DSPy from Stanford replaces hand-written prompts with typed signatures and optimizers that automatically find the best prompts and few-shot examples for your task.
Mahmudul Haque Qudrati
CEO & ML Engineer
One AI engineering post, weekly
LLM benchmarks, prompt techniques, and token-cost breakdowns — not another AI news roundup.
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.
Team workspace
Ship faster with chat, meetings, and projects in one place — Zlyqor.
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.
Resources

Mahmudul Haque Qudrati
CEO & ML Engineer
Visionary technologist, software engineer, and machine learning specialist. Founder and CEO of Pristren, directing engineering teams that ship production-grade AI/ML pipelines, mission-critical full-stack applications, and developer tooling. Creator of Zlyqor, the unified team workspace platform. Author of 540+ technical guides and benchmark research reports on large language models, agentic workflows, Model Context Protocol (MCP), and modern web stacks.
More from Mahmudul
Related Articles
How to Use Claude to Make Videos Like Vox and Others
Claude can help you make Vox-style videos by generating scripts, editing with code, and automating animation. Here's a practical guide with real workflows and costs.
OpenAI Ends Cursor Model Access on Nov 12, 2026: What Developers Need to Know
OpenAI will terminate Cursor's access to its models on November 12, 2026, following SpaceX's acquisition. This guide explains the timeline, why it happened, and practical steps to migrate your workflow.
I Used Claude Code to Get a Second Opinion on My MRI: A Practical Overview
A developer fed his MRI scan to Claude Code and got a second opinion. Here's how the experiment worked, what it cost, and why you shouldn't rely on it for medical decisions.
// discussion
Comments