Class: DSPy::LM::Adapters::OpenAI::SchemaConverter

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

Overview

Converts DSPy signatures to OpenAI structured output format

Constant Summary collapse

STRUCTURED_OUTPUT_MODELS =

Models that support structured outputs as of July 2025

T.let([
  "gpt-4o-mini",
  "gpt-4o-2024-08-06",
  "gpt-4o",
  "gpt-4-turbo",
  "gpt-4-turbo-2024-04-09"
].freeze, T::Array[String])

Class Method Summary collapse

Class Method Details

.supports_structured_outputs?(model) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dspy/lm/adapters/openai/schema_converter.rb', line 77

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(/^openai\//, "")
  
  # 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_openai_format(signature_class, name: nil, strict: true) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dspy/lm/adapters/openai/schema_converter.rb', line 24

def self.to_openai_format(signature_class, name: nil, strict: true)
  # Build cache params from the method parameters
  cache_params = { strict: strict }
  cache_params[:name] = name if name
  
  # Check cache first
  cache_manager = DSPy::LM.cache_manager
  cached_schema = cache_manager.get_schema(signature_class, "openai", cache_params)
  
  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
  
  # Build the complete schema
  dspy_schema = {
    "$schema": "http://json-schema.org/draft-06/schema#",
    type: "object",
    properties: output_schema[:properties] || {},
    required: output_schema[:required] || []
  }

  # Generate a schema name if not provided
  schema_name = name || generate_schema_name(signature_class)

  # Remove the $schema field as OpenAI doesn't use it
  openai_schema = dspy_schema.except(:$schema)

  # Add additionalProperties: false for strict mode
  if strict
    openai_schema = add_additional_properties_recursively(openai_schema)
  end

  # Wrap in OpenAI's required format
  result = {
    type: "json_schema",
    json_schema: {
      name: schema_name,
      strict: strict,
      schema: openai_schema
    }
  }
  
  # Cache the result with same params
  cache_manager.cache_schema(signature_class, "openai", result, cache_params)
  
  result
end

.validate_compatibility(schema) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dspy/lm/adapters/openai/schema_converter.rb', line 100

def self.validate_compatibility(schema)
  issues = []

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

  # Check for unsupported JSON Schema features
  if contains_pattern_properties?(schema)
    issues << "Pattern properties are not supported in OpenAI structured outputs"
  end

  if contains_conditional_schemas?(schema)
    issues << "Conditional schemas (if/then/else) are not supported"
  end

  issues
end