fix(util): drop null and empty enum values for Gemini schemas#4545
Open
RsLuna7 wants to merge 1 commit into
Open
fix(util): drop null and empty enum values for Gemini schemas#4545RsLuna7 wants to merge 1 commit into
RsLuna7 wants to merge 1 commit into
Conversation
JSON Schema allows null inside an enum array to express an optional value, but Gemini only accepts non-empty strings there and rejects the whole request with "enum[n]: cannot be empty" (HTTP 400). convertEnumValuesToStrings walked the array with gjson.Result.String(), which renders a JSON null as "", so the empty string was passed straight through to Gemini. Skip null and empty entries instead, and delete the enum keyword altogether when no valid value remains, so the property degrades to a plain typed field rather than an invalid empty enum. Covered by a regression test built from two schemas that actually triggered the 400: a nullable todos[].status and a nullable capability_mode.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Gemini rejects a request with
enum[n]: cannot be empty(HTTP 400) whenever a tool schema declares an optional enum the JSON Schema way, i.e. withnulllisted among the allowed values:{ "status": { "type": ["string", "null"], "enum": ["pending", "in_progress", "completed", "cancelled", null] } }This shape is not exotic — it is what several schema generators emit for an optional enum field (for example Rust's
schemarsforOption<SomeEnum>), so any client whose tool definitions contain a nullable enum hits the 400 and the whole request fails, not just the affected property.Cause
convertEnumValuesToStringsininternal/util/gemini_schema.gonormalizes every enum entry withgjson.Result.String(). For a JSONnullthat returns"", so the empty string is written straight back into the enum array and forwarded to Gemini, which only accepts non-empty strings there.Fix
nulland empty entries while normalizing.enumkeyword instead of emitting an empty array, so the property degrades to a plain typed field rather than an invalid empty enum.convertEnumValuesToStringsruns beforeaddEnumHints, so the hint text generated downstream no longer picks up empty values either — no separate change needed there.Tests
Adds
TestCleanJSONSchemaForAntigravity_RemovesNullAndEmptyEnumValues, built from two schemas that actually triggered the 400 in the field: a nullabletodos[].statusand a nullablecapability_mode. It asserts the filtered enums keep exactly their valid values and that no entry is a non-string or an empty string.Verification
go test ./internal/util -count=1— okgo build -o test-output ./cmd/server— okgofmt— clean on both changed files