- Home
- Skills
- Omidzamani
- Dspy Skills
- Dspy Custom Module Design
dspy-custom-module-design_skill
- Python
26
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill omidzamani/dspy-skills --skill dspy-custom-module-design- SKILL.md7.7 KB
Overview
This skill guides the design and implementation of production-quality custom DSPy modules. It focuses on architecture, state management, serialization, error handling, and testing patterns to make modules reusable and robust. Practical examples cover stateless and stateful modules, caching, validation, and safe serialization.
How this skill works
The skill describes how to extend dspy.Module and wire predictors, retrievers, and generators into coherent components. It explains stateful patterns (caching, counters), error handling that returns safe dspy.Prediction objects, and serialization/load workflows for saving module state and programs. It also covers testing strategies and mocking LM calls for unit tests.
When to use it
- You need a reusable DSPy component beyond built-in modules
- Implementing stateful behavior (caching, counters, session state)
- Preparing modules for production deployment and serialization
- Designing modules that validate inputs and fail safely
- Sharing modules across projects or composing larger pipelines
Best practices
- Single responsibility: keep each module focused on one job
- Validate inputs early and return safe Prediction objects instead of raising
- Log key events: cache hits, misses, errors, and validation failures
- Keep custom state manageable; prefer bounded caches and explicit clear methods
- Serialize only necessary state and provide load paths that reconstruct runtime components
Example use cases
- BasicQA module wrapping a single Predict model for simple Q&A
- StatefulRAG with retrieval, generation, and a bounded query cache
- RobustClassifier that validates labels and normalizes unexpected outputs
- ProductionRAG combining validation, logging, cache management, and graceful error responses
- Saving a module directory and loading a full program for deployment
FAQ
Catch exceptions and return a dspy.Prediction (or equivalent output) with an error field and safe default values. Log the exception for observability rather than letting it propagate.
What gets saved by module.save()?
Module.save persists declared state and supported components. Custom runtime state may need explicit serialization; for full program saves, use save_program where supported (dspy>=2.6.0). Mock or rebuild non-serializable resources on load.