Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(gemini): give every array an items schema; tuples/prefixItems → anyOf (#1334) (#1338)

tashdroid7f5974417800
diff --git a/server/src/__tests__/lib/gemini-schema-compat.test.ts b/server/src/__tests__/lib/gemini-schema-compat.test.ts
index 723112c1..96488593 100644
--- a/server/src/__tests__/lib/gemini-schema-compat.test.ts
+++ b/server/src/__tests__/lib/gemini-schema-compat.test.ts
@@ -252,3 +252,79 @@ describe('sanitizeForGemini vendor extensions and pass-through', () => {
expect(JSON.stringify(sanitizeForGemini(input))).toBe(JSON.stringify(input));
});
});
+
+describe('sanitizeForGemini items shapes (#1334)', () => {
+ // Gemini requires `items` on every ARRAY ("…items: missing field" otherwise)
+ // and rejects a list-valued `items`; `items: {}` and `items: { anyOf }` are
+ // accepted. Shapes below were checked against the live API.
+ it('reproduces #1334: a nested array whose tuple lived in prefixItems gets items', () => {
+ const out = sanitizeForGemini({
+ type: 'object',
+ properties: {
+ query: {
+ type: 'object',
+ properties: {
+ where: {
+ type: 'array',
+ items: { type: 'array', prefixItems: [{ type: 'string' }, { type: 'string' }, {}] },
+ },
+ },
+ },
+ },
+ }) as any;
+ expect(out.properties.query.properties.where).toEqual({ type: 'array', items: { type: 'array', items: {} } });
+ });
+
+ it('turns prefixItems into an anyOf over its members', () => {
+ const out = sanitizeForGemini({
+ type: 'array',
+ prefixItems: [{ type: 'string' }, { type: 'integer' }],
+ }) as Record<string, unknown>;
+ expect(out.items).toEqual({ anyOf: [{ type: 'string' }, { type: 'integer' }] });
+ });
+
+ it('collapses a tuple items array into anyOf over its members', () => {
+ const out = sanitizeForGemini({
+ type: 'array',
+ items: [{ type: 'string' }, { type: 'integer' }],
+ }) as Record<string, unknown>;
+ expect(out.items).toEqual({ anyOf: [{ type: 'string' }, { type: 'integer' }] });
+ });
+
+ it('unwraps a single-member tuple and keeps its keywords', () => {
+ const out = sanitizeForGemini({
+ type: 'array',
+ items: [{ type: 'string', enum: ['a', 'b'] }],
+ }) as Record<string, unknown>;
+ expect(out.items).toEqual({ type: 'string', enum: ['a', 'b'] });
+ });
+
+ it('adds items: {} to an array that has none', () => {
+ expect(sanitizeForGemini({ type: 'array' })).toEqual({ type: 'array', items: {} });
+ expect(sanitizeForGemini({ type: ['array', 'null'] })).toEqual({ type: 'array', nullable: true, items: {} });
+ });
+
+ it('keeps an empty items object instead of dropping it', () => {
+ expect(sanitizeForGemini({ type: 'array', items: {} })).toEqual({ type: 'array', items: {} });
+ });
+
+ it('maps boolean, null and schema-less tuple items to items: {}', () => {
+ expect(sanitizeForGemini({ type: 'array', items: true })).toEqual({ type: 'array', items: {} });
+ expect(sanitizeForGemini({ type: 'array', items: null })).toEqual({ type: 'array', items: {} });
+ expect(sanitizeForGemini({ type: 'array', items: [{ type: 'string' }, true] })).toEqual({ type: 'array', items: {} });
+ });
+
+ it('does not treat parameters named items or type as keywords', () => {
+ const input = { type: 'object', properties: { items: { type: 'string' }, type: { type: 'string' } } };
+ expect(sanitizeForGemini(input)).toEqual(input);
+ });
+
+ it('leaves non-array schemas without items alone', () => {
+ expect(sanitizeForGemini({ type: 'string' })).toEqual({ type: 'string' });
+ });
+
+ it('keeps a normal single-schema items untouched', () => {
+ const input = { type: 'array', items: { type: 'string', description: 'tag' } };
+ expect(JSON.stringify(sanitizeForGemini(input))).toBe(JSON.stringify(input));
+ });
+});
diff --git a/server/src/__tests__/providers/google-schema.test.ts b/server/src/__tests__/providers/google-schema.test.ts
index e0fd1a2e..d28055d6 100644
--- a/server/src/__tests__/providers/google-schema.test.ts
+++ b/server/src/__tests__/providers/google-schema.test.ts
@@ -198,11 +198,13 @@ describe('sanitizeForGemini', () => {
count: { type: 'number', multipleOf: 0.5 },
},
};
+ // prefixItems/contains are stripped, but Gemini requires `items` on every
+ // array (#1334), so the tuple's member becomes `items` and `bag` gets `{}`.
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: {
- tuple: { type: 'array' },
- bag: { type: 'array' },
+ tuple: { type: 'array', items: { type: 'string' } },
+ bag: { type: 'array', items: {} },
names: { type: 'object' },
count: { type: 'number' },
},
diff --git a/server/src/lib/gemini-wire.ts b/server/src/lib/gemini-wire.ts
index eb42e236..dd818d23 100644
--- a/server/src/lib/gemini-wire.ts
+++ b/server/src/lib/gemini-wire.ts
@@ -188,7 +188,53 @@ function sanitizeSchema(schema: unknown, insidePropertiesMap: boolean, ctx: Sani
if (nullable) out.nullable = true;
// Keywords written alongside a `$ref` (description, overrides) win over the
// definition they point at.
- return inlined ? { ...inlined, ...out } : out;
+ const merged = inlined ? { ...inlined, ...out } : out;
+ // A `properties` map is keyed by parameter names, not keywords; a tool
+ // param literally named `items` or `type` must not be read as a schema.
+ if (insidePropertiesMap) return merged;
+ return normalizeArrayItems(merged, schema as Record<string, unknown>, ctx);
+}
+
+// Gemini's Schema proto models `items` as a single Schema message and requires
+// it on every ARRAY: an array without `items` 400s with "…items: missing
+// field" (#1334 — Claude Code's nested tuple params lose their element schema
+// once `prefixItems` is stripped), and a JSON Schema tuple (`items: [...]`)
+// 400s with "Proto field is not repeating". Tuples become an `anyOf` over their
+// members, and an array with no usable element schema gets `items: {}`, which
+// Gemini accepts as "any element" (both verified against the live API).
+function normalizeArrayItems(
+ merged: Record<string, unknown>,
+ source: Record<string, unknown>,
+ ctx: SanitizeContext,
+): Record<string, unknown> {
+ const members: unknown[] = [];
+ if (Array.isArray(source.prefixItems)) {
+ members.push(...(sanitizeSchema(source.prefixItems, false, ctx) as unknown[]));
+ }
+ let single: Record<string, unknown> | undefined;
+ if (Array.isArray(merged.items)) members.push(...merged.items);
+ else if (isSchemaObject(merged.items)) single = merged.items;
+
+ if (members.length > 0) {
+ const branches = [...members, ...(single ? [single] : [])];
+ if (branches.every(isSchemaObject) && branches.every(b => Object.keys(b).length > 0)) {
+ merged.items = branches.length === 1 ? branches[0] : { anyOf: branches };
+ } else {
+ // A boolean/empty member accepts anything, so the union does too.
+ merged.items = {};
+ }
+ } else if (single === undefined && ('items' in merged || isArrayType(merged.type))) {
+ merged.items = {};
+ }
+ return merged;
+}
+
+function isSchemaObject(value: unknown): value is Record<string, unknown> {
+ return !!value && typeof value === 'object' && !Array.isArray(value);
+}
+
+function isArrayType(type: unknown): boolean {
+ return typeof type === 'string' && type.toLowerCase() === 'array';
}
function serializeResponse(value: unknown): string {