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

T.let([
  "gemini-1.5-pro",
  "gemini-1.5-flash",
  "gemini-2.0-flash-exp"
].freeze, T::Array[String])

Class Method Summary collapse

Class Method Details

.supports_structured_outputs?(model) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dspy/lm/adapters/gemini/schema_converter.rb', line 45

def self.supports_structured_outputs?(model)
  # Check cache first
  cache_manager = DSPy::LM.cache_manager
  cached_result = cache_manager.get_capability(model, "structured_outputs")
  
  if !cached_result.nil?
    DSPy.logger.debug("Using cached capability check for #{model}")
    return cached_result
  end
  
  # Extract base model name without provider prefix
  base_model = model.sub(/^gemini\//, "")
  
  # Check if it's a supported model or a newer version
  result = STRUCTURED_OUTPUT_MODELS.any? { |supported| base_model.start_with?(supported) }
  
  # Cache the result
  cache_manager.cache_capability(model, "structured_outputs", result)
  
  result
end

.to_gemini_format(signature_class) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dspy/lm/adapters/gemini/schema_converter.rb', line 22

def self.to_gemini_format(signature_class)
  # Check cache first
  cache_manager = DSPy::LM.cache_manager
  cached_schema = cache_manager.get_schema(signature_class, "gemini", {})
  
  if cached_schema
    DSPy.logger.debug("Using cached schema for #{signature_class.name}")
    return cached_schema
  end
  
  # 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)
  gemini_schema = convert_dspy_schema_to_gemini(output_schema)
  
  # Cache the result
  cache_manager.cache_schema(signature_class, "gemini", gemini_schema, {})
  
  gemini_schema
end

.validate_compatibility(schema) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dspy/lm/adapters/gemini/schema_converter.rb', line 68

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