Home Knowledge Base DSPy

DSPy is a Stanford-developed framework that treats LLM prompt engineering as a compilation problem — automatically optimizing prompts and few-shot examples by defining the task as a program with measurable metrics — replacing hand-crafted prompt strings with declarative signatures and learnable modules that the DSPy compiler tunes end-to-end for maximum task performance.

What Is DSPy?

Why DSPy Matters

DSPy Core Modules

Predict:

ChainOfThought:

ReAct:

MultiChainComparison:

DSPy Optimizers

BootstrapFewShot:

MIPRO (Multi-prompt Instruction Proposal and Refinement Optimizer):

Example DSPy Program

import dspy

class RAGPipeline(dspy.Module):
    def __init__(self):
        self.retrieve = dspy.Retrieve(k=3)
        self.generate = dspy.ChainOfThought("context, question -> answer")

    def forward(self, question):
        context = self.retrieve(question).passages
        return self.generate(context=context, question=question)

# Compile with optimizer
optimizer = dspy.BootstrapFewShot(metric=exact_match)
compiled = optimizer.compile(RAGPipeline(), trainset=train_examples)

DSPy vs Traditional Prompt Engineering vs LangChain

AspectDSPyHand-crafted promptsLangChain
Prompt authoringAutomatedManualManual
Cross-model portabilityExcellentPoorModerate
Metric-driven optimizationNativeNoneNone
Learning curveSteepLowMedium
Research backingStanford NLPN/ACommunity
Production adoptionGrowingWidespreadVery wide

DSPy is the framework that makes LLM application development as rigorous as machine learning model development — by replacing fragile hand-crafted prompts with compiled, metric-optimized programs, DSPy enables teams to build LLM applications that reliably improve as data and compute scale, rather than degrading whenever the underlying model or task distribution shifts.

dspyprogrammingoptimize

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.