Leanpub Header

Skip to main content

DSPy in Depth

Programming Language Models from Zero to Production

This book is 100% completeLast updated on 2026-07-23

Learn to build production-ready LLM applications with DSPy through hands-on tutorials, complete runnable examples, and real-world projects. Master DSPy's core abstractions and create AI systems that improve with data instead of endless prompt tweaking.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
118
Pages
About

About

About the Book

This book teaches you to build production-grade LLM applications using DSPy, the declarative programming framework from Stanford NLP. Through detailed tutorials, complete runnable code, and real-world projects, you will master DSPy's three core abstractions: signatures, modules, and optimizers. By the end, you will be able to design, build, optimize, evaluate, and deploy AI systems that improve with data rather than trial-and-error prompt iteration. No prior DSPy experience is required, but basic Python proficiency and familiarity with how LLM API calls work will help you get the most out of this book.

Bundle

Bundles that include this book

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Programming Language Models from Zero to Production

Introduction: The End of Prompt Engineering as We Know It

Chapter 1: What Is DSPy and Why It Matters

  1. The Prompting Crisis
  2. From Prompting to Programming
  3. The DSPy Philosophy
  4. How DSPy Fits in the Ecosystem

Chapter 2: Installation and Your First Program

  1. Installing DSPy
  2. Configuring Your Language Model
  3. Your First Signature and Prediction
  4. Understanding the Output
  5. Switching Between Models
  6. A Complete First Program
  7. Migration Notes from Older Versions

Chapter 3: Signatures in Depth

  1. String-Based Signatures
  2. Class-Based Signatures
  3. Input Fields and Output Fields
  4. Type Annotations and Pydantic Integration
  5. Signature Mutation and Composition
  6. Signature Introspection
  7. Designing Effective Signatures

Chapter 4: Core Modules

  1. dspy.Predict: Basic Prediction
  2. dspy.ChainOfThought: Step-by-Step Reasoning
  3. dspy.ProgramOfThought: Code-Assisted Reasoning
  4. dspy.MultiChainComparison: Comparing Outputs
  5. dspy.BestOfN and dspy.Refine: Output Refinement
  6. dspy.Parallel: Concurrent Execution
  7. Choosing the Right Module

Chapter 5: Composing Custom Modules

  1. The dspy.Module Base Class
  2. Building Your First Compound Module
  3. Forward Method Design Patterns
  4. Nested Modules and Sub-modules
  5. Module Inspection and Introspection
  6. Deep Copy and State Management

Chapter 6: Language Models and Configuration

  1. The dspy.LM Interface
  2. OpenAI, Anthropic, and Google Providers
  3. Local Models and OpenAI-Compatible Endpoints
  4. Context Management with dspy.context()
  5. Caching and Performance
  6. Tracking Usage and Costs
  7. Adapter Selection

Chapter 7: Evaluation and Metrics

  1. Writing Metric Functions
  2. The dspy.Evaluate Class
  3. Built-in Metrics
  4. LLM-as-a-Judge Evaluation
  5. Debugging with inspect_history()
  6. Composite Metrics
  7. Evaluation Best Practices

Chapter 8: Optimizers (Teleprompters)

  1. The Optimization Paradigm
  2. BootstrapFewShot Family
  3. COPRO: Instruction Optimization
  4. MIPROv2: Joint Instruction and Demo Search
  5. GEPA: Reflective Prompt Evolution
  6. SIMBA and Other Specialized Optimizers
  7. Choosing the Right Optimizer
  8. Saving and Loading Compiled Programs

Chapter 9: Retrieval-Augmented Generation (RAG)

  1. The dspy.Retrieve Module
  2. ColBERTv2 Integration
  3. Building a Basic RAG Pipeline
  4. Multi-Hop RAG
  5. Optimizing RAG Programs
  6. Custom Retriever Integration

Chapter 10: Agents and Tool Use

  1. The ReAct Paradigm
  2. dspy.ReAct Module
  3. Defining Tools as Functions
  4. Building Production Agents
  5. Advanced Tool Patterns

Chapter 11: Structured Outputs and Type Safety

  1. Typed Predictors
  2. Pydantic Models in Signatures
  3. JSON Schema Generation
  4. Output Validation Strategies
  5. Complex Nested Structures

Chapter 12: Advanced Patterns and Workflows

  1. Assertions and Constraints (Refine Module)
  2. Multimodal Programs with Images and Audio
  3. Conversation History Management
  4. Async and Streaming Support
  5. MCP Integration
  6. Caching Strategies for Production

Chapter 13: Production Deployment

  1. Saving and Loading Optimized Programs
  2. FastAPI Deployment
  3. MLflow Integration
  4. Caching Strategies
  5. Monitoring and Observability

Chapter 14: Performance Optimization and Best Practices

  1. Cost Optimization
  2. Latency Reduction
  3. Error Handling Patterns
  4. Testing DSPy Programs
  5. Common Pitfalls and Anti-Patterns

Chapter 15: Real-World Projects

  1. Project 1: Intelligent Customer Service Agent
  2. Project 2: Multi-Document Research Assistant
  3. Project 3: Automated Code Review System

Conclusion: The Future of LLM Programming

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub