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

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/gemini/lm/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: ai.google.dev/gemini-api/docs/models/gemini Last updated: Oct 2025 Note: Gemini 1.5 series deprecated Oct 2025

T.let([
  # Gemini 2.0 series
  "gemini-2.0-flash",
  "gemini-2.0-flash-lite",
  # Gemini 2.5 series (current)
  "gemini-2.5-pro",
  "gemini-2.5-flash",
  "gemini-2.5-flash-lite",
  "gemini-2.5-flash-image"
].freeze, T::Array[String])
UNSUPPORTED_MODELS =

Models that do not support structured outputs or are deprecated

T.let([
  # Legacy Gemini 1.0 series
  "gemini-pro",
  "gemini-1.0-pro-002",
  "gemini-1.0-pro",
  # Deprecated Gemini 1.5 series (removed Oct 2025)
  "gemini-1.5-pro",
  "gemini-1.5-pro-preview-0514",
  "gemini-1.5-pro-preview-0409",
  "gemini-1.5-flash",
  "gemini-1.5-flash-8b"
].freeze, T::Array[String])

Class Method Summary collapse

Class Method Details

.supports_structured_outputs?(model) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/dspy/gemini/lm/schema_converter.rb', line 51

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



42
43
44
45
46
47
48
# File 'lib/dspy/gemini/lm/schema_converter.rb', line 42

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dspy/gemini/lm/schema_converter.rb', line 60

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