@@ -47,13 +47,13 @@ export function extractModel(params: Record<string, unknown>, context?: unknown)
4747 return params . model ;
4848 }
4949
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
50+ // We retrieve the model from the chat context
51+ // because the model is defined when the chat is created,
52+ // not provided as a parameter in chat.sendMessage
5353 if ( context && typeof context === 'object' ) {
5454 const chatObj = context as Record < string , unknown > ;
5555 if ( chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] && typeof chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] === 'string' ) {
56- return chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] as string ;
56+ return chatObj [ GOOGLE_GENAI_MODEL_PROPERTY ] ;
5757 }
5858 }
5959
@@ -238,11 +238,12 @@ function instrumentMethod<T extends unknown[], R>(
238238 if ( finalOptions . recordInputs && args [ 0 ] && typeof args [ 0 ] === 'object' ) {
239239 addPrivateRequestAttributes ( span , args [ 0 ] as Record < string , unknown > ) ;
240240 }
241- const result = ( originalMethod as ( ...args : T ) => R ) . apply ( context , args ) as R ;
241+ const result = ( originalMethod as ( ...args : T ) => R ) . apply ( context , args ) ;
242242
243243 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
244+ // For chat instances, retrieve the model from the chat context.
245+ // The model is defined when the chat is created,
246+ // not provided as a parameter in chat.sendMessage.
246247 ( result as Record < string , unknown > ) [ GOOGLE_GENAI_MODEL_PROPERTY ] = model ;
247248 }
248249
@@ -255,7 +256,7 @@ function instrumentMethod<T extends unknown[], R>(
255256 throw error ;
256257 }
257258 } ,
258- ) as R ;
259+ ) ;
259260 }
260261
261262 // Async/content-producing path
@@ -273,15 +274,15 @@ function instrumentMethod<T extends unknown[], R>(
273274
274275 const result = await Promise . resolve ( ( originalMethod as ( ...args : T ) => Promise < R > ) . apply ( context , args ) ) ;
275276 addResponseAttributes ( span , result as GoogleGenAIResponse , finalOptions . recordOutputs ) ;
276- return result as R ;
277+ return result ;
277278 } catch ( error ) {
278279 captureException ( error , {
279280 mechanism : { handled : false , type : 'auto.ai.google_genai' , data : { function : methodPath } } ,
280281 } ) ;
281282 throw error ;
282283 }
283284 } ,
284- ) as Promise < R > ;
285+ ) ;
285286 } ;
286287
287288 return run ;
0 commit comments