Skip to content

Commit 25a09c6

Browse files
authored
Merge pull request #1233 from pnkvalavala/fix/response-schemas-for-gemini-models
fix: handle complex nested schemas in gemini function calling & structured outputs
2 parents cd4ca6c + f7e2703 commit 25a09c6

File tree

5 files changed

+731
-48
lines changed

5 files changed

+731
-48
lines changed

src/providers/google-vertex-ai/chatComplete.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import type {
4949
import {
5050
getMimeType,
5151
recursivelyDeleteUnsupportedParameters,
52+
transformGeminiToolParameters,
5253
transformVertexLogprobs,
5354
} from './utils';
5455

@@ -301,6 +302,11 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = {
301302
) {
302303
tools.push(buildGoogleSearchRetrievalTool(tool));
303304
} else {
305+
if (tool.function?.parameters) {
306+
tool.function.parameters = transformGeminiToolParameters(
307+
tool.function.parameters
308+
);
309+
}
304310
functionDeclarations.push(tool.function);
305311
}
306312
}

src/providers/google-vertex-ai/transformGenerationConfig.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Params } from '../../types/requestBody';
2-
import { derefer, recursivelyDeleteUnsupportedParameters } from './utils';
2+
import {
3+
recursivelyDeleteUnsupportedParameters,
4+
transformGeminiToolParameters,
5+
} from './utils';
36
import { GoogleEmbedParams } from './embed';
47
import { EmbedInstancesData } from './types';
58
/**
@@ -39,20 +42,11 @@ export function transformGenerationConfig(params: Params) {
3942
}
4043
if (params?.response_format?.type === 'json_schema') {
4144
generationConfig['responseMimeType'] = 'application/json';
42-
recursivelyDeleteUnsupportedParameters(
43-
params?.response_format?.json_schema?.schema
44-
);
4545
let schema =
4646
params?.response_format?.json_schema?.schema ??
4747
params?.response_format?.json_schema;
48-
if (Object.keys(schema).includes('$defs')) {
49-
schema = derefer(schema);
50-
delete schema['$defs'];
51-
}
52-
if (Object.hasOwn(schema, '$schema')) {
53-
delete schema['$schema'];
54-
}
55-
generationConfig['responseSchema'] = schema;
48+
recursivelyDeleteUnsupportedParameters(schema);
49+
generationConfig['responseSchema'] = transformGeminiToolParameters(schema);
5650
}
5751

5852
if (params?.thinking) {

0 commit comments

Comments
 (0)