Overview
References:- GEPA: Agrawal et al. (2025). “GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning.” arXiv:2507.19457
- MIPRO: Opsahl-Ong et al. (2024). “Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs.” arXiv:2406.11695
What Are Specs?
Specs are JSON files that encode:- Principles: High-level guidelines for the task
- Rules: Specific policies with priorities (0-10)
- Constraints: Must/must-not/should directives
- Examples: Good and bad examples for each rule
- Glossary: Domain-specific terminology
- Interfaces: Input/output formats and capabilities
Example Spec Structure
How Specs Are Used
GEPA: Spec-Guided Mutations
When GEPA usesproposer_type = "spec", the spec is included in mutation prompts:
- Spec Loading: GEPA loads the spec JSON file at initialization
- Context Serialization: Spec is converted to compact markdown format (up to
spec_max_tokens) - Mutation Prompts: Spec context is injected into LLM-guided mutation prompts
- Rule Filtering: Only rules with
priority >= spec_priority_thresholdare included
MIPRO: Spec-Enhanced Meta-Prompts
MIPRO includes spec context in meta-prompts for instruction generation:- Spec Loading: MIPRO loads the spec during initialization
- Compact Context: Spec is serialized to compact format (respects
spec_max_tokens) - Meta-Prompt Injection: Spec context is added to meta-LLM prompts
- Instruction Generation: Meta-LLM uses spec rules/principles to generate better instructions
Configuration Parameters
spec_path (Required)
Path to the spec JSON file (relative to config file or absolute).
spec_max_tokens (Default: 5000)
Maximum tokens for spec context in prompts. The serializer will:
- Start with high-priority rules (priority >= 7)
- Remove examples if still too long
- Remove glossary if still too long
- Increase priority threshold if still too long
spec_include_examples (Default: true)
Whether to include rule examples in the spec context.
spec_priority_threshold (Optional)
Only include rules with priority >= threshold. Higher threshold = fewer but more important rules.
- 10: Critical rules (must always be followed)
- 9: High-priority rules (important for accuracy)
- 8: Medium-high priority (recommended)
- 7: Medium priority (helpful)
- <7: Lower priority (may be filtered out)
Spec Format Details
Principles
High-level guidelines that apply across all rules:Rules
Specific policies with priorities and constraints:Constraints Types
- must: Required behaviors (always enforced)
- must_not: Prohibited behaviors (never allowed)
- should: Recommended behaviors (preferred when possible)
- should_not: Discouraged behaviors (avoid when possible)
Benefits of Using Specs
1. Domain Knowledge Injection
Specs encode expert knowledge about the task:- Edge cases and disambiguation rules
- Domain-specific terminology
- Priority-based policies
2. Constraint-Aware Optimization
Optimizers respect spec constraints:- GEPA: Mutations follow spec rules (must/must_not)
- MIPRO: Instruction proposals align with spec principles
3. Faster Convergence
Spec-guided optimization typically:- Converges faster (fewer generations/iterations)
- Produces more accurate prompts
- Better handles edge cases
4. Consistency
Specs ensure:- Consistent terminology across prompts
- Alignment with domain requirements
- Compliance with business rules
Example: Banking77 Pipeline Spec
Location:examples/task_apps/banking77_pipeline/banking77_pipeline_spec.json
Key Rules:
- R-card-disambiguation (Priority 10): Distinguish card delivery vs. payment issues
- R-urgency-signals (Priority 10): Handle urgent queries (lost cards, fraud)
- R-balance-transfer (Priority 9): Disambiguate balance update scenarios
- R-stage-coordination (Priority 8): Coordinate between analyzer and classifier stages
When to Use Specs
Use specs when:- ✅ You have domain expertise to encode
- ✅ Task has complex edge cases or disambiguation rules
- ✅ You want faster convergence
- ✅ Consistency with business rules is critical
- ✅ Multi-stage pipelines need coordination rules
- ❌ Task is simple and straightforward
- ❌ No domain-specific rules or constraints
- ❌ You want maximum exploration (specs may constrain search)
Creating a Spec
Step 1: Define Principles
Start with high-level guidelines:Step 2: Add Rules
Define specific policies with priorities:Step 3: Add Glossary
Define domain-specific terms:Step 4: Reference in Config
Point to the spec file:Best Practices
- Start with High-Priority Rules: Focus on critical constraints first (priority 8+)
- Include Examples: Good and bad examples help the optimizer understand intent
- Use Clear Constraints: Be specific with must/must_not directives
- Test Token Limits: Ensure
spec_max_tokensfits in your model’s context window - Filter by Priority: Use
spec_priority_thresholdto focus on important rules - Update Regularly: Keep specs in sync with task requirements
Comparison: DSPy vs Spec Mode
| Aspect | DSPy Mode | Spec Mode |
|---|---|---|
| Guidance | Generic prompt engineering principles | Domain-specific rules and constraints |
| Convergence | Slower (broader exploration) | Faster (focused search) |
| Accuracy | Good for general tasks | Better for domain-specific tasks |
| Setup | No additional files | Requires spec JSON file |
| Best For | Simple tasks, exploration | Complex tasks, edge cases |
Next Steps
- Configuration Reference – Complete spec parameter documentation
- GEPA Guide – How GEPA uses specs
- MIPRO Guide – How MIPRO uses specs
- Banking77 Example – Real-world spec usage