Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions internal/util/gemini_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func cleanJSONSchema(jsonStr string, addPlaceholder bool) string {
jsonStr = mergeAllOf(jsonStr)
jsonStr = flattenAnyOfOneOf(jsonStr)
jsonStr = flattenTypeArrays(jsonStr)
jsonStr = inferMissingObjectType(jsonStr)

// Phase 3: Cleanup
jsonStr = removeUnsupportedKeywords(jsonStr)
Expand Down Expand Up @@ -495,6 +496,48 @@ func walkForExtensions(value gjson.Result, path string, paths *[]string) {
}
}

// inferMissingObjectType adds "type":"object" to any schema node that has
// "properties" but no "type" field. Gemini API rejects schemas where
// "properties" appears on a non-OBJECT type.
func inferMissingObjectType(jsonStr string) string {
paths := findPaths(jsonStr, "properties")
sortByDepth(paths)

for _, p := range paths {
parentPath := trimSuffix(p, ".properties")
if isSchemaPropertiesKeyword(parentPath) {
continue
}
typePath := joinPath(parentPath, "type")
if !gjson.Get(jsonStr, typePath).Exists() {
jsonStr, _ = sjson.Set(jsonStr, typePath, "object")
}
}
return jsonStr
}

// isSchemaPropertiesKeyword determines whether path points to a JSON Schema
// "properties" keyword (a map of field definitions) rather than a field that
// happens to be named "properties". It walks the path segments and tracks
// whether we are at schema level or inside a properties map (field-name level).
func isSchemaPropertiesKeyword(path string) bool {
if path == "" {
return false
}
parts := splitGJSONPath(path)
schemaLevel := true
for _, part := range parts {
if schemaLevel {
if part == "properties" {
schemaLevel = false
}
} else {
schemaLevel = true
}
}
return !schemaLevel
}

func cleanupRequiredFields(jsonStr string) string {
for _, p := range findPaths(jsonStr, "required") {
parentPath := trimSuffix(p, ".required")
Expand Down
135 changes: 135 additions & 0 deletions internal/util/gemini_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,3 +1046,138 @@ func TestRemoveExtensionFields(t *testing.T) {
})
}
}

func TestInferMissingObjectType(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "adds type:object when properties present but no type",
input: `{
"properties": {
"name": { "type": "string" }
}
}`,
expected: `{
"type": "object",
"properties": {
"name": { "type": "string" }
}
}`,
},
{
name: "does not overwrite existing type",
input: `{
"type": "object",
"properties": {
"name": { "type": "string" }
}
}`,
expected: `{
"type": "object",
"properties": {
"name": { "type": "string" }
}
}`,
},
{
name: "adds type:object to nested schema missing type",
input: `{
"type": "object",
"properties": {
"address": {
"properties": {
"street": { "type": "string" }
}
}
}
}`,
expected: `{
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" }
}
}
}
}`,
},
{
name: "does not modify field literally named properties",
input: `{
"type": "object",
"properties": {
"properties": {
"type": "string",
"description": "a field named properties"
}
}
}`,
expected: `{
"type": "object",
"properties": {
"properties": {
"type": "string",
"description": "a field named properties"
}
}
}`,
},
{
name: "handles field named properties that itself has properties",
input: `{
"type": "object",
"properties": {
"properties": {
"properties": {
"inner": { "type": "number" }
}
}
}
}`,
expected: `{
"type": "object",
"properties": {
"properties": {
"type": "object",
"properties": {
"inner": { "type": "number" }
}
}
}
}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := inferMissingObjectType(tt.input)
compareJSON(t, tt.expected, actual)
})
}
}

func TestCleanJSONSchemaForGemini_InferMissingObjectType(t *testing.T) {
input := `{
"properties": {
"user": {
"properties": {
"name": { "type": "string" }
}
}
}
}`

result := CleanJSONSchemaForGemini(input)

if !gjson.Get(result, "type").Exists() || gjson.Get(result, "type").String() != "object" {
t.Errorf("root should have type:object, got: %s", result)
}
if !gjson.Get(result, "properties.user.type").Exists() || gjson.Get(result, "properties.user.type").String() != "object" {
t.Errorf("nested user should have type:object, got: %s", result)
}
}