Repository navigation
fix(aistudio): improve JSON Schema compatibility for null fields, boolean schemas, and not constraints - #44
Merged
Mag1cFall merged 1 commit intoOct 4, 2026
Conversation
…lean schemas, and not constraints
- Default empty or null top-level schema to empty object
- Strip explicit null fields from schema and properties to prevent validation failure
- Support boolean schemas for items and properties (true -> {}, false -> delete)
- Normalize or prune no-op and unsupported "not" constraints into enum schemas
- Add unit tests covering null fields, not variants, boolean schemas, and const
Owner
|
Thank you so much! Tested and merged, with some more fixes |
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.
Description
Motivation (Client Schema Compatibility Context)
When using tool calling (Function Calling) and structured outputs (
response_format: json_schema) via OpenAI-, Anthropic-, or Gemini-compatible endpoints, client libraries and modern schema generators (such as Pydantic v2, Instructor, LangChain, LlamaIndex, and the official OpenAI Python SDK) frequently emit valid Draft-07/2020-12 constructs or lenient extensions:nullvalues: Pydantic v2 and various serializers produce optional/unset schema fields with explicitnullvalues (e.g.,"description": null,"format": null,"items": null,"properties": null,"nullable": null). Previously,AIStudio2APIstrictly validated these fields using rigid type decoders (schemaString,json.Unmarshal(..., &bool)), resulting in immediate rejection (schema.description must be a string/schema.nullable must be a boolean) before the request could even reach Google AI Studio.nullor empty schemas: Tools defined with empty parameter sets or serializers outputting"parameters": null/"parameters": ""failed withschema must be a JSON object, breaking zero-argument tool calls.items&properties): Under the JSON Schema specification (Draft-07, 2020-12), schemas can be booleans ("items": trueallowing any elements,"items": falsedisallowing them, or boolean properties). The encoder previously expected JSON objects exclusively and rejected boolean values."not"constraints: Many client libraries express non-null types or excluded options via"not"constraints (e.g.,not: {"type": "null"},not: ["gpt-3.5", "gpt-4"],not: null). These caused upstream protocol encoding failures or HTTP 400 errors because the wire format expects a specific schema structure at wire index 19.While the official OpenAI API leniently sanitizes and tolerates these constructs,
AIStudio2APIwas previously failing at the proxy encoding boundary. This PR aligns the proxy's schema normalization with real-world client behavior.Key Changes
Top-level null & empty schema fallback:
encodeJSONSchema, inspects incoming raw message bytes: if empty, whitespace-only, ornull, safely falls back to a default empty object schema{"type":"object","properties":{}}.Explicit
nullfield stripping (cleanNullFields):cleanNullFieldsto purge keys with explicitnullvalues (format,description,nullable,enum,items,properties,required, etc.).propertiesdictionaries.constfields wherenullmay be a valid constant value.Boolean schema support for
itemsandproperties:items: maps booleantrueto open schema{}and prunesfalse.properties: normalizes booleantrueproperties to{}and removesfalseproperties to prevent invalid downstream wire encoding.Normalization and pruning of
"not"constraints (normalizeNot):not: null,not: false,not: true,not: "",not: {},not: {"type": "null"}).not: ["gpt-3.5"]ornot: "gpt-3.5") into compliant enum sub-schemas ({"enum": [...]}).Comprehensive unit test suite (
internal/aistudio/schema_test.go):TestEncodeJSONSchema_NullFields: verifies sanitization across 22 common schema fields with explicitnullvalues.TestEncodeJSONSchema_NotVariants: validates all"not"variants, booleanitems, and booleanproperties.TestEncodeJSONSchema_TopLevelNullOrEmpty: tests empty strings, spaces, andnullroot inputs.TestEncodeJSONSchema_DirectConst: ensures validconstschemas remain intact.Verification
go test -v ./internal/aistudio/...: all test suites passed.go test ./...: all repository package tests passed.go vet ./...: 0 issues detected.