Skip to main content
DSPy: declarative LM programs, auto-optimize prompts, RAG.

Skill metadata

Reference: full SKILL.md

The following is the complete skill definition that Mibyan loads when this skill is triggered. This is what the agent sees as instructions when the skill is active.

DSPy: Declarative Language Model Programming

When to Use This Skill

Use DSPy when you need to:
  • Build complex AI systems with multiple components and workflows
  • Program LMs declaratively instead of manual prompt engineering
  • Optimize prompts automatically using data-driven methods
  • Create modular AI pipelines that are maintainable and portable
  • Improve model outputs systematically with optimizers
  • Build RAG systems, agents, or classifiers with better reliability
GitHub Stars: 22,000+ | Created By: Stanford NLP

Installation

Quick Start

Basic Example: Question Answering

Chain of Thought Reasoning

Core Concepts

1. Signatures

Signatures define the structure of your AI task (inputs → outputs):
When to use each:
  • Inline: Quick prototyping, simple tasks
  • Class: Complex tasks, type hints, better documentation

2. Modules

Modules are reusable components that transform inputs to outputs:

dspy.Predict

Basic prediction module:

dspy.ChainOfThought

Generates reasoning steps before answering:

dspy.ReAct

Agent-like reasoning with tools:

dspy.ProgramOfThought

Generates and executes code for reasoning:

3. Optimizers

Optimizers improve your modules automatically using training data:

BootstrapFewShot

Learns from examples:

MIPRO (Most Important Prompt Optimization)

Iteratively improves prompts:

BootstrapFinetune

Creates datasets for model fine-tuning:

4. Building Complex Systems

Multi-Stage Pipeline

RAG System with Optimization

LM Provider Configuration

Anthropic Claude

OpenAI

Local Models (Ollama)

Multiple Models

Common Patterns

Pattern 1: Structured Output

Pattern 2: Assertion-Driven Optimization

Pattern 3: Self-Consistency

Pattern 4: Retrieval with Reranking

Evaluation and Metrics

Custom Metrics

Evaluation

Best Practices

1. Start Simple, Iterate

2. Use Descriptive Signatures

3. Optimize with Representative Data

4. Save and Load Optimized Models

5. Monitor and Debug

Comparison to Other Approaches

When to choose DSPy:
  • You have training data or can generate it
  • You need systematic prompt improvement
  • You’re building complex multi-stage systems
  • You want to optimize across different LMs
When to choose alternatives:
  • Quick prototypes (manual prompting)
  • Simple chains with existing tools (LangChain)
  • Custom optimization logic needed

Resources

See Also

  • references/modules.md - Detailed module guide (Predict, ChainOfThought, ReAct, ProgramOfThought)
  • references/optimizers.md - Optimization algorithms (BootstrapFewShot, MIPRO, BootstrapFinetune)
  • references/examples.md - Real-world examples (RAG, agents, classifiers)