@@ -38,7 +38,7 @@ import {
38
38
type ToolConfig ,
39
39
type UsageMetadata ,
40
40
} from '@google/generative-ai' ;
41
- import { GenkitError , z , type Genkit , type JSONSchema } from 'genkit' ;
41
+ import { GenkitError , z , type JSONSchema } from 'genkit' ;
42
42
import {
43
43
GenerationCommonConfigDescriptions ,
44
44
GenerationCommonConfigSchema ,
@@ -57,8 +57,8 @@ import {
57
57
type ToolResponsePart ,
58
58
} from 'genkit/model' ;
59
59
import { downloadRequestMedia } from 'genkit/model/middleware' ;
60
- import { runInNewSpan } from 'genkit/tracing ' ;
61
- import { getApiKeyFromEnvVar , getGenkitClientHeader } from './common' ;
60
+ import { model } from 'genkit/plugin ' ;
61
+ import { getApiKeyFromEnvVar , getGenkitClientHeader } from './common.js ' ;
62
62
import { handleCacheIfNeeded } from './context-caching' ;
63
63
import { extractCacheConfig } from './context-caching/utils' ;
64
64
@@ -644,26 +644,26 @@ export const gemma3ne4bit = modelRef({
644
644
} ) ;
645
645
646
646
export const SUPPORTED_GEMINI_MODELS = {
647
- 'gemini-1.5-pro' : gemini15Pro ,
648
- 'gemini-1.5-flash' : gemini15Flash ,
649
- 'gemini-1.5-flash-8b' : gemini15Flash8b ,
650
- 'gemini-2.0-pro-exp-02-05' : gemini20ProExp0205 ,
651
- 'gemini-2.0-flash' : gemini20Flash ,
652
- 'gemini-2.0-flash-lite' : gemini20FlashLite ,
653
- 'gemini-2.0-flash-exp' : gemini20FlashExp ,
654
- 'gemini-2.5-pro-exp-03-25' : gemini25ProExp0325 ,
655
- 'gemini-2.5-pro-preview-03-25' : gemini25ProPreview0325 ,
656
- 'gemini-2.5-pro-preview-tts' : gemini25ProPreviewTts ,
657
- 'gemini-2.5-flash-preview-04-17' : gemini25FlashPreview0417 ,
658
- 'gemini-2.5-flash-preview-tts' : gemini25FlashPreviewTts ,
659
- 'gemini-2.5-flash' : gemini25Flash ,
660
- 'gemini-2.5-flash-lite' : gemini25FlashLite ,
661
- 'gemini-2.5-pro' : gemini25Pro ,
662
- 'gemma-3-12b-it' : gemma312bit ,
663
- 'gemma-3-1b-it' : gemma31bit ,
664
- 'gemma-3-27b-it' : gemma327bit ,
665
- 'gemma-3-4b-it' : gemma34bit ,
666
- 'gemma-3n-e4b-it' : gemma3ne4bit ,
647
+ 'googleai/ gemini-1.5-pro' : gemini15Pro ,
648
+ 'googleai/ gemini-1.5-flash' : gemini15Flash ,
649
+ 'googleai/ gemini-1.5-flash-8b' : gemini15Flash8b ,
650
+ 'googleai/ gemini-2.0-pro-exp-02-05' : gemini20ProExp0205 ,
651
+ 'googleai/ gemini-2.0-flash' : gemini20Flash ,
652
+ 'googleai/ gemini-2.0-flash-lite' : gemini20FlashLite ,
653
+ 'googleai/ gemini-2.0-flash-exp' : gemini20FlashExp ,
654
+ 'googleai/ gemini-2.5-pro-exp-03-25' : gemini25ProExp0325 ,
655
+ 'googleai/ gemini-2.5-pro-preview-03-25' : gemini25ProPreview0325 ,
656
+ 'googleai/ gemini-2.5-pro-preview-tts' : gemini25ProPreviewTts ,
657
+ 'googleai/ gemini-2.5-flash-preview-04-17' : gemini25FlashPreview0417 ,
658
+ 'googleai/ gemini-2.5-flash-preview-tts' : gemini25FlashPreviewTts ,
659
+ 'googleai/ gemini-2.5-flash' : gemini25Flash ,
660
+ 'googleai/ gemini-2.5-flash-lite' : gemini25FlashLite ,
661
+ 'googleai/ gemini-2.5-pro' : gemini25Pro ,
662
+ 'googleai/ gemma-3-12b-it' : gemma312bit ,
663
+ 'googleai/ gemma-3-1b-it' : gemma31bit ,
664
+ 'googleai/ gemma-3-27b-it' : gemma327bit ,
665
+ 'googleai/ gemma-3-4b-it' : gemma34bit ,
666
+ 'googleai/ gemma-3n-e4b-it' : gemma3ne4bit ,
667
667
} ;
668
668
669
669
export const GENERIC_GEMINI_MODEL = modelRef ( {
@@ -705,7 +705,7 @@ export type GeminiVersionString =
705
705
* ```js
706
706
* await ai.generate({
707
707
* prompt: 'hi',
708
- * model: gemini('gemini-1.5-flash')
708
+ * model: gemini('googleai/ gemini-1.5-flash')
709
709
* });
710
710
* ```
711
711
*/
@@ -1118,7 +1118,6 @@ export function cleanSchema(schema: JSONSchema): JSONSchema {
1118
1118
* Defines a new GoogleAI model.
1119
1119
*/
1120
1120
export function defineGoogleAIModel ( {
1121
- ai,
1122
1121
name,
1123
1122
apiKey : apiKeyOption ,
1124
1123
apiVersion,
@@ -1127,7 +1126,6 @@ export function defineGoogleAIModel({
1127
1126
defaultConfig,
1128
1127
debugTraces,
1129
1128
} : {
1130
- ai : Genkit ;
1131
1129
name : string ;
1132
1130
apiKey ?: string | false ;
1133
1131
apiVersion ?: string ;
@@ -1154,10 +1152,10 @@ export function defineGoogleAIModel({
1154
1152
? name . substring ( 'googleai/' . length )
1155
1153
: name ;
1156
1154
1157
- const model : ModelReference < z . ZodTypeAny > =
1155
+ const modelReference : ModelReference < z . ZodTypeAny > =
1158
1156
SUPPORTED_GEMINI_MODELS [ apiModelName ] ??
1159
1157
modelRef ( {
1160
- name : `googleai/ ${ apiModelName } ` ,
1158
+ name : name , // Keep the full name for the model reference
1161
1159
info : {
1162
1160
label : `Google AI - ${ apiModelName } ` ,
1163
1161
supports : {
@@ -1173,7 +1171,7 @@ export function defineGoogleAIModel({
1173
1171
} ) ;
1174
1172
1175
1173
const middleware : ModelMiddleware [ ] = [ ] ;
1176
- if ( model . info ?. supports ?. media ) {
1174
+ if ( modelReference . info ?. supports ?. media ) {
1177
1175
// the gemini api doesn't support downloading media from http(s)
1178
1176
middleware . push (
1179
1177
downloadRequestMedia ( {
@@ -1199,12 +1197,11 @@ export function defineGoogleAIModel({
1199
1197
) ;
1200
1198
}
1201
1199
1202
- return ai . defineModel (
1200
+ return model (
1203
1201
{
1204
- apiVersion : 'v2' ,
1205
- name : model . name ,
1206
- ...model . info ,
1207
- configSchema : model . configSchema ,
1202
+ name : modelReference . name ,
1203
+ ...modelReference . info ,
1204
+ configSchema : modelReference . configSchema ,
1208
1205
use : middleware ,
1209
1206
} ,
1210
1207
async ( request , { streamingRequested, sendChunk, abortSignal } ) => {
@@ -1228,7 +1225,7 @@ export function defineGoogleAIModel({
1228
1225
// systemInstructions to be provided as a separate input. The first
1229
1226
// message detected with role=system will be used for systemInstructions.
1230
1227
let systemInstruction : GeminiMessage | undefined = undefined ;
1231
- if ( model . info ?. supports ?. systemRole ) {
1228
+ if ( modelReference . info ?. supports ?. systemRole ) {
1232
1229
const systemMessage = messages . find ( ( m ) => m . role === 'system' ) ;
1233
1230
if ( systemMessage ) {
1234
1231
messages . splice ( messages . indexOf ( systemMessage ) , 1 ) ;
@@ -1306,7 +1303,10 @@ export function defineGoogleAIModel({
1306
1303
generationConfig . responseSchema = cleanSchema ( request . output . schema ) ;
1307
1304
}
1308
1305
1309
- const msg = toGeminiMessage ( messages [ messages . length - 1 ] , model ) ;
1306
+ const msg = toGeminiMessage (
1307
+ messages [ messages . length - 1 ] ,
1308
+ modelReference
1309
+ ) ;
1310
1310
1311
1311
const fromJSONModeScopedGeminiCandidate = (
1312
1312
candidate : GeminiCandidate
@@ -1321,11 +1321,11 @@ export function defineGoogleAIModel({
1321
1321
toolConfig,
1322
1322
history : messages
1323
1323
. slice ( 0 , - 1 )
1324
- . map ( ( message ) => toGeminiMessage ( message , model ) ) ,
1324
+ . map ( ( message ) => toGeminiMessage ( message , modelReference ) ) ,
1325
1325
safetySettings : safetySettingsFromConfig ,
1326
1326
} as StartChatParams ;
1327
1327
const modelVersion = ( versionFromConfig ||
1328
- model . version ||
1328
+ modelReference . version ||
1329
1329
apiModelName ) as string ;
1330
1330
const cacheConfigDetails = extractCacheConfig ( request ) ;
1331
1331
@@ -1426,31 +1426,8 @@ export function defineGoogleAIModel({
1426
1426
} ;
1427
1427
} ;
1428
1428
1429
- // If debugTraces is enable, we wrap the actual model call with a span, add raw
1430
- // API params as for input.
1431
- return debugTraces
1432
- ? await runInNewSpan (
1433
- ai . registry ,
1434
- {
1435
- metadata : {
1436
- name : streamingRequested ? 'sendMessageStream' : 'sendMessage' ,
1437
- } ,
1438
- } ,
1439
- async ( metadata ) => {
1440
- metadata . input = {
1441
- sdk : '@google/generative-ai' ,
1442
- cache : cache ,
1443
- model : genModel . model ,
1444
- chatOptions : updatedChatRequest ,
1445
- parts : msg . parts ,
1446
- options,
1447
- } ;
1448
- const response = await callGemini ( ) ;
1449
- metadata . output = response . custom ;
1450
- return response ;
1451
- }
1452
- )
1453
- : await callGemini ( ) ;
1429
+ // TODO v2: no ai.registry available here; run without the debug span wrapper.
1430
+ return await callGemini ( ) ;
1454
1431
}
1455
1432
) ;
1456
1433
}
0 commit comments