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(google): sanitize JSON Schema fields Gemini rejects (#65, credits @danscMax)

danscMax6929cd5e2f71
diff --git a/server/src/__tests__/providers/google-schema.test.ts b/server/src/__tests__/providers/google-schema.test.ts
new file mode 100644
index 00000000..bc6f952a
--- /dev/null
+++ b/server/src/__tests__/providers/google-schema.test.ts
@@ -0,0 +1,97 @@
+import { describe, it, expect } from 'vitest';
+import { sanitizeForGemini } from '../../providers/google.js';
+
+describe('sanitizeForGemini', () => {
+ it('strips top-level JSON-Schema-only fields that Google rejects', () => {
+ const input = {
+ $schema: 'https://json-schema.org/draft/2020-12/schema',
+ $id: 'http://example.com/foo.json',
+ type: 'object',
+ properties: { name: { type: 'string' } },
+ };
+ expect(sanitizeForGemini(input)).toEqual({
+ type: 'object',
+ properties: { name: { type: 'string' } },
+ });
+ });
+
+ it('strips exclusiveMinimum / exclusiveMaximum recursively in nested properties', () => {
+ const input = {
+ type: 'object',
+ properties: {
+ temp_threshold: { type: 'number', exclusiveMinimum: 0, exclusiveMaximum: 100 },
+ nested: {
+ type: 'object',
+ properties: {
+ count: { type: 'integer', exclusiveMinimum: 1 },
+ },
+ },
+ },
+ };
+ expect(sanitizeForGemini(input)).toEqual({
+ type: 'object',
+ properties: {
+ temp_threshold: { type: 'number' },
+ nested: {
+ type: 'object',
+ properties: {
+ count: { type: 'integer' },
+ },
+ },
+ },
+ });
+ });
+
+ it('walks through arrays (e.g. anyOf branches)', () => {
+ const input = {
+ anyOf: [
+ { type: 'string', $ref: '#/definitions/foo' },
+ { type: 'number', exclusiveMinimum: 0 },
+ ],
+ };
+ expect(sanitizeForGemini(input)).toEqual({
+ anyOf: [
+ { type: 'string' },
+ { type: 'number' },
+ ],
+ });
+ });
+
+ it('removes $defs / definitions / patternProperties / if-then-else', () => {
+ const input = {
+ type: 'object',
+ $defs: { Foo: { type: 'string' } },
+ definitions: { Bar: { type: 'integer' } },
+ patternProperties: { '^x-': { type: 'string' } },
+ if: { properties: { kind: { const: 'A' } } },
+ then: { required: ['extra'] },
+ else: { required: [] },
+ properties: { kind: { type: 'string' } },
+ };
+ expect(sanitizeForGemini(input)).toEqual({
+ type: 'object',
+ properties: { kind: { type: 'string' } },
+ });
+ });
+
+ it('passes through supported OpenAPI fields untouched', () => {
+ const input = {
+ type: 'object',
+ description: 'A tool input',
+ required: ['city'],
+ properties: {
+ city: { type: 'string', description: 'City name', enum: ['Karachi', 'Lahore'] },
+ items: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 10 },
+ },
+ };
+ expect(sanitizeForGemini(input)).toEqual(input);
+ });
+
+ it('handles primitives and null safely', () => {
+ expect(sanitizeForGemini(null)).toBe(null);
+ expect(sanitizeForGemini(undefined)).toBe(undefined);
+ expect(sanitizeForGemini('hello')).toBe('hello');
+ expect(sanitizeForGemini(42)).toBe(42);
+ expect(sanitizeForGemini(true)).toBe(true);
+ });
+});
diff --git a/server/src/providers/google.ts b/server/src/providers/google.ts
index 532d18d6..beb6dfa0 100644
--- a/server/src/providers/google.ts
+++ b/server/src/providers/google.ts
@@ -67,6 +67,34 @@ function toGeminiFinishReason(finishReason?: string): string {
return 'stop';
}
+// Google Gemini accepts only a subset of JSON Schema (~OpenAPI 3.0).
+// Strip fields that opencode / other strict-JSON-Schema clients send but
+// Google rejects with 400 "Unknown name '<field>'".
+const GEMINI_UNSUPPORTED_SCHEMA_KEYS = new Set([
+ '$schema', '$id', '$ref', '$defs', '$comment',
+ 'definitions',
+ 'exclusiveMinimum', 'exclusiveMaximum',
+ 'patternProperties', 'unevaluatedProperties', 'unevaluatedItems',
+ 'if', 'then', 'else',
+ 'contentEncoding', 'contentMediaType', 'contentSchema',
+ 'dependentRequired', 'dependentSchemas',
+]);
+
+export function sanitizeForGemini(schema: unknown): unknown {
+ if (Array.isArray(schema)) {
+ return schema.map(sanitizeForGemini);
+ }
+ if (schema && typeof schema === 'object') {
+ const out: Record<string, unknown> = {};
+ for (const [k, v] of Object.entries(schema as Record<string, unknown>)) {
+ if (GEMINI_UNSUPPORTED_SCHEMA_KEYS.has(k)) continue;
+ out[k] = sanitizeForGemini(v);
+ }
+ return out;
+ }
+ return schema;
+}
+
function toGeminiTools(tools?: ChatToolDefinition[]): Array<{ functionDeclarations: Array<Record<string, unknown>> }> | undefined {
if (!tools || tools.length === 0) return undefined;
@@ -74,7 +102,7 @@ function toGeminiTools(tools?: ChatToolDefinition[]): Array<{ functionDeclaratio
functionDeclarations: tools.map(t => ({
name: t.function.name,
description: t.function.description,
- parameters: t.function.parameters,
+ parameters: sanitizeForGemini(t.function.parameters),
})),
}];
}