How DSPy Works

Prompt engineering is manual tuning — you adjust words until the output looks right. DSPy treats it as an optimization problem: define what you want (signatures), provide examples, and let the framework find the prompts and few-shot examples that actually work.

33,164 stars Python 10 components 6-stage pipeline

What dspy Does

Framework for programming language models using structured Python code instead of prompts

DSPy allows developers to build AI systems using declarative Python code with typed signatures and modules, then automatically optimize their prompts and weights. It provides abstractions for composing LM calls into pipelines and algorithms for improving their performance.

Architecture Overview

dspy is organized into 5 layers, with 10 components and 21 connections between them.

Signatures & Types
Type-safe interfaces defining input/output contracts with custom types like Image, Audio, Tool
Adapters
Interface layer between DSPy and language models, handling format conversion and parsing
Modules
Composable building blocks like Predict, ChainOfThought, ReAct for creating AI programs
Optimizers
Algorithms for automatically improving prompts and weights through teleprompt techniques
Clients & Infrastructure
LM clients, caching, evaluation, and utility functions for the framework

How Data Flows Through dspy

User creates signatures and modules, which get processed by adapters into LM calls, then parsed back into structured outputs. Optimizers can improve this pipeline automatically.

1Signature Definition

User defines typed input/output interface with field descriptions

2Module Creation

Predict, ChainOfThought, or custom modules use signatures to define behavior

3Adapter Processing

Adapter converts signature+inputs into LM-specific format (chat messages, JSON schema)

4LM Execution

Language model processes formatted input and returns response

5Response Parsing

Adapter parses LM response back into structured signature outputs

6Optimization

Teleprompt optimizers can improve prompts and demonstrations automatically

System Dynamics

Beyond the pipeline, dspy has runtime behaviors that shape how it responds to load, failures, and configuration changes.

Data Pools

Pool

DSPY_CACHE

Disk-based cache for LM responses to avoid redundant API calls

Type: cache

Pool

settings

Global configuration state for LM, adapter, and framework settings

Type: state-store

Feedback Loops

Loop

Teleprompt Optimization

Trigger: User calls optimizer with training data → Generate candidates, evaluate performance, update prompts/weights (exits when: Convergence criteria met or max iterations)

Type: training-loop

Loop

Adapter Fallback

Trigger: ChatAdapter parsing fails → Fall back to JSONAdapter with same inputs (exits when: Successful parse or both adapters fail)

Type: retry

Loop

LM Retry Logic

Trigger: LM call fails or times out → Retry with exponential backoff via tenacity (exits when: Success or max retries exceeded)

Type: retry

Control Points

Control

settings.configure

Control

Adapter selection

Control

Cache enabled

Control

Log level

Delays

Delay

LM API Calls

Duration: varies by model

Delay

Cache Lookups

Duration: until cache expires

Delay

Optimization Iterations

Duration: minutes to hours

Technology Choices

dspy is built with 8 key technologies. Each serves a specific role in the system.

litellm
Unified LM client interface
pydantic
Type validation and structured data
openai
OpenAI API client
json_repair
Robust JSON parsing for LM outputs
tenacity
Retry logic for LM calls
diskcache
Persistent caching layer
pytest
Testing framework
asyncer
Async/await utilities

Key Components

Who Should Read This

ML researchers and engineers who want to move beyond manual prompt engineering, or teams building complex LLM pipelines.

This analysis was generated by CodeSea from the stanfordnlp/dspy source code. For the full interactive visualization — including pipeline graph, architecture diagram, and system behavior map — see the complete analysis.

Explore Further

Frequently Asked Questions

What is dspy?

Framework for programming language models using structured Python code instead of prompts

How does dspy's pipeline work?

dspy processes data through 6 stages: Signature Definition, Module Creation, Adapter Processing, LM Execution, Response Parsing, and more. User creates signatures and modules, which get processed by adapters into LM calls, then parsed back into structured outputs. Optimizers can improve this pipeline automatically.

What tech stack does dspy use?

dspy is built with litellm (Unified LM client interface), pydantic (Type validation and structured data), openai (OpenAI API client), json_repair (Robust JSON parsing for LM outputs), tenacity (Retry logic for LM calls), and 3 more technologies.

How does dspy handle errors and scaling?

dspy uses 3 feedback loops, 4 control points, 2 data pools to manage its runtime behavior. These mechanisms handle error recovery, load distribution, and configuration changes.

How does dspy compare to langchain?

CodeSea has detailed side-by-side architecture comparisons of dspy with langchain, llama_index, guidance. These cover tech stack differences, pipeline design, and system behavior.

Visualize dspy yourself

See the interactive pipeline graph, architecture diagram, and system behavior map.

See Full Analysis