Build AI Applications with Python and DSPy

Transform from AI beginner to production-ready developer through a structured, hands-on learning path that covers the complete AI application development lifecycle.

You'll master DSPy fundamentals with type-safe signatures and Pydantic models that eliminate prompt fragility, build sophisticated reasoning systems using ChainOfThought, ReAct, and ProgramOfThought modules, and systematically test and evaluate realiability using custom metrics and LLM-as-a-judge techniques.

Learn to automatically optimize your AI systems using advanced techniques like MIPROv2 and GEPA that boost accuracy by 20-50%, integrate external tools and APIs through the Model Context Protocol (MCP), and build production-grade RAG applications with vector databases, embeddings, and intelligent retrieval strategies.

Gain real-world experience through two comprehensive capstone projects—RepoRank (GitHub analyzer), CRM Auto Reply (customer support system) while mastering MLflow observability, FastAPI deployment, Docker containerization, and autonomous agent architectures for scalable production systems.

11 Chapters, 4 Appendix, 210+ plus page online book. 3 Chapters Available.
Digital access to this title, all updates for this edition.
Available for Pre-booking pricing of 29$.
Loading...
Building AI Applications using Python and DSPy

Table of Contents*

  • Setting Up Your Environment
  • • Virtual Environment and Packages
  • • Ollama Setup
  • • Testing your setup
  • • Google Gemini
  • • Claude (Anthropic)
  • • OpenAI
  • Prompting
  • • What Makes a Prompt?
  • • Prompt Breakdown Example
  • • DSPy Signature
  • Signatures Deep Dive
  • • Constraining Output to Predefined Values
  • • Multi Field Signature with varying types
  • • Custom data types with pydantic
  • What's Next?

Chapter 2: Core DSPy Modules

  • Exploring Predict, ChainOfThought, ReAct
  • Introduction to DSPy Modules
  • • Understanding the Module Base Class
  • • The Module Lifecycle
  • dspy.Predict: The Foundational Predictor
  • • Basic Usage and Signatures
  • • Supplying Few-shot Demonstrations
  • dspy.ChainOfThought
  • • How it works: Generating a Rationale
  • • Implementing CoT for Complex Reasoning
  • dspy.ProgramOfThought
  • • When to Use Code-based Reasoning
  • • Implementing PoT for Math Problems
  • dspy.ReAct: Combining Reasoning and Action
  • • The ReAct Framework
  • • Building a Simple ReAct Agent
  • dspy.CodeAct: Code-driven Action Selection
  • • Generating and Executing Code
  • • Safety Considerations and Sandboxing
  • Advanced Reasoning Modules
  • • dspy.MultiChainComparison
  • • dspy.BestOfN
  • • dspy.Refine
  • Parallel Processing Modules
  • Composing Modules: Building DSPy Programs

Chapter 3: Evaluating Your DSPy Programs

  • Metrics and Evals
  • Why Systematic Evaluation Matters
  • Metrics for LLM Systems
  • • Common Metrics: Exact Match, F1 Score
  • • Choosing the Right Metric
  • • Combining Multiple Metrics
  • The DSPy Evaluator
  • Custom Evaluation Functions
  • LLM-as-a-Judge Evaluation Patterns
  • • Why Use LLMs for Evaluation?
  • • Designing Judge Prompts and Signatures
  • • Scoring Scales: Binary, Likert, Continuous
  • • Chain-of-Thought Judging
  • • Pairwise Comparison
  • • Handling Judge Bias and Calibration
  • • Multi-Judge Consensus
  • Debugging Strategies for DSPy Programs
  • Analyzing Results and Debugging

Chapter 4: Capstone Project - RepoRank

  • Build an intelligent GitHub repository analyzer that discovers, evaluates, and ranks projects using DSPy modules, LLM-as-a-judge quality assessment, and multi-dimensional scoring to generate comprehensive reports.

Chapter 5: Model Context Protocol (MCP)

  • Extending DSPy with External Tools
  • What is MCP and Why It Matters
  • • LLMs Need Access to External Data
  • • MCP as a Standardized Tool Interface
  • • How MCP Complements DSPy
  • MCP Architecture Essentials
  • • Servers, Clients, and Transports
  • • Resources: Dynamic Data Sources
  • • Tools: Executable Functions
  • Consuming MCP Tools in DSPy Programs
  • • Setting Up an MCP Client
  • • Discovering Available Tools
  • • Wrapping MCP Tools as Python Functions
  • • Creating DSPy Signatures for Tool Calling
  • • Building a ReAct Module with MCP Tools
  • Building MCP Servers with DSPy
  • • Server Setup and Architecture
  • • Exposing Resources
  • • Implementing Tools with DSPy Modules
  • Patterns
  • • Error Handling and Fallback Strategies
  • • Performance Considerations and Caching

Chapter 6: The Secret Sauce

  • Optimization and Compilation
  • From Programming to Optimization
  • What is a DSPy Optimizer (Teleprompter)?
  • The Compilation Process Explained
  • When to Use Which Optimizer
  • Basic Few-Shot Optimizers
  • • LabeledFewShot
  • • BootstrapFewShot
  • Similarity-Based Optimizers
  • • KNN & KNNFewShot
  • • SIMBA
  • Search and Exploration Optimizers
  • • BootstrapFewShotWithRandomSearch
  • • COPRO: Self-Correction
  • • GEPA: Genetic Programming
  • • MIPROv2: Advanced Mining
  • Specialized Optimizers
  • • InferRules
  • • BootstrapFinetune
  • • BetterTogether
  • • Ensemble
  • A Practical Compilation Workflow

Chapter 7: Observability with MLflow

  • Tracking Your DSPy Programs
  • Why Observability Matters for DSPy
  • Getting Started with MLflow
  • • Setup and Configuration
  • • Core Components
  • Tracking Your First DSPy Program
  • Tracking Optimization Runs
  • Analyzing Experiments
  • Debugging with Traces and Logs
  • Model Registry and Versioning
  • Production Considerations

Chapter 8: Retrieval Augmented Generation

  • The Problem: Why LLMs Need External Knowledge
  • What is RAG?
  • Understanding Embeddings: Text to Vectors
  • • What Are Embeddings?
  • • How Embeddings Capture Meaning
  • • Embedding Models
  • Vector Similarity and Search
  • Introduction to Vector Databases
  • Learning Weaviate
  • • Installation and Setup
  • • Core Concepts
  • • Schema Design
  • Preparing Your Data for RAG
  • • Document Collection and Preprocessing
  • • Chunking Strategies
  • • Generating Embeddings
  • Querying Weaviate
  • DSPy's Retrieval Abstraction
  • Building Your First RAG Pipeline with DSPy
  • RAG Pipeline Patterns
  • Production Considerations for RAG

Chapter 9: Capstone Project - CRM Auto Reply

  • Build an intelligent customer support system that automatically generates contextual replies to CRM tickets using RAG-powered knowledge retrieval, DSPy optimization, and multi-source data integration for personalized customer interactions.

Chapter 10: Building AI Agents with DSPy

  • Part I: Agent Fundamentals
  • • What is an AI Agent?
  • • The Core Loop: Observe, Think, Act
  • • Agent Architecture Patterns
  • • Memory Systems for Agents
  • • Planning and Reasoning Strategies
  • • Tool Integration Patterns
  • • Agent Reliability and Safety
  • • Evaluation and Optimization
  • Part II: Building DSPyBot
  • • Use Case: Slack Bot for DSPy Help
  • • Understanding DSPyBot's Knowledge Domain
  • • Designing the Agent Architecture
  • • Building the Knowledge Base with RAG
  • • Implementing Agent Components
  • • Adding Intelligence and Reasoning
  • • Responding in Slack
  • • Advanced Features
  • • Error Handling and Edge Cases
  • • Testing and Evaluation
  • • Optimization and Deployment

Chapter 11: Scaling and Productionizing

  • DSPy Applications
  • From Prototype to Production
  • Managing LLM Providers in Production
  • Caching Strategies
  • Request and Response Optimization
  • Exposing a DSPy Program as an API
  • Compiled Program Management
  • Security and Safety in Production
  • Testing and Quality Assurance

Appendices

  • Appendix A: Quick Guide To Python
  • Appendix B: Essential AI Glossary & Papers
  • Appendix C: Book Glossary
  • Appendix D: Resources

*Since AI is a fast moving field the author reserves the right to tweak and extend the table of contents to include more chapters or change capstone projects to something more interesting.

Early Release Pricing

Don't Miss Out on Early Bird Pricing

Get instant access to all available chapters and secure your copy before the price increases to $49

$49
$29
Save $20
Instant Download
Free Updates
48-Hour Refund Policy
FREE

Get The Free Chapters

Get first chapter of DSPy that teaches you DSPy Signature. Also BONUS - get Appendix B - that covers all AI terms and their definition.

of

Make sure to check your spam folder just in case.

What people are saying about DSPy?

Inside the AI Job Market: Key Statistics

Unlock your potential in the fastest-growing tech field.

56%
AI Skills Premium

Wage premium for AI skills comparing workers in the same job with and without AI skills. Up from 25% last year.

Source: PwC AI Jobs Barometer

$327K
AI Total Compensation

Median overall compensation for AI professionals is $326,613/year, with base salary at $160K, equity grants at $104K, and additional bonuses. Based on 170 manually verified offers.

Source: AI Paygrades

51%
Skills Shortage

Over half of global technology leaders now report facing AI skills shortages, representing an 82% jump from the previous year and the steepest rise in tech skills scarcity in over 15 years.

Source: Nash Squared/Harvey Nash Digital Leadership Report, 2025

Ready to Master AI Development?

Start building production-ready AI applications today

Your Learning Journey

Master AI development through a structured, hands-on approach

Start Building Reliable AI Apps

Chapter 1

Move beyond fragile prompts to programmable AI systems. Set up your environment and learn DSPy Signatures for predictable, type-safe LLM outputs.

1

Build Intelligent Reasoning Systems

Chapter 2

Create AI that thinks step-by-step and takes action. Master ChainOfThought for complex reasoning, ReAct for tool-using agents, and compose powerful multi-step pipelines.

2

Test & Evaluate Your AI Systems

Chapter 3

Stop guessing if your AI works. Implement systematic evaluation with metrics, custom functions, and LLM-as-a-judge patterns to measure accuracy, catch failures, and continuously improve performance.

3

Capstone: RepoRank

Chapter 4 • PROJECT

Build RepoRank: an intelligent GitHub discovery tool that analyzes code quality, uses LLMs as judges, and calls external tools—putting everything together in a real project.

4

Model Context Protocol (MCP)

Chapter 5

Extend DSPy with external tools and data. Learn MCP architecture, consume tools in DSPy programs, build MCP servers, and create ReAct modules with tool calling capabilities.

5

The Secret Sauce: Optimization

Chapter 6

Stop manually tweaking prompts. Master DSPy's compilation process and optimizers (BootstrapFewShot, KNN, COPRO, GEPA, MIPROv2) that automatically boost accuracy by 20-50%.

6

Observability with MLflow

Chapter 7

Track experiments, visualize traces, compare compiled vs. uncompiled programs, debug with logs, and manage model versions using MLflow—essential for production systems.

7

Retrieval Augmented Generation

Chapter 8

Master embeddings, vector similarity, and Weaviate. Learn chunking strategies, hybrid search, and build production-ready RAG pipelines that give AI access to your knowledge.

8

Capstone: CRM Auto Reply

Chapter 9 • PROJECT

Build a hyper-contextual customer support system that automatically generates replies to CRM tickets using RAG-powered knowledge retrieval and DSPy optimization.

9

Building AI Agents with DSPy

Chapter 10

Master agent fundamentals: Observe-Think-Act loop, memory systems, planning strategies, and multi-agent patterns. Build DSPyBot, a Slack assistant for DSPy learning and support.

10

Scaling and Productionizing

Chapter 11

Take AI from prototype to production. Build FastAPI endpoints, implement caching strategies, manage LLM providers, handle compiled programs, and ensure security at scale.

11

About the Author

Ankur Gupta - DSPy Book Author

Ankur Gupta

I fell in love with programming in school when DBase III Plus was taught as part of curriculum. That took me on the path to doing my Masters in Computer Applications. My 18+ years building software across startups and large companies took me from systems programming in C and C++, to web applications in Django and AngularJS, through data engineering pipelines with Hadoop and Spark and recommendation engines running on large-scale analytics platforms like Apache Druid.

As a full-time consultant, I recently inherited a failed project for extracting features from real estate call-center transcripts. Through DSPy's optimizers and synthetic data, I refactored the unreliable implementation into a production-ready solution that actually works. This is where my journey with DSPy started. Today it's my goto tool for all consulting work where LLM interaction is needed.

I'm also an active volunteer with the Bangalore Python User Group and have spoken at PyCon India and PyCon Singapore / APAC.

I'm based in Bengaluru with my family. When I'm not buried in code or a book, I'm either playing badminton with my son or playing the role of homework monitor.

Frequently Asked Questions

All the chapters will be released by December 2025

If you don't like the book please ask for refund within next 48 hours on ankur+refund at-the-rate dspyweekly.com. Refund minus the processing fees by Gumroad will be returned.

Absolutely! We offer special pricing for teams, educational institutions, and bulk purchases. Contact us directly on ankur+pricing at-the-rate dspyweekly.com