6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'app/models/raif/concerns/llms/open_ai/json_schema_validation.rb', line 6
def validate_json_schema!(schema)
return if schema.blank?
errors = []
if schema.blank?
errors << "JSON schema must include a 'schema' property"
else
if schema[:type] != "object" && !schema.key?(:properties)
errors << "Root schema must be of type 'object' with 'properties'"
end
validate_object_properties(schema, errors)
validate_properties_count(schema, errors)
validate_nesting_depth(schema, errors)
if schema[:anyOf].present? && schema[:properties].blank?
errors << "Root objects cannot be of type 'anyOf'"
end
end
if errors.any?
error_message = "Invalid JSON schema for OpenAI structured outputs: #{errors.join("; ")}\nSchema was: #{schema.inspect}"
raise Raif::Errors::OpenAi::JsonSchemaError, error_message
else
true
end
end
|