dspy-anthropic 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: 6be44efc345ac49da9bc7177cb8023c291618566f8229ac53791fca54c59de1a
4
+ data.tar.gz: b502001a52d0ab2deceb45ebdc0d763c8fa05ca3770df6d17f10529227419158
5
+ SHA512:
6
+ metadata.gz: 16cf15c23300164bb498277e57a64f8bf843c879f3f20a070f45aac72ddeeb5de481d6a4b8266c16d7fc1d5fab1db86db9db564e64b85a2f39c31fbe8e231a8e
7
+ data.tar.gz: 55abeb260a1b0bccd4750c826d79be04b0f41dcc5595b15a73f5261303a14400eb424cc129c9eb1cf8d437ca135da51958e368bae1e05fbc939ca9402c0f9b8a
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,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dspy/lm/errors'
4
+
5
+ module DSPy
6
+ module Anthropic
7
+ class Guardrails
8
+ SUPPORTED_ANTHROPIC_VERSIONS = "~> 1.12".freeze
9
+
10
+ def self.ensure_anthropic_installed!
11
+ require 'anthropic'
12
+
13
+ spec = Gem.loaded_specs["anthropic"]
14
+ unless spec && Gem::Requirement.new(SUPPORTED_ANTHROPIC_VERSIONS).satisfied_by?(spec.version)
15
+ msg = <<~MSG
16
+ DSPY requires the `anthropic` gem #{SUPPORTED_ANTHROPIC_VERSIONS}.
17
+ Please install or upgrade it with `bundle add anthropic --version "#{SUPPORTED_ANTHROPIC_VERSIONS}"`.
18
+ MSG
19
+ raise DSPy::LM::UnsupportedVersionError, msg
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,299 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'anthropic'
4
+ require 'dspy/lm/vision_models'
5
+ require 'dspy/lm/adapter'
6
+
7
+ require 'dspy/anthropic/guardrails'
8
+ DSPy::Anthropic::Guardrails.ensure_anthropic_installed!
9
+
10
+ module DSPy
11
+ module Anthropic
12
+ module LM
13
+ module Adapters
14
+ class AnthropicAdapter < DSPy::LM::Adapter
15
+ def initialize(model:, api_key:, structured_outputs: true)
16
+ super(model: model, api_key: api_key)
17
+ validate_api_key!(api_key, 'anthropic')
18
+ @client = ::Anthropic::Client.new(api_key: api_key)
19
+ @structured_outputs_enabled = structured_outputs
20
+ end
21
+
22
+ def chat(messages:, signature: nil, **extra_params, &block)
23
+ normalized_messages = normalize_messages(messages)
24
+
25
+ # Validate vision support if images are present
26
+ if contains_images?(normalized_messages)
27
+ DSPy::LM::VisionModels.validate_vision_support!('anthropic', model)
28
+ # Convert messages to Anthropic format with proper image handling
29
+ normalized_messages = format_multimodal_messages(normalized_messages)
30
+ end
31
+
32
+ # Anthropic requires system message to be separate from messages
33
+ system_message, user_messages = extract_system_message(normalized_messages)
34
+
35
+ # Check if this is a tool use request
36
+ has_tools = extra_params.key?(:tools) && !extra_params[:tools].empty?
37
+
38
+ # Apply JSON prefilling if needed for better Claude JSON compliance (but not for tool use)
39
+ unless has_tools || contains_images?(normalized_messages)
40
+ user_messages = prepare_messages_for_json(user_messages, system_message)
41
+ end
42
+
43
+ request_params = {
44
+ model: model,
45
+ messages: user_messages,
46
+ max_tokens: 4096, # Required for Anthropic
47
+ temperature: 0.0 # DSPy default for deterministic responses
48
+ }.merge(extra_params)
49
+
50
+ # Add system message if present
51
+ request_params[:system] = system_message if system_message
52
+
53
+ # Add streaming if block provided
54
+ if block_given?
55
+ request_params[:stream] = true
56
+ end
57
+
58
+ begin
59
+ if block_given?
60
+ content = ""
61
+ @client.messages.stream(**request_params) do |chunk|
62
+ if chunk.respond_to?(:delta) && chunk.delta.respond_to?(:text)
63
+ chunk_text = chunk.delta.text
64
+ content += chunk_text
65
+ block.call(chunk)
66
+ end
67
+ end
68
+
69
+ # Create typed metadata for streaming response
70
+ metadata = DSPy::LM::ResponseMetadataFactory.create('anthropic', {
71
+ model: model,
72
+ streaming: true
73
+ })
74
+
75
+ DSPy::LM::Response.new(
76
+ content: content,
77
+ usage: nil, # Usage not available in streaming
78
+ metadata: metadata
79
+ )
80
+ else
81
+ response = @client.messages.create(**request_params)
82
+
83
+ if response.respond_to?(:error) && response.error
84
+ raise DSPy::LM::AdapterError, "Anthropic API error: #{response.error}"
85
+ end
86
+
87
+ # Handle both text content and tool use
88
+ content = ""
89
+ tool_calls = []
90
+
91
+ if response.content.is_a?(Array)
92
+ response.content.each do |content_block|
93
+ case content_block.type.to_s
94
+ when "text"
95
+ content += content_block.text
96
+ when "tool_use"
97
+ tool_calls << {
98
+ id: content_block.id,
99
+ name: content_block.name,
100
+ input: content_block.input
101
+ }
102
+ end
103
+ end
104
+ end
105
+
106
+ usage = response.usage
107
+
108
+ # Convert usage data to typed struct
109
+ usage_struct = DSPy::LM::UsageFactory.create('anthropic', usage)
110
+
111
+ metadata = {
112
+ provider: 'anthropic',
113
+ model: model,
114
+ response_id: response.id,
115
+ role: response.role
116
+ }
117
+
118
+ # Add tool calls to metadata if present
119
+ metadata[:tool_calls] = tool_calls unless tool_calls.empty?
120
+
121
+ # Create typed metadata
122
+ typed_metadata = DSPy::LM::ResponseMetadataFactory.create('anthropic', metadata)
123
+
124
+ DSPy::LM::Response.new(
125
+ content: content,
126
+ usage: usage_struct,
127
+ metadata: typed_metadata
128
+ )
129
+ end
130
+ rescue => e
131
+ # Check for specific image-related errors in the message
132
+ error_msg = e.message.to_s
133
+
134
+ if error_msg.include?('Could not process image')
135
+ raise DSPy::LM::AdapterError, "Image processing failed: #{error_msg}. Ensure your image is a valid PNG, JPEG, GIF, or WebP format, properly base64-encoded, and under 5MB."
136
+ elsif error_msg.include?('image')
137
+ raise DSPy::LM::AdapterError, "Image error: #{error_msg}. Anthropic requires base64-encoded images (URLs are not supported)."
138
+ elsif error_msg.include?('rate')
139
+ raise DSPy::LM::AdapterError, "Anthropic rate limit exceeded: #{error_msg}. Please wait and try again."
140
+ elsif error_msg.include?('authentication') || error_msg.include?('API key')
141
+ raise DSPy::LM::AdapterError, "Anthropic authentication failed: #{error_msg}. Check your API key."
142
+ else
143
+ # Generic error handling
144
+ raise DSPy::LM::AdapterError, "Anthropic adapter error: #{e.message}"
145
+ end
146
+ end
147
+ end
148
+
149
+ private
150
+
151
+ # Enhanced JSON extraction specifically for Claude models
152
+ # Handles multiple patterns of markdown-wrapped JSON responses
153
+ def extract_json_from_response(content)
154
+ return content if content.nil? || content.empty?
155
+
156
+ # Pattern 1: ```json blocks
157
+ if content.include?('```json')
158
+ extracted = content[/```json\s*\n(.*?)\n```/m, 1]
159
+ return extracted.strip if extracted
160
+ end
161
+
162
+ # Pattern 2: ## Output values header
163
+ if content.include?('## Output values')
164
+ extracted = content.split('## Output values').last
165
+ .gsub(/```json\s*\n/, '')
166
+ .gsub(/\n```.*/, '')
167
+ .strip
168
+ return extracted if extracted && !extracted.empty?
169
+ end
170
+
171
+ # Pattern 3: Generic code blocks (check if it looks like JSON)
172
+ if content.include?('```')
173
+ extracted = content[/```\s*\n(.*?)\n```/m, 1]
174
+ return extracted.strip if extracted && looks_like_json?(extracted)
175
+ end
176
+
177
+ # Pattern 4: Already valid JSON or fallback
178
+ content.strip
179
+ end
180
+
181
+ # Simple heuristic to check if content looks like JSON
182
+ def looks_like_json?(str)
183
+ return false if str.nil? || str.empty?
184
+ trimmed = str.strip
185
+ (trimmed.start_with?('{') && trimmed.end_with?('}')) ||
186
+ (trimmed.start_with?('[') && trimmed.end_with?(']'))
187
+ end
188
+
189
+ # Prepare messages for JSON output by adding prefilling and strong instructions
190
+ def prepare_messages_for_json(user_messages, system_message)
191
+ return user_messages unless requires_json_output?(user_messages, system_message)
192
+ return user_messages unless tends_to_wrap_json?
193
+
194
+ # Add strong JSON instruction to the last user message if not already present
195
+ enhanced_messages = enhance_json_instructions(user_messages)
196
+
197
+ # Only add prefill for models that support it and temporarily disable for testing
198
+ if false # supports_prefilling? - temporarily disabled
199
+ add_json_prefill(enhanced_messages)
200
+ else
201
+ enhanced_messages
202
+ end
203
+ end
204
+
205
+ # Detect if the conversation requires JSON output
206
+ def requires_json_output?(user_messages, system_message)
207
+ # Check for JSON-related keywords in messages
208
+ all_content = [system_message] + user_messages.map { |m| m[:content] }
209
+ all_content.compact.any? do |content|
210
+ content.downcase.include?('json') ||
211
+ content.include?('```') ||
212
+ content.include?('{') ||
213
+ content.include?('output')
214
+ end
215
+ end
216
+
217
+ # Check if this is a Claude model that benefits from prefilling
218
+ def supports_prefilling?
219
+ # Claude models that work well with JSON prefilling
220
+ model.downcase.include?('claude')
221
+ end
222
+
223
+ # Check if this is a Claude model that tends to wrap JSON in markdown
224
+ def tends_to_wrap_json?
225
+ # All Claude models have this tendency, especially Opus variants
226
+ model.downcase.include?('claude')
227
+ end
228
+
229
+ # Enhance the last user message with strong JSON instructions
230
+ def enhance_json_instructions(user_messages)
231
+ return user_messages if user_messages.empty?
232
+
233
+ enhanced_messages = user_messages.dup
234
+ last_message = enhanced_messages.last
235
+
236
+ # Only add instruction if not already present
237
+ unless last_message[:content].include?('ONLY valid JSON')
238
+ # Use smart default instruction for Claude models
239
+ json_instruction = "\n\nIMPORTANT: Respond with ONLY valid JSON. No markdown formatting, no code blocks, no explanations. Start your response with '{' and end with '}'."
240
+
241
+ last_message = last_message.dup
242
+ last_message[:content] = last_message[:content] + json_instruction
243
+ enhanced_messages[-1] = last_message
244
+ end
245
+
246
+ enhanced_messages
247
+ end
248
+
249
+ # Add assistant message prefill to guide Claude
250
+ def add_json_prefill(user_messages)
251
+ user_messages + [{ role: "assistant", content: "{" }]
252
+ end
253
+
254
+ def extract_system_message(messages)
255
+ system_message = nil
256
+ user_messages = []
257
+
258
+ messages.each do |msg|
259
+ if msg[:role] == 'system'
260
+ system_message = msg[:content]
261
+ else
262
+ user_messages << msg
263
+ end
264
+ end
265
+
266
+ [system_message, user_messages]
267
+ end
268
+
269
+ def format_multimodal_messages(messages)
270
+ messages.map do |msg|
271
+ if msg[:content].is_a?(Array)
272
+ # Convert multimodal content to Anthropic format
273
+ formatted_content = msg[:content].map do |item|
274
+ case item[:type]
275
+ when 'text'
276
+ { type: 'text', text: item[:text] }
277
+ when 'image'
278
+ # Validate image compatibility before formatting
279
+ item[:image].validate_for_provider!('anthropic')
280
+ item[:image].to_anthropic_format
281
+ else
282
+ item
283
+ end
284
+ end
285
+
286
+ {
287
+ role: msg[:role],
288
+ content: formatted_content
289
+ }
290
+ else
291
+ msg
292
+ end
293
+ end
294
+ end
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module Anthropic
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dspy/anthropic/version'
4
+
5
+ require 'dspy/anthropic/guardrails'
6
+ DSPy::Anthropic::Guardrails.ensure_anthropic_installed!
7
+
8
+ require 'dspy/anthropic/lm/adapters/anthropic_adapter'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dspy-anthropic
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: anthropic
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 AnthropicAdapter so Claude-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/anthropic.rb
51
+ - lib/dspy/anthropic/guardrails.rb
52
+ - lib/dspy/anthropic/lm/adapters/anthropic_adapter.rb
53
+ - lib/dspy/anthropic/version.rb
54
+ homepage: https://github.com/vicentereig/dspy.rb
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ github_repo: git@github.com:vicentereig/dspy.rb
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.3.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.6.9
74
+ specification_version: 4
75
+ summary: Anthropic adapters for DSPy.rb.
76
+ test_files: []