dspy-gemini 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: b2b48f3533914c6239c1c655e229bac9479b3cc072cb4ae2916d76aa2b168386
4
+ data.tar.gz: b2a95b2f9710eb5c5ca0a944afefdc8e3552c495b76194c22a8f501d05f257b0
5
+ SHA512:
6
+ metadata.gz: 3cbc0bc6a1482ed5168042e310e6135bcec8c17e533ee0fe86b3db70ca2aeff8efb8d5aec1fc05c27d70a2e47292d106802057b18cf374959ca37e9f545d33d3
7
+ data.tar.gz: 5bda92626cd9618697ca9f0cf993079258ae6add0df7f5bea7682d6c3e7031ae6a01fd6ba80d410b005d4086e21f010cc8dd39978960c38491f5ccebb4a85c9d
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,292 @@
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
+ [![Discord](https://img.shields.io/discord/1161519468141355160?label=discord&logo=discord&logoColor=white)](https://discord.gg/zWBhrMqn)
8
+
9
+ > [!NOTE]
10
+ > The core Prompt Engineering Framework is production-ready with
11
+ > comprehensive documentation. I am focusing now on educational content on systematic Prompt Optimization and Context Engineering.
12
+ > 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).
13
+ >
14
+ > If you want to contribute, feel free to reach out to me to coordinate efforts: hey at vicente.services
15
+ >
16
+ > And, yes, this is 100% a legit project. :)
17
+
18
+
19
+ **Build reliable LLM applications in idiomatic Ruby using composable, type-safe modules.**
20
+
21
+ DSPy.rb is the Ruby-first surgical port of Stanford's [DSPy framework](https://github.com/stanfordnlp/dspy). It delivers structured LLM programming, prompt engineering, and context engineering in the language we love. Instead of wrestling with brittle prompt strings, you define typed signatures in idiomatic Ruby and compose workflows and agents that actually behave.
22
+
23
+ **Prompts are just functions.** 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](https://dspy.ai/): define modular signatures and let the framework deal with the messy bits.
24
+
25
+ While we implement the same signatures, predictors, and optimization algorithms as the original library, DSPy.rb leans hard into Ruby conventions with Sorbet-based typing, ReAct loops, and production-ready integrations like non-blocking OpenTelemetry instrumentation.
26
+
27
+ **What you get?** Ruby LLM applications that scale and don't break when you sneeze.
28
+
29
+ Check the [examples](examples/) and take them for a spin!
30
+
31
+ ## Your First DSPy Program
32
+ ### Installation
33
+
34
+ Add to your Gemfile:
35
+
36
+ ```ruby
37
+ gem 'dspy'
38
+ ```
39
+
40
+ and
41
+
42
+ ```bash
43
+ bundle install
44
+ ```
45
+
46
+ ### Your First Reliable Predictor
47
+
48
+ ```ruby
49
+ require 'dspy'
50
+
51
+ # Configure DSPy globally to use your fave LLM (you can override per predictor).
52
+ DSPy.configure do |c|
53
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
54
+ api_key: ENV['OPENAI_API_KEY'],
55
+ structured_outputs: true) # Enable OpenAI's native JSON mode
56
+ end
57
+
58
+ # Define a signature for sentiment classification - instead of writing a full prompt!
59
+ class Classify < DSPy::Signature
60
+ description "Classify sentiment of a given sentence." # sets the goal of the underlying prompt
61
+
62
+ class Sentiment < T::Enum
63
+ enums do
64
+ Positive = new('positive')
65
+ Negative = new('negative')
66
+ Neutral = new('neutral')
67
+ end
68
+ end
69
+
70
+ # Structured Inputs: makes sure you are sending only valid prompt inputs to your model
71
+ input do
72
+ const :sentence, String, description: 'The sentence to analyze'
73
+ end
74
+
75
+ # Structured Outputs: your predictor will validate the output of the model too.
76
+ output do
77
+ const :sentiment, Sentiment, description: 'The sentiment of the sentence'
78
+ const :confidence, Float, description: 'A number between 0.0 and 1.0'
79
+ end
80
+ end
81
+
82
+ # Wire it to the simplest prompting technique: a prediction loop.
83
+ classify = DSPy::Predict.new(Classify)
84
+ # it may raise an error if you mess the inputs or your LLM messes the outputs.
85
+ result = classify.call(sentence: "This book was super fun to read!")
86
+
87
+ puts result.sentiment # => #<Sentiment::Positive>
88
+ puts result.confidence # => 0.85
89
+ ```
90
+
91
+ Save this as `examples/first_predictor.rb` and run it with:
92
+
93
+ ```bash
94
+ bundle exec ruby examples/first_predictor.rb
95
+ ```
96
+
97
+ ### Sibling Gems
98
+
99
+ 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`:
100
+
101
+ | Gem | Description | Status |
102
+ | --- | --- | --- |
103
+ | `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) |
104
+ | `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) |
105
+ | `dspy-datasets` | Dataset helpers plus Parquet/Polars tooling for richer evaluation corpora. (Toggle via `DSPY_WITH_DATASETS`.) | **Stable** (v1.0.0) |
106
+ | `dspy-evals` | High-throughput evaluation harness with metrics, callbacks, and regression fixtures. (Toggle via `DSPY_WITH_EVALS`.) | **Stable** (v1.0.0) |
107
+ | `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) |
108
+ | `dspy-gepa` | `DSPy::Teleprompt::GEPA`, reflection loops, experiment tracking, telemetry adapters. (Install or set `DSPY_WITH_GEPA=1`.) | **Stable** (v1.0.0) |
109
+ | `gepa` | GEPA optimizer core (Pareto engine, telemetry, reflective proposer). | **Stable** (v1.0.0) |
110
+ | `dspy-o11y` | Core observability APIs: `DSPy::Observability`, async span processor, observation types. (Install or set `DSPY_WITH_O11Y=1`.) | **Stable** (v1.0.0) |
111
+ | `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) |
112
+ | `dspy-deep_search` | Production DeepSearch loop with Exa-backed search/read, token budgeting, and instrumentation (Issue #163). | **Stable** (v1.0.0) |
113
+ | `dspy-deep_research` | Planner/QA orchestration atop DeepSearch plus the memory supervisor used by the CLI example. | **Stable** (v1.0.0) |
114
+
115
+ 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.
116
+ ### Access to 200+ Models Across 5 Providers
117
+
118
+ DSPy.rb provides unified access to major LLM providers with provider-specific optimizations:
119
+
120
+ ```ruby
121
+ # OpenAI (GPT-4, GPT-4o, GPT-4o-mini, GPT-5, etc.)
122
+ DSPy.configure do |c|
123
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
124
+ api_key: ENV['OPENAI_API_KEY'],
125
+ structured_outputs: true) # Native JSON mode
126
+ end
127
+
128
+ # Google Gemini (Gemini 1.5 Pro, Flash, Gemini 2.0, etc.)
129
+ DSPy.configure do |c|
130
+ c.lm = DSPy::LM.new('gemini/gemini-2.5-flash',
131
+ api_key: ENV['GEMINI_API_KEY'],
132
+ structured_outputs: true) # Native structured outputs
133
+ end
134
+
135
+ # Anthropic Claude (Claude 3.5, Claude 4, etc.)
136
+ DSPy.configure do |c|
137
+ c.lm = DSPy::LM.new('anthropic/claude-sonnet-4-5-20250929',
138
+ api_key: ENV['ANTHROPIC_API_KEY'],
139
+ structured_outputs: true) # Tool-based extraction (default)
140
+ end
141
+
142
+ # Ollama - Run any local model (Llama, Mistral, Gemma, etc.)
143
+ DSPy.configure do |c|
144
+ c.lm = DSPy::LM.new('ollama/llama3.2') # Free, runs locally, no API key needed
145
+ end
146
+
147
+ # OpenRouter - Access to 200+ models from multiple providers
148
+ DSPy.configure do |c|
149
+ c.lm = DSPy::LM.new('openrouter/deepseek/deepseek-chat-v3.1:free',
150
+ api_key: ENV['OPENROUTER_API_KEY'])
151
+ end
152
+ ```
153
+
154
+ ## What You Get
155
+
156
+ **Developer Experience:** Official clients, multimodal coverage, and observability baked in.
157
+ <details>
158
+ <summary>Expand for everything included</summary>
159
+
160
+ - LLM provider support using official Ruby clients:
161
+ - [OpenAI Ruby](https://github.com/openai/openai-ruby) with vision model support
162
+ - [Anthropic Ruby SDK](https://github.com/anthropics/anthropic-sdk-ruby) with multimodal capabilities
163
+ - [Google Gemini API](https://ai.google.dev/) with native structured outputs
164
+ - [Ollama](https://ollama.com/) via OpenAI compatibility layer for local models
165
+ - **Multimodal Support** - Complete image analysis with DSPy::Image, type-safe bounding boxes, vision-capable models
166
+ - Runtime type checking with [Sorbet](https://sorbet.org/) including T::Enum and union types
167
+ - Type-safe tool definitions for ReAct agents
168
+ - Comprehensive instrumentation and observability
169
+ </details>
170
+
171
+ **Core Building Blocks:** Predictors, agents, and pipelines wired through type-safe signatures.
172
+ <details>
173
+ <summary>Expand for everything included</summary>
174
+
175
+ - **Signatures** - Define input/output schemas using Sorbet types with T::Enum and union type support
176
+ - **Predict** - LLM completion with structured data extraction and multimodal support
177
+ - **Chain of Thought** - Step-by-step reasoning for complex problems with automatic prompt optimization
178
+ - **ReAct** - Tool-using agents with type-safe tool definitions and error recovery
179
+ - **Module Composition** - Combine multiple LLM calls into production-ready workflows
180
+ </details>
181
+
182
+ **Optimization & Evaluation:** Treat prompt optimization like a real ML workflow.
183
+ <details>
184
+ <summary>Expand for everything included</summary>
185
+
186
+ - **Prompt Objects** - Manipulate prompts as first-class objects instead of strings
187
+ - **Typed Examples** - Type-safe training data with automatic validation
188
+ - **Evaluation Framework** - Advanced metrics beyond simple accuracy with error-resilient pipelines
189
+ - **MIPROv2 Optimization** - Advanced Bayesian optimization with Gaussian Processes, multiple optimization strategies, auto-config presets, and storage persistence
190
+ </details>
191
+
192
+ **Production Features:** Hardened behaviors for teams shipping actual products.
193
+ <details>
194
+ <summary>Expand for everything included</summary>
195
+
196
+ - **Reliable JSON Extraction** - Native structured outputs for OpenAI and Gemini, Anthropic tool-based extraction, and automatic strategy selection with fallback
197
+ - **Type-Safe Configuration** - Strategy enums with automatic provider optimization (Strict/Compatible modes)
198
+ - **Smart Retry Logic** - Progressive fallback with exponential backoff for handling transient failures
199
+ - **Zero-Config Langfuse Integration** - Set env vars and get automatic OpenTelemetry traces in Langfuse
200
+ - **Performance Caching** - Schema and capability caching for faster repeated operations
201
+ - **File-based Storage** - Optimization result persistence with versioning
202
+ - **Structured Logging** - JSON and key=value formats with span tracking
203
+ </details>
204
+
205
+ ## Recent Achievements
206
+
207
+ DSPy.rb has gone from experimental to production-ready in three fast releases.
208
+ <details>
209
+ <summary>Expand for the full changelog highlights</summary>
210
+
211
+ ### Foundation
212
+ - ✅ **JSON Parsing Reliability** - Native OpenAI structured outputs with adaptive retry logic and schema-aware fallbacks
213
+ - ✅ **Type-Safe Strategy Configuration** - Provider-optimized strategy selection and enum-backed optimizer presets
214
+ - ✅ **Core Module System** - Predict, ChainOfThought, ReAct with type safety (add `dspy-code_act` for Think-Code-Observe agents)
215
+ - ✅ **Production Observability** - OpenTelemetry, New Relic, and Langfuse integration
216
+ - ✅ **Advanced Optimization** - MIPROv2 with Bayesian optimization, Gaussian Processes, and multi-mode search
217
+
218
+ ### Recent Advances
219
+ - ✅ **MIPROv2 ADE Integrity (v0.29.1)** - Stratified train/val/test splits, honest precision accounting, and enum-driven `--auto` presets with integration coverage
220
+ - ✅ **Instruction Deduplication (v0.29.1)** - Candidate generation now filters repeated programs so optimization logs highlight unique strategies
221
+ - ✅ **GEPA Teleprompter (v0.29.0)** - Genetic-Pareto reflective prompt evolution with merge proposer scheduling, reflective mutation, and ADE demo parity
222
+ - ✅ **Optimizer Utilities Parity (v0.29.0)** - Bootstrap strategies, dataset summaries, and Layer 3 utilities unlock multi-predictor programs on Ruby
223
+ - ✅ **Observability Hardening (v0.29.0)** - OTLP exporter runs on a single-thread executor preventing frozen SSL contexts without blocking spans
224
+ - ✅ **Documentation Refresh (v0.29.x)** - New GEPA guide plus ADE optimization docs covering presets, stratified splits, and error-handling defaults
225
+ </details>
226
+
227
+ **Current Focus Areas:** Closing the loop on production patterns and community adoption ahead of v1.0.
228
+ <details>
229
+ <summary>Expand for the roadmap</summary>
230
+
231
+ ### Production Readiness
232
+ - 🚧 **Production Patterns** - Real-world usage validation and performance optimization
233
+ - 🚧 **Ruby Ecosystem Integration** - Rails integration, Sidekiq compatibility, deployment patterns
234
+
235
+ ### Community & Adoption
236
+ - 🚧 **Community Examples** - Real-world applications and case studies
237
+ - 🚧 **Contributor Experience** - Making it easier to contribute and extend
238
+ - 🚧 **Performance Benchmarks** - Comparative analysis vs other frameworks
239
+ </details>
240
+
241
+ **v1.0 Philosophy:** v1.0 lands after battle-testing, not checkbox bingo. The API is already stable; the milestone marks production confidence.
242
+
243
+
244
+ ## Documentation
245
+
246
+ 📖 **[Complete Documentation Website](https://vicentereig.github.io/dspy.rb/)**
247
+
248
+ ### LLM-Friendly Documentation
249
+
250
+ For LLMs and AI assistants working with DSPy.rb:
251
+ - **[llms.txt](https://vicentereig.github.io/dspy.rb/llms.txt)** - Concise reference optimized for LLMs
252
+ - **[llms-full.txt](https://vicentereig.github.io/dspy.rb/llms-full.txt)** - Comprehensive API documentation
253
+
254
+ ### Getting Started
255
+ - **[Installation & Setup](docs/src/getting-started/installation.md)** - Detailed installation and configuration
256
+ - **[Quick Start Guide](docs/src/getting-started/quick-start.md)** - Your first DSPy programs
257
+ - **[Core Concepts](docs/src/getting-started/core-concepts.md)** - Understanding signatures, predictors, and modules
258
+
259
+ ### Prompt Engineering
260
+ - **[Signatures & Types](docs/src/core-concepts/signatures.md)** - Define typed interfaces for LLM operations
261
+ - **[Predictors](docs/src/core-concepts/predictors.md)** - Predict, ChainOfThought, ReAct, and more
262
+ - **[Modules & Pipelines](docs/src/core-concepts/modules.md)** - Compose complex multi-stage workflows
263
+ - **[Multimodal Support](docs/src/core-concepts/multimodal.md)** - Image analysis with vision-capable models
264
+ - **[Examples & Validation](docs/src/core-concepts/examples.md)** - Type-safe training data
265
+ - **[Rich Types](docs/src/advanced/complex-types.md)** - Sorbet type integration with automatic coercion for structs, enums, and arrays
266
+ - **[Composable Pipelines](docs/src/advanced/pipelines.md)** - Manual module composition patterns
267
+
268
+ ### Prompt Optimization
269
+ - **[Evaluation Framework](docs/src/optimization/evaluation.md)** - Advanced metrics beyond simple accuracy
270
+ - **[Prompt Optimization](docs/src/optimization/prompt-optimization.md)** - Manipulate prompts as objects
271
+ - **[MIPROv2 Optimizer](docs/src/optimization/miprov2.md)** - Advanced Bayesian optimization with Gaussian Processes
272
+ - **[GEPA Optimizer](docs/src/optimization/gepa.md)** *(beta)* - Reflective mutation with optional reflection LMs
273
+
274
+ ### Context Engineering
275
+ - **[Tools](docs/src/core-concepts/toolsets.md)** - Tool wieldint agents.
276
+ - **[Agentic Memory](docs/src/core-concepts/memory.md)** - Memory Tools & Agentic Loops
277
+ - **[RAG Patterns](docs/src/advanced/rag.md)** - Manual RAG implementation with external services
278
+
279
+ ### Production Features
280
+ - **[Observability](docs/src/production/observability.md)** - Zero-config Langfuse integration with a dedicated export worker that never blocks your LLMs
281
+ - **[Storage System](docs/src/production/storage.md)** - Persistence and optimization result storage
282
+ - **[Custom Metrics](docs/src/advanced/custom-metrics.md)** - Proc-based evaluation logic
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+ ## License
292
+ This project is licensed under the MIT License.
@@ -0,0 +1,37 @@
1
+ # DSPy Gemini adapter gem
2
+
3
+ `dspy-gemini` provides the Gemini adapter for DSPy.rb so we can rely on Google's API without bloating the core gem. Install it whenever you plan to call `gemini/*` model ids from DSPy.
4
+
5
+ ## When you need it
6
+ - You call `DSPy::LM.new` with a provider of `gemini`.
7
+ - You want structured outputs, multimodal prompts, or streaming responses backed by Gemini's Generative Language API.
8
+
9
+ Projects that only target OpenAI, Anthropic, or other providers can skip this gem.
10
+
11
+ ## Installation
12
+ Add it beside `dspy` and install dependencies:
13
+
14
+ ```ruby
15
+ # Gemfile
16
+ gem 'dspy'
17
+ gem 'dspy-gemini'
18
+ ```
19
+
20
+ ```sh
21
+ bundle install
22
+ ```
23
+
24
+ The adapter enforces `gemini-ai ~> 4.3` at runtime (and raises if the dependency is missing or out of range).
25
+
26
+ ## Configuration
27
+ - Set `ENV['GEMINI_API_KEY']`, or pass `api_key:` directly.
28
+
29
+ ## Basic usage
30
+
31
+ ```ruby
32
+ require 'dspy'
33
+ # No need to explicitly require 'dspy/gemini'
34
+
35
+ lm = DSPy::LM.new('gemini/gemini-1.5-flash', api_key: ENV.fetch('GEMINI_API_KEY'))
36
+
37
+ ```
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dspy/lm/errors'
4
+
5
+ module DSPy
6
+ module Gemini
7
+ class Guardrails
8
+ SUPPORTED_GEMINI_VERSIONS = "~> 4.3".freeze
9
+
10
+ def self.ensure_gemini_installed!
11
+ require 'gemini-ai'
12
+
13
+ spec = Gem.loaded_specs["gemini-ai"]
14
+ unless spec && Gem::Requirement.new(SUPPORTED_GEMINI_VERSIONS).satisfied_by?(spec.version)
15
+ msg = <<~MSG
16
+ DSPY requires `gemini-ai` gem #{SUPPORTED_GEMINI_VERSIONS}.
17
+ Please Install or upgrade it with `bundle add gemini-ai --version "#{SUPPORTED_GEMINI_VERSIONS}"`.
18
+ MSG
19
+ raise DSPy::LM::UnsupportedVersionError, msg
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'gemini-ai'
4
+ require 'json'
5
+ require 'dspy/lm/vision_models'
6
+ require 'dspy/lm/adapter'
7
+
8
+ require 'dspy/gemini/guardrails'
9
+ DSPy::Gemini::Guardrails.ensure_gemini_installed!
10
+
11
+ module DSPy
12
+ module Gemini
13
+ module LM
14
+ module Adapters
15
+ class GeminiAdapter < DSPy::LM::Adapter
16
+ def initialize(model:, api_key:, structured_outputs: false)
17
+ super(model: model, api_key: api_key)
18
+ validate_api_key!(api_key, 'gemini')
19
+
20
+ @structured_outputs_enabled = structured_outputs
21
+
22
+ # Disable streaming for VCR tests since SSE responses don't record properly
23
+ # But keep streaming enabled for SSEVCR tests (SSE-specific cassettes)
24
+ @use_streaming = true
25
+ begin
26
+ vcr_active = defined?(VCR) && VCR.current_cassette
27
+ ssevcr_active = defined?(SSEVCR) && SSEVCR.turned_on?
28
+
29
+ # Only disable streaming if regular VCR is active but SSEVCR is not
30
+ @use_streaming = false if vcr_active && !ssevcr_active
31
+ rescue
32
+ # If VCR/SSEVCR is not available or any error occurs, use streaming
33
+ @use_streaming = true
34
+ end
35
+
36
+ @client = ::Gemini.new(
37
+ credentials: {
38
+ service: 'generative-language-api',
39
+ api_key: api_key,
40
+ version: 'v1beta' # Use beta API version for structured outputs support
41
+ },
42
+ options: {
43
+ model: model,
44
+ server_sent_events: @use_streaming
45
+ }
46
+ )
47
+ end
48
+
49
+ def chat(messages:, signature: nil, **extra_params, &block)
50
+ normalized_messages = normalize_messages(messages)
51
+
52
+ # Validate vision support if images are present
53
+ if contains_images?(normalized_messages)
54
+ DSPy::LM::VisionModels.validate_vision_support!('gemini', model)
55
+ # Convert messages to Gemini format with proper image handling
56
+ normalized_messages = format_multimodal_messages(normalized_messages)
57
+ end
58
+
59
+ # Convert DSPy message format to Gemini format
60
+ gemini_messages = convert_messages_to_gemini_format(normalized_messages)
61
+
62
+ request_params = {
63
+ contents: gemini_messages
64
+ }.merge(extra_params)
65
+
66
+ begin
67
+ content = ""
68
+ final_response_data = nil
69
+
70
+ # Check if we're using streaming or not
71
+ if @use_streaming
72
+ # Streaming mode
73
+ @client.stream_generate_content(request_params) do |chunk|
74
+ # Handle case where chunk might be a string (from SSE VCR)
75
+ if chunk.is_a?(String)
76
+ begin
77
+ chunk = JSON.parse(chunk)
78
+ rescue JSON::ParserError => e
79
+ raise DSPy::LM::AdapterError, "Failed to parse Gemini streaming response: #{e.message}"
80
+ end
81
+ end
82
+
83
+ # Extract content from chunks
84
+ if chunk.dig('candidates', 0, 'content', 'parts')
85
+ chunk_text = extract_text_from_parts(chunk.dig('candidates', 0, 'content', 'parts'))
86
+ content += chunk_text
87
+
88
+ # Call block only if provided (for real streaming)
89
+ block.call(chunk) if block_given?
90
+ end
91
+
92
+ # Store final response data (usage, metadata) from last chunk
93
+ if chunk['usageMetadata'] || chunk.dig('candidates', 0, 'finishReason')
94
+ final_response_data = chunk
95
+ end
96
+ end
97
+ else
98
+ # Non-streaming mode (for VCR tests)
99
+ response = @client.generate_content(request_params)
100
+
101
+ # Extract content from single response
102
+ if response.dig('candidates', 0, 'content', 'parts')
103
+ content = extract_text_from_parts(response.dig('candidates', 0, 'content', 'parts'))
104
+ end
105
+
106
+ # Use response as final data
107
+ final_response_data = response
108
+ end
109
+
110
+ # Extract usage information from final chunk
111
+ usage_data = final_response_data&.dig('usageMetadata')
112
+ usage_struct = usage_data ? DSPy::LM::UsageFactory.create('gemini', usage_data) : nil
113
+
114
+ # Create metadata from final chunk
115
+ metadata = {
116
+ provider: 'gemini',
117
+ model: model,
118
+ finish_reason: final_response_data&.dig('candidates', 0, 'finishReason'),
119
+ safety_ratings: final_response_data&.dig('candidates', 0, 'safetyRatings'),
120
+ streaming: block_given?
121
+ }
122
+
123
+ # Create typed metadata
124
+ typed_metadata = DSPy::LM::ResponseMetadataFactory.create('gemini', metadata)
125
+
126
+ DSPy::LM::Response.new(
127
+ content: content,
128
+ usage: usage_struct,
129
+ metadata: typed_metadata
130
+ )
131
+ rescue => e
132
+ handle_gemini_error(e)
133
+ end
134
+ end
135
+
136
+ private
137
+
138
+ # Convert DSPy message format to Gemini format
139
+ def convert_messages_to_gemini_format(messages)
140
+ # Gemini expects contents array with role and parts
141
+ messages.map do |msg|
142
+ role = case msg[:role]
143
+ when 'system'
144
+ 'user' # Gemini doesn't have explicit system role, merge with user
145
+ when 'assistant'
146
+ 'model'
147
+ else
148
+ msg[:role]
149
+ end
150
+
151
+ if msg[:content].is_a?(Array)
152
+ # Multimodal content
153
+ parts = msg[:content].map do |item|
154
+ case item[:type]
155
+ when 'text'
156
+ { text: item[:text] }
157
+ when 'image'
158
+ item[:image].to_gemini_format
159
+ else
160
+ item
161
+ end
162
+ end
163
+
164
+ { role: role, parts: parts }
165
+ else
166
+ # Text-only content
167
+ { role: role, parts: [{ text: msg[:content] }] }
168
+ end
169
+ end
170
+ end
171
+
172
+ # Extract text content from Gemini parts array
173
+ def extract_text_from_parts(parts)
174
+ return "" unless parts.is_a?(Array)
175
+
176
+ parts.map { |part| part['text'] }.compact.join
177
+ end
178
+
179
+ # Format multimodal messages for Gemini
180
+ def format_multimodal_messages(messages)
181
+ messages.map do |msg|
182
+ if msg[:content].is_a?(Array)
183
+ # Convert multimodal content to Gemini format
184
+ formatted_content = msg[:content].map do |item|
185
+ case item[:type]
186
+ when 'text'
187
+ { type: 'text', text: item[:text] }
188
+ when 'image'
189
+ # Validate image compatibility before formatting
190
+ item[:image].validate_for_provider!('gemini')
191
+ item[:image].to_gemini_format
192
+ else
193
+ item
194
+ end
195
+ end
196
+
197
+ {
198
+ role: msg[:role],
199
+ content: formatted_content
200
+ }
201
+ else
202
+ msg
203
+ end
204
+ end
205
+ end
206
+
207
+ # Handle Gemini-specific errors
208
+ def handle_gemini_error(error)
209
+ error_msg = error.message.to_s
210
+
211
+ if error_msg.include?('API_KEY') || error_msg.include?('status 400') || error_msg.include?('status 401') || error_msg.include?('status 403')
212
+ raise DSPy::LM::AdapterError, "Gemini authentication failed: #{error_msg}. Check your API key."
213
+ elsif error_msg.include?('RATE_LIMIT') || error_msg.downcase.include?('quota') || error_msg.include?('status 429')
214
+ raise DSPy::LM::AdapterError, "Gemini rate limit exceeded: #{error_msg}. Please wait and try again."
215
+ elsif error_msg.include?('SAFETY') || error_msg.include?('blocked')
216
+ raise DSPy::LM::AdapterError, "Gemini content was blocked by safety filters: #{error_msg}"
217
+ elsif error_msg.include?('image') || error_msg.include?('media')
218
+ raise DSPy::LM::AdapterError, "Gemini image processing failed: #{error_msg}. Ensure your image is a valid format and under size limits."
219
+ else
220
+ # Generic error handling
221
+ raise DSPy::LM::AdapterError, "Gemini adapter error: #{error_msg}"
222
+ end
223
+ end
224
+ end
225
+ end
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sorbet-runtime"
4
+
5
+ module DSPy
6
+ module Gemini
7
+ module LM
8
+ # Converts DSPy signatures to Gemini structured output format
9
+ class SchemaConverter
10
+ extend T::Sig
11
+
12
+ # Models that support structured outputs (JSON + Schema)
13
+ # Based on official Google documentation: https://ai.google.dev/gemini-api/docs/models/gemini
14
+ # Last updated: Oct 2025
15
+ # Note: Gemini 1.5 series deprecated Oct 2025
16
+ STRUCTURED_OUTPUT_MODELS = T.let([
17
+ # Gemini 2.0 series
18
+ "gemini-2.0-flash",
19
+ "gemini-2.0-flash-lite",
20
+ # Gemini 2.5 series (current)
21
+ "gemini-2.5-pro",
22
+ "gemini-2.5-flash",
23
+ "gemini-2.5-flash-lite",
24
+ "gemini-2.5-flash-image"
25
+ ].freeze, T::Array[String])
26
+
27
+ # Models that do not support structured outputs or are deprecated
28
+ UNSUPPORTED_MODELS = T.let([
29
+ # Legacy Gemini 1.0 series
30
+ "gemini-pro",
31
+ "gemini-1.0-pro-002",
32
+ "gemini-1.0-pro",
33
+ # Deprecated Gemini 1.5 series (removed Oct 2025)
34
+ "gemini-1.5-pro",
35
+ "gemini-1.5-pro-preview-0514",
36
+ "gemini-1.5-pro-preview-0409",
37
+ "gemini-1.5-flash",
38
+ "gemini-1.5-flash-8b"
39
+ ].freeze, T::Array[String])
40
+
41
+ sig { params(signature_class: T.class_of(DSPy::Signature)).returns(T::Hash[Symbol, T.untyped]) }
42
+ def self.to_gemini_format(signature_class)
43
+ # Get the output JSON schema from the signature class
44
+ output_schema = signature_class.output_json_schema
45
+
46
+ # Convert to Gemini format (OpenAPI 3.0 Schema subset - not related to OpenAI)
47
+ convert_dspy_schema_to_gemini(output_schema)
48
+ end
49
+
50
+ sig { params(model: String).returns(T::Boolean) }
51
+ def self.supports_structured_outputs?(model)
52
+ # Extract base model name without provider prefix
53
+ base_model = model.sub(/^gemini\//, "")
54
+
55
+ # Check if it's a supported model or a newer version
56
+ STRUCTURED_OUTPUT_MODELS.any? { |supported| base_model.start_with?(supported) }
57
+ end
58
+
59
+ sig { params(schema: T::Hash[Symbol, T.untyped]).returns(T::Array[String]) }
60
+ def self.validate_compatibility(schema)
61
+ issues = []
62
+
63
+ # Check for deeply nested objects (Gemini has depth limits)
64
+ depth = calculate_depth(schema)
65
+ if depth > 5
66
+ issues << "Schema depth (#{depth}) exceeds recommended limit of 5 levels"
67
+ end
68
+
69
+ issues
70
+ end
71
+
72
+ private
73
+
74
+ sig { params(dspy_schema: T::Hash[Symbol, T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
75
+ def self.convert_dspy_schema_to_gemini(dspy_schema)
76
+ # For Gemini's responseJsonSchema, we need pure JSON Schema format
77
+ # Remove OpenAPI-specific fields like "$schema"
78
+ result = {
79
+ type: "object",
80
+ properties: {},
81
+ required: []
82
+ }
83
+
84
+ # Convert properties
85
+ properties = dspy_schema[:properties] || {}
86
+ properties.each do |prop_name, prop_schema|
87
+ result[:properties][prop_name] = convert_property_to_gemini(prop_schema)
88
+ end
89
+
90
+ # Set required fields
91
+ result[:required] = (dspy_schema[:required] || []).map(&:to_s)
92
+
93
+ result
94
+ end
95
+
96
+ sig { params(property_schema: T::Hash[Symbol, T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
97
+ def self.convert_property_to_gemini(property_schema)
98
+ # Handle oneOf/anyOf schemas (union types) - Gemini supports these in responseJsonSchema
99
+ if property_schema[:oneOf]
100
+ return {
101
+ oneOf: property_schema[:oneOf].map { |schema| convert_property_to_gemini(schema) },
102
+ description: property_schema[:description]
103
+ }.compact
104
+ end
105
+
106
+ if property_schema[:anyOf]
107
+ return {
108
+ anyOf: property_schema[:anyOf].map { |schema| convert_property_to_gemini(schema) },
109
+ description: property_schema[:description]
110
+ }.compact
111
+ end
112
+
113
+ case property_schema[:type]
114
+ when "string"
115
+ result = { type: "string" }
116
+ # Gemini responseJsonSchema doesn't support const, so convert to single-value enum
117
+ # See: https://ai.google.dev/api/generate-content#FIELDS.response_json_schema
118
+ if property_schema[:const]
119
+ result[:enum] = [property_schema[:const]]
120
+ elsif property_schema[:enum]
121
+ result[:enum] = property_schema[:enum]
122
+ end
123
+ result
124
+ when "integer"
125
+ { type: "integer" }
126
+ when "number"
127
+ { type: "number" }
128
+ when "boolean"
129
+ { type: "boolean" }
130
+ when "array"
131
+ {
132
+ type: "array",
133
+ items: convert_property_to_gemini(property_schema[:items] || { type: "string" })
134
+ }
135
+ when "object"
136
+ result = { type: "object" }
137
+
138
+ if property_schema[:properties]
139
+ result[:properties] = {}
140
+ property_schema[:properties].each do |nested_prop, nested_schema|
141
+ result[:properties][nested_prop] = convert_property_to_gemini(nested_schema)
142
+ end
143
+
144
+ # Set required fields for nested objects
145
+ if property_schema[:required]
146
+ result[:required] = property_schema[:required].map(&:to_s)
147
+ end
148
+ end
149
+
150
+ result
151
+ else
152
+ # Default to string for unknown types
153
+ { type: "string" }
154
+ end
155
+ end
156
+
157
+ sig { params(schema: T::Hash[Symbol, T.untyped], current_depth: Integer).returns(Integer) }
158
+ def self.calculate_depth(schema, current_depth = 0)
159
+ return current_depth unless schema.is_a?(Hash)
160
+
161
+ max_depth = current_depth
162
+
163
+ # Check properties
164
+ if schema[:properties].is_a?(Hash)
165
+ schema[:properties].each_value do |prop|
166
+ if prop.is_a?(Hash)
167
+ prop_depth = calculate_depth(prop, current_depth + 1)
168
+ max_depth = [max_depth, prop_depth].max
169
+ end
170
+ end
171
+ end
172
+
173
+ # Check array items
174
+ if schema[:items].is_a?(Hash)
175
+ items_depth = calculate_depth(schema[:items], current_depth + 1)
176
+ max_depth = [max_depth, items_depth].max
177
+ end
178
+
179
+ max_depth
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module Gemini
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dspy/gemini/version'
4
+
5
+ require 'dspy/gemini/guardrails'
6
+ DSPy::Gemini::Guardrails.ensure_gemini_installed!
7
+
8
+ require 'dspy/gemini/lm/adapters/gemini_adapter'
9
+ require 'dspy/gemini/lm/schema_converter'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dspy-gemini
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vicente Reig Rincón de Arellano
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: dspy
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '='
17
+ - !ruby/object:Gem::Version
18
+ version: 0.31.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - '='
24
+ - !ruby/object:Gem::Version
25
+ version: 0.31.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: gemini-ai
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: Provides the GeminiAdapter so Gemini-compatible providers can be added
41
+ to DSPy.rb projects independently of the core gem.
42
+ email:
43
+ - hey@vicente.services
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - lib/dspy/gemini.rb
51
+ - lib/dspy/gemini/README.md
52
+ - lib/dspy/gemini/guardrails.rb
53
+ - lib/dspy/gemini/lm/adapters/gemini_adapter.rb
54
+ - lib/dspy/gemini/lm/schema_converter.rb
55
+ - lib/dspy/gemini/version.rb
56
+ homepage: https://github.com/vicentereig/dspy.rb
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ github_repo: git@github.com:vicentereig/dspy.rb
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.6.9
76
+ specification_version: 4
77
+ summary: Gemini adapters for DSPy.rb.
78
+ test_files: []