Introduction to DSPy Framework
Why DSPy replaces hand-written prompts with declarative signatures.
Introduction to DSPy Framework is a free AI Prompt Engineering lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Fragility of Hand-Written Prompts
Hand-crafted prompts are brittle. A single word change can shift model behavior dramatically, and what works for GPT-4 often breaks on Claude or Gemini.
Prompt engineers spend hours tweaking wording, only to find the changes don't generalize across datasets or model versions. This fragility is the core problem DSPy solves.
What DSPy Is
DSPy (Declarative Self-improving Python) is a framework from Stanford that treats prompting as a compilation problem rather than a craft problem.
Instead of writing: "Think step by step and answer the following question...", you declare what you want (a signature) and let DSPy figure out how to prompt the model to achieve it.
Installing DSPy
DSPy is available on PyPI. Install it and configure a language model backend to get started.
pip install dspy-ai
# Basic setup in Python
import dspy
# Configure the language model
lm = dspy.LM('openai/gpt-4o-mini', api_key='sk-...')
dspy.configure(lm=lm)
print('DSPy configured successfully')Core Idea: Separate Logic from Prompting
In traditional prompting, the program logic and the prompt text are tangled together. If you want to change how the model reasons, you rewrite the prompt string.
DSPy separates these concerns: you write Python logic using modules, and DSPy generates (and optimizes) the actual prompt text automatically during a compilation step.
Signatures: Declaring Intent
A signature in DSPy is a typed declaration of inputs and outputs. It's the what, not the how.
DSPy uses the signature to construct an appropriate prompt at compile time — no prompt engineering required by you.
import dspy
# Declare: given a question, produce an answer
class SimpleQA(dspy.Signature):
"""Answer questions with short factual responses."""
question: str = dspy.InputField()
answer: str = dspy.OutputField(desc='A concise factual answer')
# DSPy turns this declaration into a real prompt automatically
print(SimpleQA.__doc__)Modules: Composable Reasoning Blocks
DSPy provides modules that implement common reasoning patterns. You use them like PyTorch layers — compose them into larger programs.
dspy.Predict— direct predictiondspy.ChainOfThought— adds reasoning stepsdspy.ReAct— reasoning + tool use
import dspy
class SimpleQA(dspy.Signature):
"""Answer questions with short factual responses."""
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Three different module strategies for the same signature
predict_module = dspy.Predict(SimpleQA)
cot_module = dspy.ChainOfThought(SimpleQA)
# Call just like a function
result = predict_module(question='What is the capital of France?')
print(result.answer)What Compilation Does
Compilation is the process where DSPy optimizes your program's prompts using a training set and a metric function.
Before compilation: DSPy uses a basic default prompt. After compilation: DSPy has found few-shot examples, better instructions, or chain-of-thought demonstrations that maximize your metric. The optimized prompts are saved and reused at inference time.
A Minimal DSPy Program
Here is a complete minimal DSPy program: a signature, a module, and a call. No prompt string is written anywhere.
import dspy
# 1. Configure LM
lm = dspy.LM('openai/gpt-4o-mini', api_key='sk-...')
dspy.configure(lm=lm)
# 2. Define signature
class Summarize(dspy.Signature):
"""Summarize text into one sentence."""
text: str = dspy.InputField()
summary: str = dspy.OutputField()
# 3. Create module
summarizer = dspy.Predict(Summarize)
# 4. Use it
result = summarizer(text='DSPy is a framework for programming language models.')
print(result.summary)DSPy vs Traditional Prompt Engineering
The key philosophical difference:
- Traditional: You write prompt text → model executes it → you tweak text manually
- DSPy: You write Python logic → optimizer finds the best prompt text → model executes optimized prompt
DSPy treats prompts as implementation details that a compiler should handle, just like a compiler turns Python into bytecode.
When DSPy Is Worth It
DSPy shines when you have:
- A clear metric to optimize (accuracy, F1, user rating)
- A training set of examples (even 20-50 suffice)
- A pipeline with multiple reasoning steps that interact
For one-off simple prompts, DSPy's overhead isn't worthwhile. For production LLM pipelines with measurable goals, it's transformative.
DSPy Ecosystem Overview
DSPy is not just an API wrapper — it's a full ecosystem:
- Signatures: Declare I/O
- Modules: Reasoning strategies
- Optimizers: BootstrapFewShot, MIPRO, COPRO
- Evaluate: Built-in evaluation harness
- Assertions: Constrain outputs declaratively
You'll explore each of these in the following lessons of this course.
Knowledge Check: DSPy Core Concept
What is the primary way DSPy differs from writing prompts manually?
Recap: DSPy Framework Foundations
DSPy addresses the fragility of hand-written prompts by treating prompting as a compilation problem. You write signatures (typed I/O declarations) and compose them with modules like Predict and ChainOfThought. An optimizer then finds the best prompt text automatically using your training data and metric. The result is a prompt pipeline that is portable, testable, and automatically improvable — without manual prompt engineering.
Frequently asked questions
Is the “Introduction to DSPy Framework” lesson free?
Yes — the full text of “Introduction to DSPy Framework” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to DSPy Framework”?
Why DSPy replaces hand-written prompts with declarative signatures. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to DSPy Framework” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to DSPy Framework
- Defining Signatures and Modules
- Compiling and Optimizing Prompts
- Evaluating DSPy Pipelines