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 at Pristren

Mahmudul Haque Qudrati

CEO & ML Engineer

March 22, 2026
8 min read
DSPy: Stop Prompting, Start Programming - Declarative LLM Pipelines

The Problem With Manual Prompting

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:

python
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.

Start free

Built-in Modules

DSPy provides composable modules for common reasoning patterns:

python
# 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:

python
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

#dspy#stanford#declarative#optimization#prompting

// discussion

Comments

0/4000
Mahmudul Haque Qudrati — CEO & ML Engineer at Pristren

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.

PristrenZlyqor