@@ -20,14 +20,7 @@ import {
20
20
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE ,
21
21
} from '../ai/gen-ai-attributes' ;
22
22
import { buildMethodPath , getFinalOperationName , getSpanOperation } from '../ai/utils' ;
23
- import { isThenable } from '../is' ;
24
- import {
25
- CHAT_PATH ,
26
- CHATS_CREATE_METHOD ,
27
- GOOGLE_GENAI_INTEGRATION_NAME ,
28
- GOOGLE_GENAI_MODEL_PROPERTY ,
29
- GOOGLE_GENAI_SYSTEM_NAME ,
30
- } from './constants' ;
23
+ import { CHAT_PATH , CHATS_CREATE_METHOD , GOOGLE_GENAI_INTEGRATION_NAME , GOOGLE_GENAI_SYSTEM_NAME } from './constants' ;
31
24
import type {
32
25
Candidate ,
33
26
ContentPart ,
@@ -39,21 +32,26 @@ import type {
39
32
import { shouldInstrument } from './utils' ;
40
33
41
34
/**
42
- * Extract model from parameters or context
43
- * For chat instances, the model is stored during chat creation and retrieved from context
35
+ * Extract model from parameters or chat context object
36
+ * For chat instances, the model is available on the chat object as 'model' (older versions) or 'modelVersion' (newer versions)
44
37
*/
45
38
export function extractModel ( params : Record < string , unknown > , context ?: unknown ) : string {
46
39
if ( 'model' in params && typeof params . model === 'string' ) {
47
40
return params . model ;
48
41
}
49
42
50
- // For chat instances, try to get the model from the chat context
51
- // This is because the model is set during chat creation
52
- // and not passed as a parameter to the chat.sendMessage method
43
+ // Try to get model from chat context object (chat instance has model property)
53
44
if ( context && typeof context === 'object' ) {
54
- const chatObj = context as Record < string , unknown > ;
55
- if ( chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] && typeof chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] === 'string' ) {
56
- return chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] as string ;
45
+ const contextObj = context as Record < string , unknown > ;
46
+
47
+ // Check for 'model' property (older versions, and streaming)
48
+ if ( 'model' in contextObj && typeof contextObj . model === 'string' ) {
49
+ return contextObj . model ;
50
+ }
51
+
52
+ // Check for 'modelVersion' property (newer versions)
53
+ if ( 'modelVersion' in contextObj && typeof contextObj . modelVersion === 'string' ) {
54
+ return contextObj . modelVersion ;
57
55
}
58
56
}
59
57
@@ -217,7 +215,7 @@ function instrumentMethod<T extends unknown[], R>(
217
215
context : unknown ,
218
216
options ?: GoogleGenAIOptions ,
219
217
) : ( ...args : T ) => R | Promise < R > {
220
- const isSyncCreate = ! isThenable ( originalMethod ) && methodPath === CHATS_CREATE_METHOD ;
218
+ const isSyncCreate = methodPath === CHATS_CREATE_METHOD
221
219
222
220
const run = ( ...args : T ) : R | Promise < R > => {
223
221
const finalOptions = options || getRecordingOptionsFromIntegration ( ) ;
@@ -238,13 +236,7 @@ function instrumentMethod<T extends unknown[], R>(
238
236
if ( finalOptions . recordInputs && args [ 0 ] && typeof args [ 0 ] === 'object' ) {
239
237
addPrivateRequestAttributes ( span , args [ 0 ] as Record < string , unknown > ) ;
240
238
}
241
- const result = ( originalMethod as ( ...args : T ) => R ) . apply ( context , args ) as R ;
242
-
243
- if ( typeof model === 'string' && model !== 'unknown' && typeof result === 'object' ) {
244
- // We store the model in the result object so that it can be accessed later
245
- // This is because the model is not passed as a parameter to the chat.sendMessage method
246
- ( result as Record < string , unknown > ) [ GOOGLE_GENAI_MODEL_PROPERTY ] = model ;
247
- }
239
+ const result = ( originalMethod as ( ...args : T ) => R ) . apply ( context , args ) ;
248
240
249
241
// No response attributes for create (returns object of chat instance, not generated content)
250
242
return result ;
@@ -255,7 +247,7 @@ function instrumentMethod<T extends unknown[], R>(
255
247
throw error ;
256
248
}
257
249
} ,
258
- ) as R ;
250
+ ) ;
259
251
}
260
252
261
253
// Async/content-producing path
@@ -273,15 +265,15 @@ function instrumentMethod<T extends unknown[], R>(
273
265
274
266
const result = await Promise . resolve ( ( originalMethod as ( ...args : T ) => Promise < R > ) . apply ( context , args ) ) ;
275
267
addResponseAttributes ( span , result as GoogleGenAIResponse , finalOptions . recordOutputs ) ;
276
- return result as R ;
268
+ return result ;
277
269
} catch ( error ) {
278
270
captureException ( error , {
279
271
mechanism : { handled : false , type : 'auto.ai.google_genai' , data : { function : methodPath } } ,
280
272
} ) ;
281
273
throw error ;
282
274
}
283
275
} ,
284
- ) as Promise < R > ;
276
+ ) ;
285
277
} ;
286
278
287
279
return run ;
0 commit comments