@@ -117,20 +117,14 @@ function addResponseAttributes(span: Span, result: unknown, recordOutputs?: bool
117117
118118// Extract and record AI request inputs, if present. This is intentionally separate from response attributes.
119119function addRequestAttributes ( span : Span , params : Record < string , unknown > ) : void {
120- if ( 'messages' in params ) {
121- const truncatedMessages = getTruncatedJsonString ( params . messages ) ;
122- span . setAttributes ( {
123- [ GEN_AI_REQUEST_MESSAGES_ATTRIBUTE ] : truncatedMessages ,
124- [ GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE ] : undefined ,
125- } ) ;
126- if ( Array . isArray ( params . messages ) ) {
127- span . setAttribute ( GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE , params . messages . length ) ;
128- }
129- } else if ( 'input' in params ) {
130- const truncatedInput = getTruncatedJsonString ( params . input ) ;
120+ const src = 'input' in params ? params . input : 'messages' in params ? params . messages : undefined ;
121+ // typically an array, but can be other types. skip if an empty array.
122+ const length = Array . isArray ( src ) ? src . length : undefined ;
123+ if ( src && length !== 0 ) {
124+ const truncatedInput = getTruncatedJsonString ( src ) ;
131125 span . setAttribute ( GEN_AI_REQUEST_MESSAGES_ATTRIBUTE , truncatedInput ) ;
132- if ( Array . isArray ( params . input ) ) {
133- span . setAttribute ( GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE , params . input . length ) ;
126+ if ( length ) {
127+ span . setAttribute ( GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE , length ) ;
134128 }
135129 }
136130}
0 commit comments