DSPy.rb

Build reliable LLM applications in Ruby using composable, type-safe modules.

DSPy.rb brings structured LLM programming to Ruby developers. Instead of wrestling with prompt strings and parsing responses, you define typed signatures and compose them into pipelines that just work.

Traditional prompting is like writing code with string concatenation: it works until it doesn't. DSPy.rb brings you the programming approach pioneered by dspy.ai: instead of crafting fragile prompts, you define modular signatures and let the framework handle the messy details.

The result? LLM applications that actually scale and don't break when you sneeze.

What You Get

Core Building Blocks:

  • Signatures - Define input/output schemas using Sorbet types
  • Predict - Basic LLM completion with structured data
  • Chain of Thought - Step-by-step reasoning for complex problems
  • ReAct - Tool-using agents with basic tool integration
  • CodeAct - Dynamic code execution agents for programming tasks
  • Manual Composition - Combine multiple LLM calls into workflows

Optimization & Evaluation:

  • Prompt Objects - Manipulate prompts as first-class objects instead of strings
  • Typed Examples - Type-safe training data with automatic validation
  • Evaluation Framework - Basic testing with simple metrics
  • Basic Optimization - Simple prompt optimization techniques

Production Features:

  • File-based Storage - Basic optimization result persistence
  • Multi-Platform Observability - OpenTelemetry, New Relic, and Langfuse integration
  • Basic Instrumentation - Event tracking and logging

Developer Experience:

  • LLM provider support using official Ruby clients:
  • Runtime type checking with Sorbet
  • Type-safe tool definitions for ReAct agents
  • Comprehensive instrumentation and observability

Fair Warning

This is fresh off the oven and evolving fast. I'm actively building this as a Ruby port of the DSPy library. If you hit bugs or want to contribute, just email me directly!

Quick Start

Installation

Skip the gem for now - install straight from this repo while I prep the first release:

gem 'dspy', github: 'vicentereig/dspy.rb'

Your First DSPy Program

# Define a signature for sentiment classification
class Classify < DSPy::Signature
  description "Classify sentiment of a given sentence."

  class Sentiment < T::Enum
    enums do
      Positive = new('positive')
      Negative = new('negative')
      Neutral = new('neutral')
    end
  end

  input do
    const :sentence, String
  end

  output do
    const :sentiment, Sentiment
    const :confidence, Float
  end
end

# Configure DSPy with your LLM
DSPy.configure do |c|
  c.lm = DSPy::LM.new('openai/gpt-4o-mini', api_key: ENV['OPENAI_API_KEY'])
end

# Create the predictor and run inference
classify = DSPy::Predict.new(Classify)
result = classify.call(sentence: "This book was super fun to read!")

puts result.sentiment    # => #<Sentiment::Positive>  
puts result.confidence   # => 0.85

Documentation

Getting Started

Core Features

Optimization

Production Features

Advanced Usage

What's Next

These are my goals to release v1.0.

  • ✅ Prompt objects foundation - Done
  • ✅ Evaluation framework - Done
  • ✅ Teleprompter base classes - Done
  • ✅ MIPROv2 optimization algorithm - Done
  • ✅ Storage & persistence system - Done
  • ✅ Registry & version management - Done
  • ✅ OpenTelemetry integration - Done
  • ✅ New Relic integration - Done
  • ✅ Langfuse integration - Done
  • 🚧 Ollama support
  • Context Engineering (see recent research: How Contexts Fail, How to Fix Your Context, Context Engineering)
  • Agentic Memory support
  • MCP Support
  • Documentation website
  • Performance benchmarks

License

This project is licensed under the MIT License.