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.
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.
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:
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.
Practical deep-dives on LLMs, developer tools, and AI engineering. No filler. Unsubscribe any time.
// written byFIG. AUTH-01
530
Mahmudul Haque Qudrati
CEO & ML Engineer
CEO and ML Engineer at Pristren. Builds AI-powered software for teams and writes about machine learning, LLMs, developer tools, and practical AI applications.
Open Code Review – An AI-powered code review CLI tool: A Practical Overview
Open Code Review is an open-source CLI tool from Alibaba that uses AI to review code changes. It runs locally, supports multiple LLMs, and costs about $0.01 per review. Here's a practical breakdown.