-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: add missing 'items' to array schemas in Codex tool parameters #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
huynguyen03dev
wants to merge
1
commit into
router-for-me:main
from
huynguyen03dev:fix/codex-array-schema-missing-items
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Package util provides utility functions for the CLI Proxy API server. | ||
| package util | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "github.com/tidwall/gjson" | ||
| "github.com/tidwall/sjson" | ||
| ) | ||
|
|
||
| // FixCodexToolSchemas fixes tool schemas in a Codex API request body. | ||
| // It adds missing "items" to array-type schemas which OpenAI's strict validation requires. | ||
| func FixCodexToolSchemas(body []byte) []byte { | ||
| tools := gjson.GetBytes(body, "tools") | ||
| if !tools.IsArray() { | ||
| return body | ||
| } | ||
|
|
||
| for i, tool := range tools.Array() { | ||
| if tool.Get("type").String() != "function" { | ||
| continue | ||
| } | ||
|
|
||
| var params gjson.Result | ||
| var setPath string | ||
|
|
||
| // Support both Chat Completions (function.parameters) and Responses API (parameters) | ||
| if tool.Get("function.parameters").Exists() { | ||
| params = tool.Get("function.parameters") | ||
| setPath = "tools." + strconv.Itoa(i) + ".function.parameters" | ||
| } else if tool.Get("parameters").Exists() { | ||
| params = tool.Get("parameters") | ||
| setPath = "tools." + strconv.Itoa(i) + ".parameters" | ||
| } else { | ||
| continue | ||
| } | ||
|
|
||
| fixed := addMissingArrayItems(params.Raw) | ||
| if fixed != params.Raw { | ||
| body, _ = sjson.SetRawBytes(body, setPath, []byte(fixed)) | ||
| } | ||
| } | ||
| return body | ||
| } | ||
|
|
||
| // addMissingArrayItems adds a default "items" schema to arrays that are missing it. | ||
| func addMissingArrayItems(jsonStr string) string { | ||
| paths := findArrayTypePaths(gjson.Parse(jsonStr), "") | ||
| for _, p := range paths { | ||
| itemsPath := p + ".items" | ||
| if p == "" { | ||
| itemsPath = "items" | ||
| } | ||
| items := gjson.Get(jsonStr, itemsPath) | ||
| // Add items if missing or null | ||
| if !items.Exists() || items.Type == gjson.Null { | ||
| jsonStr, _ = sjson.SetRaw(jsonStr, itemsPath, `{}`) | ||
| } | ||
| } | ||
| return jsonStr | ||
| } | ||
|
|
||
| // isArrayType checks if a node's type indicates an array (string or array containing "array"). | ||
| func isArrayType(node gjson.Result) bool { | ||
| typeVal := node.Get("type") | ||
| if typeVal.IsArray() { | ||
| for _, t := range typeVal.Array() { | ||
| if t.String() == "array" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| return typeVal.String() == "array" | ||
| } | ||
|
|
||
| // findArrayTypePaths recursively finds all paths where type="array". | ||
| func findArrayTypePaths(node gjson.Result, path string) []string { | ||
| var paths []string | ||
|
|
||
| if node.IsObject() { | ||
| if isArrayType(node) { | ||
| paths = append(paths, path) | ||
| } | ||
| node.ForEach(func(key, value gjson.Result) bool { | ||
| newPath := key.String() | ||
| if path != "" { | ||
| newPath = path + "." + key.String() | ||
| } | ||
| paths = append(paths, findArrayTypePaths(value, newPath)...) | ||
| return true | ||
| }) | ||
| } else if node.IsArray() { | ||
| for i, elem := range node.Array() { | ||
| newPath := strconv.Itoa(i) | ||
| if path != "" { | ||
| newPath = path + "." + strconv.Itoa(i) | ||
| } | ||
| paths = append(paths, findArrayTypePaths(elem, newPath)...) | ||
| } | ||
| } | ||
|
|
||
| return paths | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/tidwall/gjson" | ||
| ) | ||
|
|
||
| func TestFixCodexToolSchemas_AddsMissingItems(t *testing.T) { | ||
| input := `{"tools":[{"type":"function","parameters":{"type":"object","properties":{"options":{"type":"array"}}}}]}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| items := gjson.GetBytes(result, "tools.0.parameters.properties.options.items") | ||
| if !items.Exists() { | ||
| t.Error("expected items to be added to array schema") | ||
| } | ||
| } | ||
|
|
||
| func TestFixCodexToolSchemas_PreservesExistingItems(t *testing.T) { | ||
| input := `{"tools":[{"type":"function","parameters":{"type":"object","properties":{"options":{"type":"array","items":{"type":"string"}}}}}]}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| itemsType := gjson.GetBytes(result, "tools.0.parameters.properties.options.items.type").String() | ||
| if itemsType != "string" { | ||
| t.Errorf("expected existing items to be preserved, got type=%s", itemsType) | ||
| } | ||
| } | ||
|
|
||
| func TestFixCodexToolSchemas_HandlesAnyOf(t *testing.T) { | ||
| input := `{"tools":[{"type":"function","parameters":{"anyOf":[{"type":"array"}]}}]}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| items := gjson.GetBytes(result, "tools.0.parameters.anyOf.0.items") | ||
| if !items.Exists() { | ||
| t.Error("expected items to be added to array schema inside anyOf") | ||
| } | ||
| } | ||
|
|
||
| func TestFixCodexToolSchemas_NoTools(t *testing.T) { | ||
| input := `{"model":"gpt-5"}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| if string(result) != input { | ||
| t.Error("expected unchanged output when no tools present") | ||
| } | ||
| } | ||
|
|
||
| func TestFixCodexToolSchemas_ChatCompletionsFormat(t *testing.T) { | ||
| input := `{"tools":[{"type":"function","function":{"name":"test","parameters":{"type":"object","properties":{"items":{"type":"array"}}}}}]}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| items := gjson.GetBytes(result, "tools.0.function.parameters.properties.items.items") | ||
| if !items.Exists() { | ||
| t.Error("expected items to be added to array schema in function.parameters") | ||
| } | ||
| } | ||
|
|
||
| func TestFixCodexToolSchemas_NullableArrayType(t *testing.T) { | ||
| input := `{"tools":[{"type":"function","parameters":{"type":"object","properties":{"data":{"type":["array","null"]}}}}]}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| items := gjson.GetBytes(result, "tools.0.parameters.properties.data.items") | ||
| if !items.Exists() { | ||
| t.Error("expected items to be added to nullable array schema") | ||
| } | ||
| } | ||
|
|
||
| func TestFixCodexToolSchemas_NullItems(t *testing.T) { | ||
| input := `{"tools":[{"type":"function","parameters":{"type":"object","properties":{"list":{"type":"array","items":null}}}}]}` | ||
| result := FixCodexToolSchemas([]byte(input)) | ||
|
|
||
| items := gjson.GetBytes(result, "tools.0.parameters.properties.list.items") | ||
| if items.Type == gjson.Null { | ||
| t.Error("expected null items to be replaced with empty object") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recursive implementation of
findArrayTypePathscan be inefficient due to the use ofappend(slice, anotherSlice...)within the recursion. Each call creates a new slice, leading to multiple allocations. A more performant and arguably clearer approach is to use a closure to append to a singlepathsslice, avoiding the overhead of repeated slice allocations.