Understanding DSPy Code
Learn how DSPy Code works and how it helps you build better DSPy programs.
What is DSPy Code?
DSPy Code is an interactive development environment for DSPy that:
- Generates DSPy code from natural language
- Validates your code against best practices
- Helps you optimize programs with GEPA
- Provides a knowledge base about DSPy concepts
- Adapts to your installed DSPy version
Core Concepts
1. Living Playbook
DSPy Code is a "living playbook" that:
Adapts to YOUR DSPy version:
- Indexes your installed DSPy package
- Answers questions based on YOUR version
- Generates code compatible with YOUR installation
Understands your project:
- Scans your existing DSPy code
- Learns your patterns and conventions
- Generates code that fits your project
Stays current:
- Re-indexes when you run
/init - Detects version changes
- Warns about outdated versions
2. Interactive-Only Design
All commands are slash commands in interactive mode:
dspy-code
â /init
â /model
â Create a sentiment analyzer
â /save sentiment.py
â /validate
â /run
Why interactive-only?
- Natural conversation flow
- Context-aware responses
- Better error handling
- Easier to learn
3. Natural Language Generation
Describe what you want in plain English:
DSPy Code generates:
- Complete Signature
- Module implementation
- Example usage
- Configuration code
4. Version Awareness
DSPy Code shows your DSPy version on startup:
If your version is old:
Why this matters:
- DSPy APIs change between versions
- Generated code matches YOUR version
- Avoid compatibility issues
Architecture
Components
DSPy Code
âââ Interactive Session
â âââ Natural language processing
â âââ Context management
â âââ Conversation history
âââ Code Generation
â âââ Signature generator
â âââ Module generator
â âââ Program templates
âââ Validation Engine
â âââ Syntax checker
â âââ Best practices validator
â âââ Quality scorer
âââ Optimization
â âââ GEPA integration
â âââ Metric generation
â âââ Data collection
âââ Codebase RAG
â âââ DSPy indexer
â âââ Project scanner
â âââ Semantic search
âââ Model Connector
âââ Ollama
âââ OpenAI
âââ Anthropic
âââ Gemini
Data Flow
User Input
â
Natural Language Understanding
â
Intent Detection
â
Code Generation / Command Execution
â
Validation (if code)
â
Response to User
How Code Generation Works
Step 1: Intent Detection
DSPy Code analyzes your request:
Detected intent:
- Type: Module generation
- Task: Sentiment analysis
- Predictor: Not specified (will use default)
Step 2: Signature Generation
Creates the DSPy Signature:
class SentimentSignature(dspy.Signature):
"""Analyze sentiment of text."""
text = dspy.InputField(desc="Text to analyze")
sentiment = dspy.OutputField(desc="positive, negative, or neutral")
Step 3: Module Generation
Creates the Module:
class SentimentAnalyzer(dspy.Module):
def __init__(self):
super().__init__()
self.predictor = dspy.Predict(SentimentSignature)
def forward(self, text):
return self.predictor(text=text)
Step 4: Add Usage Examples
Includes example code:
# Example usage
analyzer = SentimentAnalyzer()
result = analyzer(text="I love this!")
print(result.sentiment)
Step 5: Context Storage
Stores in session context:
Now you can use /save, /validate, /run!
How Validation Works
Validation Checks
1. Signature Validation:
- â Inherits from dspy.Signature
- â Has InputField and OutputField
- â Fields have descriptions
- â Docstring present
2. Module Validation:
- â Inherits from dspy.Module
- â
Has
__init__method - â
Has
forwardmethod - â Uses DSPy predictors
3. Best Practices:
- â Type hints used
- â Descriptive names
- â No anti-patterns
- â Proper error handling
4. Syntax:
- â Valid Python
- â No syntax errors
- â Imports present
Quality Scoring
Each check contributes to a quality score:
Signature structure: 20 points
Module structure: 20 points
Best practices: 30 points
Documentation: 15 points
Type hints: 15 points
Total: 100 points
Score interpretation:
- 90-100: Excellent
- 80-89: Good
- 70-79: Acceptable
- <70: Needs improvement
How Codebase RAG Works
Indexing Process
1. Discovery:
2. Scanning:
3. Extraction:
4. Indexing:
Answering Questions
When you ask a question:
1. Search:
- Semantic search in indexed code
- Find relevant classes/functions
- Rank by relevance
2. Context Building:
- Extract code snippets
- Get docstrings
- Find usage examples
3. Answer Generation:
- Use LLM with context
- Generate explanation
- Include code examples
4. Response:
ChainOfThought is a DSPy predictor that uses reasoning...
Example from your DSPy version:
class ChainOfThought(Predict):
def __init__(self, signature, rationale_type=None, **config):
...
How Optimization Works
GEPA Overview
GEPA (Genetic Pareto) optimizes DSPy programs by:
- Evaluation: Test current performance
- Reflection: Analyze failures
- Evolution: Generate better prompts
- Selection: Keep best versions
- Iteration: Repeat until optimal
Optimization Cost & Resource Considerations
- Cloud models (OpenAI, Anthropic, Gemini): GEPA can trigger many LLM calls. Only run optimization if you understand the potential API cost and have billing/quotas configured appropriately.
- Local runs: For smooth optimization on local hardware, we recommend at least 32 GB RAM, especially with larger models.
- Start with small budgets and fewer examples when experimenting.
Optimization Process
1. Prepare Data:
2. Generate Optimization Script:
3. Run GEPA:
from dspy.teleprompt import GEPA
optimizer = GEPA(
metric=accuracy_with_feedback,
breadth=10,
depth=3,
init_temperature=1.4
)
optimized = optimizer.compile(
program,
trainset=examples
)
4. Results:
Metrics with Feedback
GEPA uses metrics that provide feedback:
def accuracy_with_feedback(gold, pred, trace=None):
if gold.sentiment == pred.sentiment:
return 1.0
else:
feedback = f"Expected {gold.sentiment}, got {pred.sentiment}"
return {'score': 0.0, 'feedback': feedback}
This feedback helps GEPA learn!
How Model Connection Works
Supported Providers
1. Ollama (Local):
2. OpenAI (modern SDK, example small model):
3. Anthropic (paid only, optional):
Anthropic no longer offers free API keys. If you have a paid key, DSPy Code will work with Claude; otherwise, just skip Anthropic and use another provider.
4. Gemini (via google-genai):
Connection Process
1. Validation:
- Check provider is valid
- Verify model name
- Test API key (if needed)
2. Configuration:
if provider == "ollama":
lm = dspy.OllamaLocal(model=model_name)
elif provider == "openai":
lm = dspy.OpenAI(model=model_name, api_key=api_key)
3. Testing:
4. Storage:
# dspy_config.yaml
models:
default: ollama/llama3.1:8b
ollama:
llama3.1:8b:
base_url: http://localhost:11434
Session Management
Session Context
Each session maintains:
context = {
'last_generated': "...", # Last generated code
'type': 'module', # Code type
'last_generated_data': [...], # Training data
'data_task': 'sentiment', # Task description
'history': [...] # Conversation history
}
Persistence
Save session:
Load session:
List sessions:
What's Saved
- Generated code
- Training data
- Model configuration
- Conversation history
- Project context
Configuration
Project Configuration
dspy_config.yaml:
project_name: my-dspy-project
dspy_version: 3.0.4
models:
default: ollama/llama3.1:8b
ollama:
llama3.1:8b:
base_url: http://localhost:11434
paths:
generated: generated/
data: data/
cache: .cache/
rag:
enabled: true
cache_ttl: 86400
User Configuration
~/.dspy-code/config.yaml:
Best Practices
1. Always Initialize
Run /init when starting a new project:
This builds the codebase index!
2. Check Status Often
Use /status to see what's in context:
3. Validate Before Running
Always validate generated code:
4. Save Your Work
Save sessions for complex projects:
5. Use Descriptive Requests
Be specific when generating code:
Good: "Build a module using ChainOfThought for sentiment analysis with text input and sentiment output"
Bad: "Make a sentiment thing"
6. Iterate
Refine generated code:
Add error handling to the last generated code
Add type hints to all functions
Optimize for better performance
Troubleshooting
Code Not Saving
Check context:
If no code in context, generate first:
Validation Fails
Read error messages:
Fix issues:
Model Not Connected
Check status:
Connect:
Index Not Built
Run init:
Summary
DSPy Code is:
- â Interactive development environment
- â Natural language code generator
- â Validation and quality checker
- â GEPA optimization platform
- â DSPy knowledge base
- â Version-aware assistant
Start building better DSPy programs today!