Repository navigation
Commit 18510c4
authored
fix(llm-proxy): sanitize non-string enums in Gemini tool schemas (archestra-ai#5407)
## Problem
Chat with `gemini-2.5-pro` + MCP tools fails with HTTP 400
`INVALID_ARGUMENT`:
```
Invalid value at 'tools[0].function_declarations[0].parameters
.properties[2].value.any_of[0].properties[0].value.enum[0]' (TYPE_STRING), true
```
Gemini's `function_declaration` schema validation only permits `enum` on
**string-typed** fields. A boolean `true` inside `enum` is rejected.
## Root cause
`@ai-sdk/google` (verified in installed `3.0.43` and latest `3.0.80`)
converts a zod `const: true` (e.g. a boolean acknowledgement flag /
discriminated-union discriminator) into `enum: [true]` and never coerces
enum values to strings:
```js
if (constValue !== void 0) { result.enum = [constValue]; } // const -> enum, verbatim
if (enumValues !== void 0) { result.enum = enumValues; } // enum verbatim, no coercion
```
The proxy then forwarded `fd.parameters` verbatim through
`@google/genai` to Gemini in `restToSdkGenerateContentParams`, so the
boolean enum reached Gemini unchanged. Bumping the AI SDK would **not**
fix this (latest has identical handling), and the proxy also serves
external clients, so sanitizing in the proxy is the correct layer.
## Fix
A recursive sanitizer (`gemini-schema.ts`) applied at the single
chokepoint `restToSdkGenerateContentParams` — shared by both the
native-Gemini and OpenAI-compat-to-Gemini paths. For any schema node
with an empty or non-string `enum`:
- drop the `enum` (Gemini cannot express the literal anyway);
- preserve the existing `type`, or infer a safe one from the values
(null-only/heterogeneous → `string`, all-float → `number`);
- fold the dropped literal(s) into `description` (e.g. *"Value must be
`true`."*) so the model keeps the hint.
Valid string enums are left untouched. The walk covers all JSON-schema
applicator keywords (`properties`, `anyOf`/`oneOf`/`allOf`,
`items`/`prefixItems`, `$defs`/`definitions`, `additionalProperties`,
`dependencies`/`dependentSchemas`, `unevaluatedItems`,
`if`/`then`/`else`, `not`, `contains`, ...) and never recurses into
data-bearing keys (`enum`/`const`/`default`/`examples`).
### Why drop, not stringify
For tool params forwarded to a strict MCP server, stringifying
(`type:string, enum:["true"]`) would make the model emit `"true"` and
break downstream validation. Dropping keeps the correct JSON type.
## Tests
- `gemini-schema.test.ts` — reported `anyOf` shape,
boolean/numeric/empty/mixed/untyped enums, type inference, description
folding, recursion into
`items`/`$defs`/`dependencies`/`unevaluatedItems`, no input mutation.
- `gemini.test.ts` — wiring: all four fd schema fields (`parameters`,
`parametersJsonSchema`, `response`, `responseJsonSchema`) sanitized on
the outbound path, plus the no-parameters short-circuit.
`type-check`, `lint`, full `knip` all clean.
---
<!-- archestra-banner:v1 -->
<a href="https://archestra.ai/contributor-onboard" rel="nofollow
noreferrer noopener" target="_blank">
<img alt="Archestra Contributor"
src="https://raw.githubusercontent.com/archestra-ai/archestra/main/docs/assets/archestra-contributor-banner.webp"/>
</a>
## References / prior art
- Vercel AI SDK —
[`convert-json-schema-to-openapi-schema.ts`](https://github.com/vercel/ai/blob/main/packages/google/src/convert-json-schema-to-openapi-schema.ts):
rewrites `const` → `enum: [const]` and passes `enum` through verbatim
(no string coercion) — the upstream source of the boolean enum.
- Gemini API — [structured output / enum
docs](https://ai.google.dev/gemini-api/docs/structured-output): `enum`
is only valid on `STRING`-typed fields.
- [Google AI forum: Gemini rejecting enum schema (Zod cannot set `type:
"string"`)](https://discuss.ai.google.dev/t/gemini-rejecting-enum-schema-zod-cannot-set-type-string/90681)
- [`gemini-cli`
archestra-ai#2237](google-gemini/gemini-cli#2237): only
`enum`/`date-time` supported for STRING type — same class of validation.
- [`opencode`
#12784](anomalyco/opencode#12784): handles the
same problem by **stringifying** numeric enums. We deliberately **drop**
instead: stringifying changes the JSON type the model emits, which
breaks tool args validated by a strict downstream MCP server. Dropping
preserves the type and keeps the literal as a description hint.1 parent a66852f commit 18510c4
4 files changed
Lines changed: 404 additions & 6 deletions
File tree
- platform/backend/src/routes/proxy/adapters
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
0 commit comments