dspy-deep_research 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 58c30ff4c120a958e50e1947391f08146d23d292bc6ca1b9c3ef676a6dd96a1d
4
+ data.tar.gz: da5679efc54b55b7e2e535c41697055cd98d401ddb8f01fec49765201ee4c991
5
+ SHA512:
6
+ metadata.gz: 7567fe0b6ca805964f9811cf6b335b3cd86b37dd582f1fcd9d3ebfdc2956c51b92444781d921424609e7048c44451eaecaa9dc1521032e787774ccdfa2c35297
7
+ data.tar.gz: 4290423909627d6ac1709b6b0672945d8c127cc99ab93cd87b5f971a44f487b43733c04bf77f73255e14729755be02e338efc8cf96a6498672110f0530cdbe4f
data/LICENSE ADDED
@@ -0,0 +1,45 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Vicente Services SL
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ This project is a Ruby port of the original Python [DSPy library](https://github.com/stanfordnlp/dspy), which is licensed under the MIT License:
24
+
25
+ MIT License
26
+
27
+ Copyright (c) 2023 Stanford Future Data Systems
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy
30
+ of this software and associated documentation files (the "Software"), to deal
31
+ in the Software without restriction, including without limitation the rights
32
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33
+ copies of the Software, and to permit persons to whom the Software is
34
+ furnished to do so, subject to the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be included in all
37
+ copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,313 @@
1
+ # DSPy.rb
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/dspy)](https://rubygems.org/gems/dspy)
4
+ [![Total Downloads](https://img.shields.io/gem/dt/dspy)](https://rubygems.org/gems/dspy)
5
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/vicentereig/dspy.rb/ruby.yml?branch=main&label=build)](https://github.com/vicentereig/dspy.rb/actions/workflows/ruby.yml)
6
+ [![Documentation](https://img.shields.io/badge/docs-vicentereig.github.io%2Fdspy.rb-blue)](https://vicentereig.github.io/dspy.rb/)
7
+
8
+ > [!NOTE]
9
+ > The core Prompt Engineering Framework is production-ready with
10
+ > comprehensive documentation. I am focusing now on educational content on systematic Prompt Optimization and Context Engineering.
11
+ > Your feedback is invaluable. if you encounter issues, please open an [issue](https://github.com/vicentereig/dspy.rb/issues). If you have suggestions, open a [new thread](https://github.com/vicentereig/dspy.rb/discussions).
12
+ >
13
+ > If you want to contribute, feel free to reach out to me to coordinate efforts: hey at vicente.services
14
+ >
15
+ > And, yes, this is 100% a legit project. :)
16
+
17
+
18
+ **Build reliable LLM applications in idiomatic Ruby using composable, type-safe modules.**
19
+
20
+ The Ruby framework for programming with large language models. DSPy.rb brings structured LLM programming to Ruby developers, programmatic Prompt Engineering and Context Engineering.
21
+ Instead of wrestling with prompt strings and parsing responses, you define typed signatures using idiomatic Ruby to compose and decompose AI Worklows and AI Agents.
22
+
23
+ **Prompts are the just Functions.** Traditional prompting is like writing code with string concatenation: it works until it doesn't. DSPy.rb brings you
24
+ the programming approach pioneered by [dspy.ai](https://dspy.ai/): instead of crafting fragile prompts, you define modular
25
+ signatures and let the framework handle the messy details.
26
+
27
+ DSPy.rb is an idiomatic Ruby surgical port of Stanford's [DSPy framework](https://github.com/stanfordnlp/dspy). While implementing
28
+ the core concepts of signatures, predictors, and the main optimization algorithms from the original Python library, DSPy.rb embraces Ruby
29
+ conventions and adds Ruby-specific innovations like Sorbet-base Typed system, ReAct loops, and production-ready integrations like non-blocking Open Telemetry Instrumentation.
30
+
31
+ **What you get?** Ruby LLM applications that actually scale and don't break when you sneeze.
32
+
33
+ Check the [examples](examples/) and take them for a spin!
34
+
35
+ ## Your First DSPy Program
36
+ ### Installation
37
+
38
+ Add to your Gemfile:
39
+
40
+ ```ruby
41
+ gem 'dspy'
42
+ ```
43
+
44
+ and
45
+
46
+ ```bash
47
+ bundle install
48
+ ```
49
+
50
+ ### Your First Reliable Predictor
51
+
52
+ ```ruby
53
+
54
+ # Configure DSPy globablly to use your fave LLM - you can override this on an instance levle.
55
+ DSPy.configure do |c|
56
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
57
+ api_key: ENV['OPENAI_API_KEY'],
58
+ structured_outputs: true) # Enable OpenAI's native JSON mode
59
+ end
60
+
61
+ # Define a signature for sentiment classification - instead of writing a full prompt!
62
+ class Classify < DSPy::Signature
63
+ description "Classify sentiment of a given sentence." # sets the goal of the underlying prompt
64
+
65
+ class Sentiment < T::Enum
66
+ enums do
67
+ Positive = new('positive')
68
+ Negative = new('negative')
69
+ Neutral = new('neutral')
70
+ end
71
+ end
72
+
73
+ # Structured Inputs: makes sure you are sending only valid prompt inputs to your model
74
+ input do
75
+ const :sentence, String, description: 'The sentence to analyze'
76
+ end
77
+
78
+ # Structured Outputs: your predictor will validate the output of the model too.
79
+ output do
80
+ const :sentiment, Sentiment, description: 'The sentiment of the sentence'
81
+ const :confidence, Float, description: 'A number between 0.0 and 1.0'
82
+ end
83
+ end
84
+
85
+ # Wire it to the simplest prompting technique - a Predictn.
86
+ classify = DSPy::Predict.new(Classify)
87
+ # it may raise an error if you mess the inputs or your LLM messes the outputs.
88
+ result = classify.call(sentence: "This book was super fun to read!")
89
+
90
+ puts result.sentiment # => #<Sentiment::Positive>
91
+ puts result.confidence # => 0.85
92
+ ```
93
+
94
+ ### Sibling Gems
95
+
96
+ DSPy.rb ships multiple gems from this monorepo so you can opt into features with heavier dependency trees (e.g., datasets pull in Polars/Arrow, MIPROv2 requires `numo-*` BLAS bindings) only when you need them. Add these alongside `dspy`:
97
+
98
+ | Gem | Description | Status |
99
+ | --- | --- | --- |
100
+ | `dspy-schema` | Exposes `DSPy::TypeSystem::SorbetJsonSchema` for downstream reuse. (Still required by the core `dspy` gem; extraction lets other projects depend on it directly.) | **Stable** (v1.0.0) |
101
+ | `dspy-code_act` | Think-Code-Observe agents that synthesize and execute Ruby safely. (Add the gem or set `DSPY_WITH_CODE_ACT=1` before requiring `dspy/code_act`.) | **Stable** (v1.0.0) |
102
+ | `dspy-datasets` | Dataset helpers plus Parquet/Polars tooling for richer evaluation corpora. (Toggle via `DSPY_WITH_DATASETS`.) | **Stable** (v1.0.0) |
103
+ | `dspy-evals` | High-throughput evaluation harness with metrics, callbacks, and regression fixtures. (Toggle via `DSPY_WITH_EVALS`.) | **Stable** (v1.0.0) |
104
+ | `dspy-miprov2` | Bayesian optimization + Gaussian Process backend for the MIPROv2 teleprompter. (Install or export `DSPY_WITH_MIPROV2=1` before requiring the teleprompter.) | **Stable** (v1.0.0) |
105
+ | `dspy-gepa` | `DSPy::Teleprompt::GEPA`, reflection loops, experiment tracking, telemetry adapters. (Install or set `DSPY_WITH_GEPA=1`.) | **Stable** (v1.0.0) |
106
+ | `gepa` | GEPA optimizer core (Pareto engine, telemetry, reflective proposer). | **Stable** (v1.0.0) |
107
+ | `dspy-o11y` | Core observability APIs: `DSPy::Observability`, async span processor, observation types. (Install or set `DSPY_WITH_O11Y=1`.) | **Stable** (v1.0.0) |
108
+ | `dspy-o11y-langfuse` | Auto-configures DSPy observability to stream spans to Langfuse via OTLP. (Install or set `DSPY_WITH_O11Y_LANGFUSE=1`.) | **Stable** (v1.0.0) |
109
+ | `dspy-deep_search` | Production DeepSearch loop with Exa-backed search/read, token budgeting, and instrumentation (Issue #163). | **Beta** (v1.0.0) |
110
+ | `dspy-deep_research` | Planner/QA orchestration atop DeepSearch plus the memory supervisor used by the CLI example. | **Beta** (v1.0.0) |
111
+
112
+ Set the matching `DSPY_WITH_*` environment variables (see `Gemfile`) to include or exclude each sibling gem when running Bundler locally (for example `DSPY_WITH_GEPA=1` or `DSPY_WITH_O11Y_LANGFUSE=1`). Refer to `adr/013-dependency-tree.md` for the full dependency map and roadmap.
113
+ ### Your First Reliable Predictor
114
+
115
+ ```ruby
116
+
117
+ # Configure DSPy globablly to use your fave LLM - you can override this on an instance levle.
118
+ DSPy.configure do |c|
119
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
120
+ api_key: ENV['OPENAI_API_KEY'],
121
+ structured_outputs: true) # Enable OpenAI's native JSON mode
122
+ end
123
+
124
+ # Define a signature for sentiment classification - instead of writing a full prompt!
125
+ class Classify < DSPy::Signature
126
+ description "Classify sentiment of a given sentence." # sets the goal of the underlying prompt
127
+
128
+ class Sentiment < T::Enum
129
+ enums do
130
+ Positive = new('positive')
131
+ Negative = new('negative')
132
+ Neutral = new('neutral')
133
+ end
134
+ end
135
+
136
+ # Structured Inputs: makes sure you are sending only valid prompt inputs to your model
137
+ input do
138
+ const :sentence, String, description: 'The sentence to analyze'
139
+ end
140
+
141
+ # Structured Outputs: your predictor will validate the output of the model too.
142
+ output do
143
+ const :sentiment, Sentiment, description: 'The sentiment of the sentence'
144
+ const :confidence, Float, description: 'A number between 0.0 and 1.0'
145
+ end
146
+ end
147
+
148
+ # Wire it to the simplest prompting technique - a Predictn.
149
+ classify = DSPy::Predict.new(Classify)
150
+ # it may raise an error if you mess the inputs or your LLM messes the outputs.
151
+ result = classify.call(sentence: "This book was super fun to read!")
152
+
153
+ puts result.sentiment # => #<Sentiment::Positive>
154
+ puts result.confidence # => 0.85
155
+ ```
156
+
157
+ ### Access to 200+ Models Across 5 Providers
158
+
159
+ DSPy.rb provides unified access to major LLM providers with provider-specific optimizations:
160
+
161
+ ```ruby
162
+ # OpenAI (GPT-4, GPT-4o, GPT-4o-mini, GPT-5, etc.)
163
+ DSPy.configure do |c|
164
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
165
+ api_key: ENV['OPENAI_API_KEY'],
166
+ structured_outputs: true) # Native JSON mode
167
+ end
168
+
169
+ # Google Gemini (Gemini 1.5 Pro, Flash, Gemini 2.0, etc.)
170
+ DSPy.configure do |c|
171
+ c.lm = DSPy::LM.new('gemini/gemini-2.5-flash',
172
+ api_key: ENV['GEMINI_API_KEY'],
173
+ structured_outputs: true) # Native structured outputs
174
+ end
175
+
176
+ # Anthropic Claude (Claude 3.5, Claude 4, etc.)
177
+ DSPy.configure do |c|
178
+ c.lm = DSPy::LM.new('anthropic/claude-sonnet-4-5-20250929',
179
+ api_key: ENV['ANTHROPIC_API_KEY'],
180
+ structured_outputs: true) # Tool-based extraction (default)
181
+ end
182
+
183
+ # Ollama - Run any local model (Llama, Mistral, Gemma, etc.)
184
+ DSPy.configure do |c|
185
+ c.lm = DSPy::LM.new('ollama/llama3.2') # Free, runs locally, no API key needed
186
+ end
187
+
188
+ # OpenRouter - Access to 200+ models from multiple providers
189
+ DSPy.configure do |c|
190
+ c.lm = DSPy::LM.new('openrouter/deepseek/deepseek-chat-v3.1:free',
191
+ api_key: ENV['OPENROUTER_API_KEY'])
192
+ end
193
+ ```
194
+
195
+ ## What You Get
196
+
197
+ **Developer Experience:**
198
+ - LLM provider support using official Ruby clients:
199
+ - [OpenAI Ruby](https://github.com/openai/openai-ruby) with vision model support
200
+ - [Anthropic Ruby SDK](https://github.com/anthropics/anthropic-sdk-ruby) with multimodal capabilities
201
+ - [Google Gemini API](https://ai.google.dev/) with native structured outputs
202
+ - [Ollama](https://ollama.com/) via OpenAI compatibility layer for local models
203
+ - **Multimodal Support** - Complete image analysis with DSPy::Image, type-safe bounding boxes, vision-capable models
204
+ - Runtime type checking with [Sorbet](https://sorbet.org/) including T::Enum and union types
205
+ - Type-safe tool definitions for ReAct agents
206
+ - Comprehensive instrumentation and observability
207
+
208
+ **Core Building Blocks:**
209
+ - **Signatures** - Define input/output schemas using Sorbet types with T::Enum and union type support
210
+ - **Predict** - LLM completion with structured data extraction and multimodal support
211
+ - **Chain of Thought** - Step-by-step reasoning for complex problems with automatic prompt optimization
212
+ - **ReAct** - Tool-using agents with type-safe tool definitions and error recovery
213
+ - **Module Composition** - Combine multiple LLM calls into production-ready workflows
214
+
215
+ **Optimization & Evaluation:**
216
+ - **Prompt Objects** - Manipulate prompts as first-class objects instead of strings
217
+ - **Typed Examples** - Type-safe training data with automatic validation
218
+ - **Evaluation Framework** - Advanced metrics beyond simple accuracy with error-resilient pipelines
219
+ - **MIPROv2 Optimization** - Advanced Bayesian optimization with Gaussian Processes, multiple optimization strategies, auto-config presets, and storage persistence
220
+
221
+ **Production Features:**
222
+ - **Reliable JSON Extraction** - Native structured outputs for OpenAI and Gemini, Anthropic tool-based extraction, and automatic strategy selection with fallback
223
+ - **Type-Safe Configuration** - Strategy enums with automatic provider optimization (Strict/Compatible modes)
224
+ - **Smart Retry Logic** - Progressive fallback with exponential backoff for handling transient failures
225
+ - **Zero-Config Langfuse Integration** - Set env vars and get automatic OpenTelemetry traces in Langfuse
226
+ - **Performance Caching** - Schema and capability caching for faster repeated operations
227
+ - **File-based Storage** - Optimization result persistence with versioning
228
+ - **Structured Logging** - JSON and key=value formats with span tracking
229
+
230
+ ## Recent Achievements
231
+
232
+ DSPy.rb has rapidly evolved from experimental to production-ready:
233
+
234
+ ### Foundation
235
+ - ✅ **JSON Parsing Reliability** - Native OpenAI structured outputs with adaptive retry logic and schema-aware fallbacks
236
+ - ✅ **Type-Safe Strategy Configuration** - Provider-optimized strategy selection and enum-backed optimizer presets
237
+ - ✅ **Core Module System** - Predict, ChainOfThought, ReAct with type safety (add `dspy-code_act` for Think-Code-Observe agents)
238
+ - ✅ **Production Observability** - OpenTelemetry, New Relic, and Langfuse integration
239
+ - ✅ **Advanced Optimization** - MIPROv2 with Bayesian optimization, Gaussian Processes, and multi-mode search
240
+
241
+ ### Recent Advances
242
+ - ✅ **MIPROv2 ADE Integrity (v0.29.1)** - Stratified train/val/test splits, honest precision accounting, and enum-driven `--auto` presets with integration coverage
243
+ - ✅ **Instruction Deduplication (v0.29.1)** - Candidate generation now filters repeated programs so optimization logs highlight unique strategies
244
+ - ✅ **GEPA Teleprompter (v0.29.0)** - Genetic-Pareto reflective prompt evolution with merge proposer scheduling, reflective mutation, and ADE demo parity
245
+ - ✅ **Optimizer Utilities Parity (v0.29.0)** - Bootstrap strategies, dataset summaries, and Layer 3 utilities unlock multi-predictor programs on Ruby
246
+ - ✅ **Observability Hardening (v0.29.0)** - OTLP exporter runs on a single-thread executor preventing frozen SSL contexts without blocking spans
247
+ - ✅ **Documentation Refresh (v0.29.x)** - New GEPA guide plus ADE optimization docs covering presets, stratified splits, and error-handling defaults
248
+
249
+ **Current Focus Areas:**
250
+
251
+ ### Production Readiness
252
+ - 🚧 **Production Patterns** - Real-world usage validation and performance optimization
253
+ - 🚧 **Ruby Ecosystem Integration** - Rails integration, Sidekiq compatibility, deployment patterns
254
+
255
+ ### Community & Adoption
256
+ - 🚧 **Community Examples** - Real-world applications and case studies
257
+ - 🚧 **Contributor Experience** - Making it easier to contribute and extend
258
+ - 🚧 **Performance Benchmarks** - Comparative analysis vs other frameworks
259
+
260
+ **v1.0 Philosophy:**
261
+ v1.0 will be released after extensive production battle-testing, not after checking off features.
262
+ The API is already stable - v1.0 represents confidence in production reliability backed by real-world validation.
263
+
264
+
265
+ ## Documentation
266
+
267
+ 📖 **[Complete Documentation Website](https://vicentereig.github.io/dspy.rb/)**
268
+
269
+ ### LLM-Friendly Documentation
270
+
271
+ For LLMs and AI assistants working with DSPy.rb:
272
+ - **[llms.txt](https://vicentereig.github.io/dspy.rb/llms.txt)** - Concise reference optimized for LLMs
273
+ - **[llms-full.txt](https://vicentereig.github.io/dspy.rb/llms-full.txt)** - Comprehensive API documentation
274
+
275
+ ### Getting Started
276
+ - **[Installation & Setup](docs/src/getting-started/installation.md)** - Detailed installation and configuration
277
+ - **[Quick Start Guide](docs/src/getting-started/quick-start.md)** - Your first DSPy programs
278
+ - **[Core Concepts](docs/src/getting-started/core-concepts.md)** - Understanding signatures, predictors, and modules
279
+
280
+ ### Prompt Engineering
281
+ - **[Signatures & Types](docs/src/core-concepts/signatures.md)** - Define typed interfaces for LLM operations
282
+ - **[Predictors](docs/src/core-concepts/predictors.md)** - Predict, ChainOfThought, ReAct, and more
283
+ - **[Modules & Pipelines](docs/src/core-concepts/modules.md)** - Compose complex multi-stage workflows
284
+ - **[Multimodal Support](docs/src/core-concepts/multimodal.md)** - Image analysis with vision-capable models
285
+ - **[Examples & Validation](docs/src/core-concepts/examples.md)** - Type-safe training data
286
+ - **[Rich Types](docs/src/advanced/complex-types.md)** - Sorbet type integration with automatic coercion for structs, enums, and arrays
287
+ - **[Composable Pipelines](docs/src/advanced/pipelines.md)** - Manual module composition patterns
288
+
289
+ ### Prompt Optimization
290
+ - **[Evaluation Framework](docs/src/optimization/evaluation.md)** - Advanced metrics beyond simple accuracy
291
+ - **[Prompt Optimization](docs/src/optimization/prompt-optimization.md)** - Manipulate prompts as objects
292
+ - **[MIPROv2 Optimizer](docs/src/optimization/miprov2.md)** - Advanced Bayesian optimization with Gaussian Processes
293
+ - **[GEPA Optimizer](docs/src/optimization/gepa.md)** *(beta)* - Reflective mutation with optional reflection LMs
294
+
295
+ ### Context Engineering
296
+ - **[Tools](docs/src/core-concepts/toolsets.md)** - Tool wieldint agents.
297
+ - **[Agentic Memory](docs/src/core-concepts/memory.md)** - Memory Tools & Agentic Loops
298
+ - **[RAG Patterns](docs/src/advanced/rag.md)** - Manual RAG implementation with external services
299
+
300
+ ### Production Features
301
+ - **[Observability](docs/src/production/observability.md)** - Zero-config Langfuse integration with a dedicated export worker that never blocks your LLMs
302
+ - **[Storage System](docs/src/production/storage.md)** - Persistence and optimization result storage
303
+ - **[Custom Metrics](docs/src/advanced/custom-metrics.md)** - Proc-based evaluation logic
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+ ## License
313
+ This project is licensed under the MIT License.
@@ -0,0 +1,5 @@
1
+ # DSPy::DeepResearch
2
+
3
+ Orchestration for DeepResearch agents built on DSPy::DeepSearch. Manages planners, section queues, and coherence passes.
4
+
5
+ Refer to ADR 015 for the staged implementation plan.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module DeepResearch
5
+ class Error < DSPy::Error; end
6
+ class EvidenceDeficitError < Error; end
7
+ class QueueStarvationError < Error; end
8
+ class SynthesisCoherenceError < Error; end
9
+ end
10
+ end