Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed gemini model handling of nullish optional fields in response schema #1411

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion js/plugins/googleai/src/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export function fromGeminiCandidate(
};
}

function cleanSchema(schema: JSONSchema): JSONSchema {
export function cleanSchema(schema: JSONSchema): JSONSchema {
const out = structuredClone(schema);
for (const key in out) {
if (key === '$schema' || key === 'additionalProperties') {
Expand All @@ -459,6 +459,12 @@ function cleanSchema(schema: JSONSchema): JSONSchema {
if (typeof out[key] === 'object') {
out[key] = cleanSchema(out[key]);
}
// Zod nullish() and picoschema optional fields will produce type `["string", "null"]`
// which is not supported by the model API. Convert them to just `"string"`.
if (key === 'type' && Array.isArray(out[key])) {
// find the first that's not `null`.
out[key] = out[key].find((t) => t !== 'null');
}
}
return out;
}
Expand Down
33 changes: 33 additions & 0 deletions js/plugins/googleai/tests/gemini_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { GenerateContentCandidate } from '@google/generative-ai';
import assert from 'node:assert';
import { describe, it } from 'node:test';
import {
cleanSchema,
fromGeminiCandidate,
toGeminiMessage,
toGeminiSystemInstruction,
Expand Down Expand Up @@ -345,3 +346,35 @@ describe('fromGeminiCandidate', () => {
});
}
});

describe('cleanSchema', () => {
it('strips nulls from type', () => {
const cleaned = cleanSchema({
type: 'object',
properties: {
title: {
type: 'string',
},
subtitle: {
type: ['string', 'null'],
},
},
required: ['title'],
additionalProperties: true,
$schema: 'http://json-schema.org/draft-07/schema#',
});

assert.deepStrictEqual(cleaned, {
type: 'object',
properties: {
title: {
type: 'string',
},
subtitle: {
type: 'string',
},
},
required: ['title'],
});
});
});
8 changes: 7 additions & 1 deletion js/plugins/vertexai/src/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ const convertSchemaProperty = (property) => {
}
};

function cleanSchema(schema: JSONSchema): JSONSchema {
export function cleanSchema(schema: JSONSchema): JSONSchema {
const out = structuredClone(schema);
for (const key in out) {
if (key === '$schema' || key === 'additionalProperties') {
Expand All @@ -425,6 +425,12 @@ function cleanSchema(schema: JSONSchema): JSONSchema {
if (typeof out[key] === 'object') {
out[key] = cleanSchema(out[key]);
}
// Zod nullish() and picoschema optional fields will produce type `["string", "null"]`
// which is not supported by the model API. Convert them to just `"string"`.
if (key === 'type' && Array.isArray(out[key])) {
// find the first that's not `null`.
out[key] = out[key].find((t) => t !== 'null');
}
}
return out;
}
Expand Down
33 changes: 33 additions & 0 deletions js/plugins/vertexai/tests/gemini_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { MessageData } from 'genkit';
import assert from 'node:assert';
import { describe, it } from 'node:test';
import {
cleanSchema,
fromGeminiCandidate,
toGeminiMessage,
toGeminiSystemInstruction,
Expand Down Expand Up @@ -348,3 +349,35 @@ describe('fromGeminiCandidate', () => {
});
}
});

describe('cleanSchema', () => {
it('strips nulls from type', () => {
const cleaned = cleanSchema({
type: 'object',
properties: {
title: {
type: 'string',
},
subtitle: {
type: ['string', 'null'],
},
},
required: ['title'],
additionalProperties: true,
$schema: 'http://json-schema.org/draft-07/schema#',
});

assert.deepStrictEqual(cleaned, {
type: 'object',
properties: {
title: {
type: 'string',
},
subtitle: {
type: 'string',
},
},
required: ['title'],
});
});
});