What is DSPy?

Welcome to the first lesson of our "DSPy Programming" course! Today, we're going to explore DSPy, a powerful framework that's changing how developers work with large language models (LLMs).

DSPy is a framework designed to shift your focus from manually writing brittle prompt templates to writing modular, composable Python code that "programs" language models. The name DSPy stands for "Declarative Self-improving Python," which hints at its core philosophy: letting you declare what you want your language model to do rather than precisely how to do it.

Traditional prompt engineering involves crafting specific text prompts for each task you want a language model to perform. This approach can be tedious and fragile — small changes in wording can dramatically affect results, and maintaining complex prompt templates becomes unwieldy as your applications grow.

DSPy takes a different approach. Instead of handcrafting prompts for every task, you define signatures (input/output specifications) and modules (pre-built prompting techniques) and let DSPy automatically optimize the prompts and even fine-tune the model parameters based on your chosen metrics. This makes your language model applications more robust, maintainable, and easier to iterate on.

Think of DSPy as a programming language specifically designed for working with language models — it provides structure and abstractions that make complex LLM applications more manageable.

Why Use DSPy?

You might be wondering why you should learn yet another framework for working with language models. DSPy offers several compelling advantages over traditional prompt engineering approaches.

  1. DSPy makes your code more modular and reusable. Instead of writing long, complex prompts that mix instructions, examples, and formatting, you can build your application from smaller, focused components that each handle a specific part of the task. This modularity makes your code easier to understand, test, and maintain.

  2. DSPy enables systematic optimization. Rather than manually tweaking prompt wording through trial and error, DSPy provides tools to automatically optimize prompts based on examples and metrics you define. This can lead to better performance with less effort.

  3. DSPy helps you build more complex LLM pipelines. As your applications grow more sophisticated — perhaps incorporating retrieval, multi-step reasoning, or tool use — managing them with traditional prompting becomes increasingly difficult. DSPy's programming model scales better to these complex scenarios.

DSPy particularly shines when building systems like:

  • Classifiers that need to make consistent, well-reasoned decisions
  • Retrieval-Augmented Generation (RAG) systems that combine knowledge retrieval with generation
  • Agent loops that involve planning, tool use, and reflection
  • Multi-step reasoning pipelines that break complex problems into manageable steps

By using DSPy, you're investing in a more sustainable approach to building with language models — one that will scale better as your applications grow in complexity.

Installing DSPy

Before we dive deeper into DSPy concepts, let's make sure you have it installed and ready to use. Installing DSPy is straightforward using pip, Python's package manager:

This command will install the core DSPy library and its dependencies.

If you want to use specific language models or retrievers with DSPy, you might need to install additional dependencies. We'll cover those as needed in later lessons.

Once installed, you can import DSPy in your Python code with a simple import statement:

Now that we have DSPy installed, let's explore the key concepts and development stages that will guide your journey in building AI systems with this framework.

The Three Stages of DSPy Development

DSPy exposes a very small API that you can learn quickly. However, building a new AI system is a more open-ended journey of iterative development, in which you compose the tools and design patterns of DSPy to optimize for your objectives. The DSPy development process typically unfolds in three stages:

  1. DSPy Programming: This first stage is about defining your task, its constraints, exploring a few examples, and using that to inform your initial pipeline design. You'll identify what inputs your system will receive, what outputs it should produce, and sketch out the basic components and flow of your application. This is where you start writing DSPy code to express your task.

  2. DSPy Evaluation: Once your system starts working, you'll move to the stage where you collect an initial development set, define your DSPy metric, and use these to iterate on your system more systematically. Evaluation helps you understand how well your system is performing and where it might be falling short, guiding your improvements.

  3. DSPy Optimization: With a way to evaluate your system in place, you can use DSPy optimizers to tune the prompts or weights in your program. Rather than manually adjusting prompts, DSPy can automatically optimize them based on your examples and metrics, often leading to better performance than hand-crafted prompts.

These stages aren't strictly sequential — you'll likely move back and forth between them as you develop your application. For instance, after optimization, you might discover new failure cases that prompt you to revisit your program design.

In this course, we'll walk through each of these stages, starting with the fundamentals of DSPy programming before moving on to evaluation and optimization techniques.

Summary and Practice Preview

In this first lesson, we've introduced DSPy — a framework that shifts your focus from manually writing prompt templates to writing modular, composable Python code that "programs" language models. We've covered what DSPy is, why it's valuable, how to install it, and the three stages of DSPy development: Programming, Evaluation, and Optimization.

We've also discussed how to get started with DSPy projects by defining your task clearly, building an initial pipeline, and iteratively improving it based on observations. Remember that DSPy is about writing code instead of strings, focusing on the right control flow for your application.

In the upcoming practice exercises, you'll have the opportunity to install DSPy and experiment with some basic concepts. These hands-on activities will help solidify your understanding of what we've covered in this lesson.

In the next lesson, we'll dive deeper into using language models in DSPy, exploring how to configure and interact with different LMs. We'll also look at more concrete examples of DSPy in action. As we progress through the course, you'll learn about creating custom signatures, working with DSPy modules, and eventually building complex AI systems using this powerful framework.

Get ready to transform how you work with language models — DSPy will give you the tools to build more robust, maintainable, and powerful AI applications!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal