Class: DSPy::LM::Adapters::Gemini::SchemaConverter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/lm/adapters/gemini/schema_converter.rb

Overview

Converts DSPy signatures to Gemini structured output format

Constant Summary collapse

STRUCTURED_OUTPUT_MODELS =

Models that support structured outputs (JSON + Schema) Based on official Google documentation and gemini-ai gem table

T.let([
  "gemini-1.5-pro",              # ✅ Full schema support (legacy)
  "gemini-1.5-pro-preview-0514", # ✅ Full schema support (legacy)
  "gemini-1.5-pro-preview-0409", # ✅ Full schema support (legacy)
  "gemini-2.5-flash",            # ✅ Full schema support (2025 current)
  "gemini-2.5-flash-lite"        # ✅ Full schema support (2025 current)
].freeze, T::Array[String])
JSON_ONLY_MODELS =

Models that support JSON mode but NOT schema

T.let([
  "gemini-pro",                   # 🟡 JSON only, no schema
  "gemini-1.5-flash",             # 🟡 JSON only, no schema (legacy)
  "gemini-1.5-flash-preview-0514", # 🟡 JSON only, no schema (legacy)
  "gemini-1.0-pro-002",           # 🟡 JSON only, no schema
  "gemini-1.0-pro",               # 🟡 JSON only, no schema
  "gemini-2.0-flash-001",         # 🟡 JSON only, no schema (2025)
  "gemini-2.0-flash-lite-001"     # 🟡 JSON only, no schema (2025)
].freeze, T::Array[String])

Class Method Summary collapse

Class Method Details

.supports_structured_outputs?(model) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/dspy/lm/adapters/gemini/schema_converter.rb', line 44

def self.supports_structured_outputs?(model)
  # Extract base model name without provider prefix
  base_model = model.sub(/^gemini\//, "")
  
  # Check if it's a supported model or a newer version
  STRUCTURED_OUTPUT_MODELS.any? { |supported| base_model.start_with?(supported) }
end

.to_gemini_format(signature_class) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/dspy/lm/adapters/gemini/schema_converter.rb', line 35

def self.to_gemini_format(signature_class)
  # Get the output JSON schema from the signature class
  output_schema = signature_class.output_json_schema
  
  # Convert to Gemini format (OpenAPI 3.0 Schema subset - not related to OpenAI)
  convert_dspy_schema_to_gemini(output_schema)
end

.validate_compatibility(schema) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dspy/lm/adapters/gemini/schema_converter.rb', line 53

def self.validate_compatibility(schema)
  issues = []

  # Check for deeply nested objects (Gemini has depth limits)
  depth = calculate_depth(schema)
  if depth > 5
    issues << "Schema depth (#{depth}) exceeds recommended limit of 5 levels"
  end

  issues
end