File tree Expand file tree Collapse file tree 3 files changed +24
-30
lines changed Expand file tree Collapse file tree 3 files changed +24
-30
lines changed Original file line number Diff line number Diff line change 1
1
import { Params } from '../../types/requestBody' ;
2
- import { derefer , recursivelyDeleteUnsupportedParameters } from './utils' ;
2
+ import {
3
+ recursivelyDeleteUnsupportedParameters ,
4
+ transformGeminiToolParameters ,
5
+ } from './utils' ;
3
6
import { GoogleEmbedParams } from './embed' ;
4
7
import { EmbedInstancesData } from './types' ;
5
8
/**
@@ -39,20 +42,11 @@ export function transformGenerationConfig(params: Params) {
39
42
}
40
43
if ( params ?. response_format ?. type === 'json_schema' ) {
41
44
generationConfig [ 'responseMimeType' ] = 'application/json' ;
42
- recursivelyDeleteUnsupportedParameters (
43
- params ?. response_format ?. json_schema ?. schema
44
- ) ;
45
45
let schema =
46
46
params ?. response_format ?. json_schema ?. schema ??
47
47
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 ) ;
56
50
}
57
51
58
52
if ( params ?. thinking ) {
Original file line number Diff line number Diff line change @@ -267,18 +267,25 @@ export const transformGeminiToolParameters = (
267
267
if ( key === 'enum' && Array . isArray ( value ) ) {
268
268
transformed . enum = value ;
269
269
transformed . format = 'enum' ;
270
- } else if ( ( key === 'anyOf' || key === 'oneOf' ) && Array . isArray ( value ) ) {
270
+ continue ;
271
+ }
272
+
273
+ if ( ( key === 'anyOf' || key === 'oneOf' ) && Array . isArray ( value ) ) {
271
274
const nonNullItems = value . filter ( ( item ) => ! isNullTypeNode ( item ) ) ;
272
- if ( nonNullItems . length < value . length ) {
273
- // remove `null` type in schema and set nullable: true
274
- transformed [ key ] = transformNode ( nonNullItems ) ;
275
- transformed . nullable = true ;
276
- } else {
277
- transformed [ key ] = transformNode ( value ) ;
275
+ const hadNull = nonNullItems . length < value . length ;
276
+ if ( nonNullItems . length === 1 && hadNull ) {
277
+ // Flatten to single schema: get rid of anyOf/oneOf and set nullable: true
278
+ const single = transformNode ( nonNullItems [ 0 ] ) ;
279
+ if ( single && typeof single === 'object' ) single . nullable = true ;
280
+ return single ;
278
281
}
279
- } else {
280
- transformed [ key ] = transformNode ( value ) ;
282
+
283
+ transformed [ key ] = transformNode ( hadNull ? nonNullItems : value ) ;
284
+ if ( hadNull ) transformed . nullable = true ;
285
+ continue ;
281
286
}
287
+
288
+ transformed [ key ] = transformNode ( value ) ;
282
289
}
283
290
return transformed ;
284
291
} ;
Original file line number Diff line number Diff line change @@ -11,7 +11,6 @@ import {
11
11
} from '../../types/requestBody' ;
12
12
import { buildGoogleSearchRetrievalTool } from '../google-vertex-ai/chatComplete' ;
13
13
import {
14
- derefer ,
15
14
getMimeType ,
16
15
recursivelyDeleteUnsupportedParameters ,
17
16
transformGeminiToolParameters ,
@@ -63,17 +62,11 @@ const transformGenerationConfig = (params: Params) => {
63
62
}
64
63
if ( params ?. response_format ?. type === 'json_schema' ) {
65
64
generationConfig [ 'responseMimeType' ] = 'application/json' ;
66
- recursivelyDeleteUnsupportedParameters (
67
- params ?. response_format ?. json_schema ?. schema
68
- ) ;
69
65
let schema =
70
66
params ?. response_format ?. json_schema ?. schema ??
71
67
params ?. response_format ?. json_schema ;
72
- if ( Object . keys ( schema ) . includes ( '$defs' ) ) {
73
- schema = derefer ( schema ) ;
74
- delete schema [ '$defs' ] ;
75
- }
76
- generationConfig [ 'responseSchema' ] = schema ;
68
+ recursivelyDeleteUnsupportedParameters ( schema ) ;
69
+ generationConfig [ 'responseSchema' ] = transformGeminiToolParameters ( schema ) ;
77
70
}
78
71
if ( params ?. thinking ) {
79
72
const thinkingConfig : Record < string , any > = { } ;
You can’t perform that action at this time.
0 commit comments