#20990: fix: Handle single-element type arrays in tryFlattenLiteralAnyOf
agents
size: S
Cluster:
Tool Schema Fixes and Enhancements
## Summary
Fixes #20898
`tryFlattenLiteralAnyOf` was failing to flatten unions when schema variants used `type: ["string"]` instead of `type: "string"`, even though both are valid and semantically identical per JSON Schema spec.
## Changes
Added `resolveVariantType()` helper function that normalizes type representation:
- `type: "string"` → `"string"`
- `type: ["string"]` → `"string"` (single-element array)
- `type: ["string", "null"]` → `null` (multi-element, bail out)
## Testing
Added comprehensive test suite (`src/agents/schema/clean-for-gemini.test.ts`):
- ✅ Flattens anyOf with type as string
- ✅ Flattens anyOf with type as single-element array
- ✅ Flattens mixed string and array types
- ✅ Does not flatten when types differ
- ✅ Handles multi-element type arrays with null stripping
```bash
pnpm test src/agents/schema/clean-for-gemini.test.ts
# Test Files 1 passed (1)
# Tests 5 passed (5)
```
## Impact
**Low risk** - Only affects schema normalization for Gemini. Existing behavior preserved for all other cases.
Closes #20898
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Added `resolveVariantType()` helper to normalize type representation in `tryFlattenLiteralAnyOf`, fixing a bug where schemas with `type: ["string"]` (single-element array) were not being flattened even though they're semantically identical to `type: "string"` per JSON Schema spec.
- Handles string types, single-element type arrays, and correctly bails on multi-element arrays
- Comprehensive test coverage validates the fix for all edge cases
- Minimal change with clear focus on the reported issue
<h3>Confidence Score: 5/5</h3>
- Safe to merge with no risk
- The change is minimal, well-tested, and narrowly scoped to fixing a specific schema normalization issue. The helper function correctly handles all edge cases, and comprehensive tests verify the behavior. No breaking changes or side effects.
- No files require special attention
<sub>Last reviewed commit: 3266004</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#21498: fix #20898: handle array type in tryFlattenLiteralAnyOf
by neipor · 2026-02-20
88.0%
#17885: fix: replace anyOf union type in image tool schema
by pierreeurope · 2026-02-16
76.5%
#22214: fix(tools): sanitize google-antigravity schemas for Gemini-compatib...
by Kansodata · 2026-02-20
74.7%
#21835: fix: apply schema cleaning for google-antigravity in normalizeToolP...
by ephraimm · 2026-02-20
73.2%
#23507: fix(tools): strip patternProperties from schemas for OpenAI-compati...
by long-pham · 2026-02-22
72.3%
#20249: fix(schema): ensure normalizeToolParameters always includes propert...
by aldoeliacim · 2026-02-18
72.2%
#22321: fix: sanitize tool schemas for all Cloud Code Assist providers
by Sr-0w · 2026-02-21
70.5%
#11141: Fix JSON schema conversion error when using llama.cpp backend
by 9nix00 · 2026-02-07
70.2%
#21027: fix(tests): resolve TypeScript type errors in CLI test files
by kittipond2365 · 2026-02-19
69.4%
#5447: fix(schema): preserve sibling properties when resolving $ref
by abhijeet117 · 2026-01-31
69.3%