@@ -31,6 +31,7 @@ import type { AgentTerminateMode } from '../agents/types.js';
3131import { getCommonAttributes } from './telemetryAttributes.js' ;
3232import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
3333import { safeJsonStringify } from '../utils/safeJsonStringify.js' ;
34+ import type { OTelFinishReason } from './semantic.js' ;
3435import {
3536 toInputMessages ,
3637 toOutputMessages ,
@@ -545,6 +546,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
545546 prompt : GenAIPromptDetails ;
546547 response : GenAIResponseDetails ;
547548 usage : GenAIUsageDetails ;
549+ finish_reasons : OTelFinishReason [ ] ;
548550
549551 constructor (
550552 model : string ,
@@ -573,6 +575,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
573575 tool_token_count : usage_data ?. toolUsePromptTokenCount ?? 0 ,
574576 total_token_count : usage_data ?. totalTokenCount ?? 0 ,
575577 } ;
578+ this . finish_reasons = toFinishReasons ( this . response . candidates ) ;
576579 }
577580
578581 toLogRecord ( config : Config ) : LogRecord {
@@ -591,6 +594,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
591594 prompt_id : this . prompt . prompt_id ,
592595 auth_type : this . auth_type ,
593596 status_code : this . status_code ,
597+ finish_reasons : this . finish_reasons ,
594598 } ;
595599 if ( this . response_text ) {
596600 attributes [ 'response_text' ] = this . response_text ;
@@ -613,9 +617,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
613617 'event.name' : EVENT_GEN_AI_OPERATION_DETAILS ,
614618 'event.timestamp' : this [ 'event.timestamp' ] ,
615619 'gen_ai.response.id' : this . response . response_id ,
616- 'gen_ai.response.finish_reasons' : toFinishReasons (
617- this . response . candidates ,
618- ) ,
620+ 'gen_ai.response.finish_reasons' : this . finish_reasons ,
619621 'gen_ai.output.messages' : JSON . stringify (
620622 toOutputMessages ( this . response . candidates ) ,
621623 ) ,
0 commit comments