Skip to content
Merged
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
21 changes: 20 additions & 1 deletion internal/translator/codex/claude/codex_claude_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
}
tool, _ = sjson.Set(tool, "name", name)
}
tool, _ = sjson.SetRaw(tool, "parameters", toolResult.Get("input_schema").Raw)
tool, _ = sjson.SetRaw(tool, "parameters", normalizeToolParameters(toolResult.Get("input_schema").Raw))
tool, _ = sjson.Delete(tool, "input_schema")
tool, _ = sjson.Delete(tool, "parameters.$schema")
tool, _ = sjson.Set(tool, "strict", false)
Expand Down Expand Up @@ -334,3 +334,22 @@ func buildReverseMapFromClaudeOriginalToShort(original []byte) map[string]string
}
return m
}

// normalizeToolParameters ensures object schemas contain at least an empty properties map.
func normalizeToolParameters(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" || raw == "null" || !gjson.Valid(raw) {
return `{"type":"object","properties":{}}`
}
schema := raw
result := gjson.Parse(raw)
schemaType := result.Get("type").String()
if schemaType == "" {
schema, _ = sjson.Set(schema, "type", "object")
schemaType = "object"
}
if schemaType == "object" && !result.Get("properties").Exists() {
schema, _ = sjson.SetRaw(schema, "properties", `{}`)
}
return schema
Comment on lines +340 to +354

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The implementation of normalizeToolParameters can be made more robust. There are two potential issues:

  1. It doesn't correctly handle valid JSON that is not an object (e.g., [], "a string"). This can cause silent failures in sjson and pass an invalid schema downstream.
  2. It doesn't handle schemas that have a type other than "object" (e.g., "type": "string"). These are passed through as-is and will likely cause errors in the target API, which expects an object with properties.

The suggested change refactors the function to address both issues by ensuring the schema is a JSON object and has the correct structure before returning.

	raw = strings.TrimSpace(raw)
	result := gjson.Parse(raw)
	schemaType := result.Get("type").String()

	if !result.IsObject() || (schemaType != "" && schemaType != "object") {
		return `{"type":"object","properties":{}}`
	}

	schema := raw
	if schemaType == "" {
		schema, _ = sjson.Set(schema, "type", "object")
	}

	if !result.Get("properties").Exists() {
		schema, _ = sjson.SetRaw(schema, "properties", `{}`)
	}
	return schema

}
Loading