DSPy Basics
Core DSPy fundamentals for signature design, basic modules, and program composition.
Quick Start
Create a signature
import dspy
class MySignature(dspy.Signature):
"""Process input and produce output."""
input_text = dspy.InputField(desc="Text to process")
output_text = dspy.OutputField(desc="Processed result")
Use a basic module
# Simple prediction predict = dspy.Predict(MySignature) result = predict(input_text="Hello") print(result.output_text) # Chain of thought cot = dspy.ChainOfThought(MySignature) result = cot(input_text="Hello") print(result.rationale) # Reasoning steps print(result.output_text)
Compose modules
class MyProgram(dspy.Module):
def __init__(self):
super().__init__()
self.step1 = dspy.Predict(Step1Signature)
self.step2 = dspy.Predict(Step2Signature)
def forward(self, input_data):
result1 = self.step1(input=input_data)
result2 = self.step2(intermediate=result1.output)
return dspy.Prediction(final_output=result2.output)
Important: Implement forward() in subclasses, but always call via __call__() (i.e., module(input)). Never call forward() directly—DSPy warns against this and it bypasses callbacks/usage tracking.
When to Use This Skill
Use this skill when:
- •Creating new DSPy signatures
- •Building simple DSPy programs with basic modules
- •Learning foundational DSPy concepts
- •Designing input/output contracts
- •Composing modules sequentially, conditionally, or in parallel
Core Concepts
Signatures
DSPy signatures define the input/output contract for your programs. They specify what data flows into and out of your modules.
Key components:
- •
dspy.InputField: Data provided to program - •
dspy.OutputField: Data the program produces - •Field descriptions: Clear explanations of each field's purpose
- •Type hints: Optional type annotations for validation
See: references/signatures.md for:
- •InputField vs OutputField usage
- •Type hints and validation
- •Design patterns and best practices
Basic Modules
DSPy provides built-in modules that implement common prompting strategies.
Core modules:
- •
dspy.Predict: Direct prediction from LM - •
dspy.ChainOfThought: Enables reasoning before final output - •
dspy.ProgramOfThought: Multi-step reasoning with planning
See: references/programs.md for:
- •Module usage patterns
- •Composition strategies
- •Configuration options
Program Composition
DSPy programs are built by composing multiple modules together.
Composition patterns:
- •Sequential: Execute modules in order
- •Conditional: Branch based on intermediate results
- •Parallel: Execute multiple modules concurrently
- •Hierarchical: Compose modules from other modules
See: references/programs.md for detailed patterns
Templates
The templates/ directory provides boilerplate for new signatures:
- •signature-template.py: Starting point for new signatures with common patterns
Progressive Disclosure
This skill uses progressive disclosure to manage context efficiently:
- •SKILL.md (this file): Quick reference and navigation (~2000 words)
- •references/: Detailed technical docs loaded as needed
Load reference files only when you need detailed information on a specific topic.
Related Skills
- •dspy-optimization: Teleprompters, metrics, and optimization workflows
- •dspy-advanced: ReAct agents, tool calling, output refinement
- •dspy-configuration: LM setup, caching, and version management