Chapter 1

DSPy Fundamentals

Welcome to your DSPy journey! This chapter introduces you to DSPy and gets you started building your first LM-powered applications.

6 Sections ~3-4 hours ⭐ Beginner

πŸš€ Welcome to DSPy

Your journey from prompt engineering to prompt programming starts here

What You'll Learn

By the end of this chapter, you will:

βœ… Understand what DSPy is and why it matters
βœ… Grasp the paradigm shift from prompting to programming
βœ… Install and configure DSPy in your development environment
βœ… Write and run your first DSPy program
βœ… Configure and work with different language models
βœ… Build simple question-answering applications

Chapter Overview

This chapter covers the essential foundations you need to start building with DSPy:

πŸ“–

What is DSPy?

Learn what DSPy is, why it was created, and how it differs from traditional prompt engineering approaches.

πŸ”„

Programming vs. Prompting

Understand the fundamental paradigm shift from manual prompt engineering to programmatic LM pipelines.

πŸ’»

Your First DSPy Program

Write and run a complete DSPy application from scratch.

πŸ€–

Language Models

Learn how to configure and work with different LM providers (OpenAI, Anthropic, local models).

πŸ‹οΈ

Exercises

Practice what you've learned with hands-on exercises.

What Makes DSPy Different?

Before diving in, here's a quick preview of what makes DSPy special:

❌ Traditional Prompting

# Manual prompt engineering
prompt = """
You are a helpful assistant. Answer the question clearly.

Question: What is the capital of France?
Answer:
"""

response = openai.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": prompt}]
)
⚠️ Brittle and hard to maintain ⚠️ Doesn't compose well ⚠️ Manual tuning required ⚠️ No systematic optimization

βœ… DSPy Approach

import dspy

# Define the task signature
class QuestionAnswer(dspy.Signature):
    """Answer questions clearly."""
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

# Use it with automatic prompting
qa = dspy.Predict(QuestionAnswer)
response = qa(question="What is the capital of France?")
βœ… Clean, modular code βœ… Easy to compose and reuse βœ… Automatically optimizable βœ… Systematic improvement

Prerequisites

Before starting this chapter, ensure you have:

βœ… Python 3.9+ installed
βœ… Basic Python knowledge (functions, classes, imports)
βœ… Virtual environment set up (from setup instructions)
βœ… API key for at least one LM provider
βœ… Text editor or IDE ready to use
πŸ’‘

Need help with prerequisites? Review Chapter 0: Prerequisites

Learning Approach

This chapter uses a hands-on approach:

πŸ“š

Concepts

Clear explanations of core ideas

πŸ’»

Examples

Working code you can run

πŸ‹οΈ

Practice

Exercises to reinforce learning

πŸ”¬

Experimentation

Encouragement to modify and explore

πŸ’‘

Pro Tip: Don't just readβ€”run every example and complete the exercises!

Estimated Time

πŸ“– Reading 1-1.5 hours
πŸ’» Running examples 1 hour
πŸ‹οΈ Exercises 1-1.5 hours
⏱️ Total 3-4 hours

Feel free to spread this over multiple sessions!

Key Takeaways (Preview)

By the end of this chapter, you'll understand:

DSPy is a framework for programming (not prompting) LM-based applications

Signatures define tasks declaratively using input/output specifications

Modules are composable building blocks that can be optimized automatically

LMs are configurable and DSPy works with multiple providers

Programming > Prompting for building robust, maintainable applications

πŸš€

Let's Begin!

Ready to learn DSPy? Start with understanding what DSPy is and why it matters.

Remember: Learning is a journey. Take your time, experiment freely, and don't hesitate to ask questions!