roberto-mello/lavra/dspy-ruby
Claude Code and agent skill by roberto-mello/lavra
- community
- ext:[{"type":"hook","description":"Pre/post lifecycle hooks"}]
Checking that the SKILL.md file is still in the repository.
Build type-safe LLM applications with DSPy.rb. Use when implementing AI features, LLM signatures, agent systems, or prompt optimization in Ruby.
Install
npx vskill@latest install roberto-mello/lavra/dspy-rubySemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
DSPy.rb
Build LLM apps like you build software. Type-safe, modular, testable.
DSPy.rb brings software engineering best practices to LLM development. Instead of tweaking prompts, define what you want with Ruby types and let DSPy handle the rest.
Overview
DSPy.rb is a Ruby framework for building language model applications with programmatic prompts. It provides:
- Type-safe signatures — Define inputs/outputs with Sorbet types
- Modular components — Compose and reuse LLM logic
- Automatic optimization — Use data to improve prompts, not guesswork
- Production-ready — Built-in observability, testing, and error handling
Core Concepts
1. Signatures
Define interfaces between your app and LLMs using Ruby types:
2. Modules
Build complex workflows from simple building blocks:
- Predict — Basic LLM calls with signatures
- ChainOfThought — Step-by-step reasoning
- ReAct — Tool-using agents
- CodeAct — Dynamic code generation agents (install the
dspy-code_actgem)
3. Tools & Toolsets
Create type-safe tools for agents with comprehensive Sorbet support:
4. Type System & Discriminators
DSPy.rb uses sophisticated type discrimination for complex data structures:
- Automatic `_type` field injection — DSPy adds discriminator fields to structs for type safety
- Union type support —
T.any()types automatically disambiguated by_type - Reserved field name — Avoid defining your own
_typefields in structs - Recursive filtering —
_typefields filtered during deserialization at all nesting levels
5. Optimization
Improve accuracy with real data:
- MIPROv2 — Advanced multi-prompt optimization with bootstrap sampling and Bayesian optimization
- GEPA — Genetic-Pareto Reflective Prompt Evolution with feedback maps, experiment tracking, and telemetry
- Evaluation — Comprehensive framework with built-in and custom metrics, error handling, and batch processing
Quick Start
Provider Adapter Gems
Two strategies for connecting to LLM providers:
Per-provider adapters (direct SDK access)
Each adapter gem pulls in the official SDK (openai, anthropic, gemini-ai).
Unified adapter via RubyLLM (recommended for multi-provider)
RubyLLM handles provider routing based on the model name. Use the ruby_llm/ prefix:
Events System
DSPy.rb ships with a structured event bus for observing runtime behavior.
Module-Scoped Subscriptions (preferred for agents)
Global Subscriptions (for observability/integrations)
Event names use dot-separated namespaces (llm.generate, react.iteration_complete). Every event includes module metadata (module_path, module_leaf, module_scope.ancestry_token) for filtering.
Lifecycle Callbacks
Rails-style lifecycle hooks ship with every DSPy::Module:
- `before` — Runs ahead of
forwardfor setup (metrics, context loading) - `around` — Wraps
forward, callsyield, and lets you pair setup/teardown logic - `after` — Fires after
forwardreturns for cleanup or persistence
Execution order: before → around (before yield) → forward → around (after yield) → after. Callbacks are inherited from parent classes and execute in registration order.
Fiber-Local LM Context
Override the language model temporarily using fiber-local storage:
LM resolution hierarchy: Instance-level LM → Fiber-local LM (DSPy.with_lm) → Global LM (DSPy.configure).
Use configure_predictor for fine-grained control over agent internals:
Evaluation Framework
Systematically test LLM application performance with DSPy::Evals:
Built-in metrics: exact_match, contains, numeric_difference, composite_and. Custom metrics return true/false or a DSPy::Prediction with score: and feedback: fields.
Use DSPy::Example for typed test data and export_scores: true to push results to Langfuse.
GEPA Optimization
GEPA (Genetic-Pareto Reflective Prompt Evolution) uses reflection-driven instruction rewrites:
The metric must return DSPy::Prediction.new(score:, feedback:) so the reflection model can reason about failures. Use feedback_map to target individual predictors in composite modules.
Typed Context Pattern
Replace opaque string context blobs with T::Struct inputs. Each field gets its own description: annotation in the JSON schema the LLM sees:
Benefits: type safety at compile time, per-field descriptions in the LLM schema, easy to test as value objects, extensible by adding const declarations.
Schema Formats (BAML / TOON)
Control how DSPy describes signature structure to the LLM:
- JSON Schema (default) — Standard format, works with
structured_outputs: true - BAML (
schema_format: :baml) — 84% token reduction for Enhanced Prompting mode. Requiressorbet-bamlgem. - TOON (
schema_format: :toon, data_format: :toon) — Table-oriented format for both schemas and data. Enhanced Prompting mode only.
BAML and TOON apply only when structured_outputs: false. With structured_outputs: true, the provider receives JSON Schema directly.
Storage System
Persist and reload optimized programs with DSPy::Storage::ProgramStorage:
Supports checkpoint management, optimization history tracking, and import/export between environments.
Rails Integration
Directory Structure
Organize DSPy components using Rails conventions:
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingplugins/lavra/skills/optional/dspy-ruby/SKILL.md
Popularity
- Repository stars
- 47
- Repository forks
- 2
- 7-day trend score
- 19
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Extension points
hookPre/post lifecycle hooks
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/roberto-mello/lavra/dspy-ruby)