fix(gemini): normalize JSON Schema union types for Vertex compatibility#3150
fix(gemini): normalize JSON Schema union types for Vertex compatibility#3150Vaibhav701161 wants to merge 1 commit intomainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughAdds JSON Schema union-type handling to the Gemini provider by updating Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.11.4)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
🧪 Test Suite AvailableThis PR can be tested by a repository admin. |
Confidence Score: 5/5Safe to merge — the change is well-scoped to the union-type normalization path with no impact on existing string-typed schemas. No P0/P1 findings. The type-switch pattern ( No files require special attention. Important Files Changed
Reviews (2): Last reviewed commit: "fix(gemini): normalize JSON Schema union..." | Re-trigger Greptile |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@core/providers/gemini/utils.go`:
- Around line 1265-1322: The union-type handling in the propType switch should
not assume propType is only []interface{} nor treat arrays of all-unparseable
elements as null; update the case to reuse extractTypesFromValue to parse both
[]interface{} and []string (or any slice of strings) into nonNullTypes and
hasNull, deduplicate properly, and only set schema.Type = TypeNULL when hasNull
is true (instead of when len(nonNullTypes)==0 regardless of hasNull); for
multiple non-null types set schema.AnyOf as before and preserve Nullable when
hasNull is true—modify the code around the propType branch that currently builds
nonNullTypes/hasNull (and references schema.Type, schema.Nullable, schema.AnyOf)
to call extractTypesFromValue and apply these rules.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 57e4be28-1a49-49b1-b01f-df1d18a29915
📒 Files selected for processing (2)
core/providers/gemini/uniontype_test.gocore/providers/gemini/utils.go
- Handle ["T", "null"] -> Type + Nullable
- Convert multi-type unions -> anyOf
- Deduplicate type arrays
- Prevent empty Type field causing Vertex 400 error
- Support both []interface{} and []string union type inputs
- Fix: only emit TypeNULL when "null" is explicitly present (not on all-invalid arrays)
- Refactor type extraction into reusable extractUnionTypes + applyUnionType helpers
- Add comprehensive unit tests + e2e validation
Addresses review feedback from Greptile and CodeRabbit.
Fixes issue: Vertex rejects tool schemas with union type arrays
5d9577d to
fdd2c76
Compare
Summary
This change fixes an incompatibility between Bifrost and Vertex AI when handling JSON Schema union types in tool and function parameters.
Previously, schemas containing union types such as:
"type": ["integer", "null"]
were not correctly normalized before being sent to the Gemini or Vertex provider. This resulted in invalid schemas where the
typefield was effectively missing, causing Vertex to reject requests with a 400 error.This PR introduces proper normalization of union types to ensure compatibility with Gemini and Vertex AI.
Previous Behavior
Bifrost assumed that the JSON Schema
typefield was always a string. When an array was provided:"type": ["integer", "null"]
the conversion logic failed silently because:
typefieldThis led to requests being rejected by Vertex with errors such as:
parameters. schema didn't specify the schema type field
As a result:
Current Behavior
The schema conversion logic now correctly handles JSON Schema union types.
Supported transformations:
"type": "integer"
-> unchanged
"type": ["integer", "null"]
-> type: "integer", nullable: true
"type": ["string", "null"]
-> type: "string", nullable: true
"type": ["integer", "string"]
-> anyOf: [{type: "integer"}, {type: "string"}]
"type": ["integer", "string", "null"]
-> anyOf: [...], nullable: true
"type": ["null"]
-> type: "null"
Additional improvements:
Validation
The fix has been validated through both unit tests and real end-to-end requests.
Unit Tests
End-to-End Verification
A real request using:
"type": ["integer", "null"]
was successfully processed through Bifrost and accepted by Gemini (gemini-2.5-flash), resulting in a valid tool call:
"tool_calls": [
{
"function": {
"name": "get_process",
"arguments": {
"pid": 42,
"timeout_secs": null
}
}
}
]
This confirms:
(See attached screenshot)
Impact
Type of change
Affected areas
How to test
execute:
curl -s -X POST http://localhost:9090/v1/chat/completions
-H "Content-Type: application/json"
-d '{
"model": "gemini/gemini-2.5-flash",
"messages": [
{
"role": "user",
"content": "Call get_process with pid 42 and timeout_secs null"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_process",
"parameters": {
"type": "object",
"properties": {
"pid": { "type": "integer" },
"timeout_secs": { "type": ["integer", "null"] }
},
"required": ["pid"]
}
}
}
],
"tool_choice": "auto"
}'
Expected:
Breaking changes
Related issues
Fixes: #3141
Security considerations
No changes to authentication, data handling, or external interfaces. Schema normalization is internal.
Checklist