@@ -48,8 +48,9 @@ function extractRequestAttributes(args: unknown[], methodPath: string): Record<s
48
48
if ( 'top_p' in params ) attributes [ GEN_AI_REQUEST_TOP_P_ATTRIBUTE ] = params . top_p ;
49
49
if ( 'stream' in params ) attributes [ GEN_AI_REQUEST_STREAM_ATTRIBUTE ] = params . stream ;
50
50
if ( 'top_k' in params ) attributes [ GEN_AI_REQUEST_TOP_K_ATTRIBUTE ] = params . top_k ;
51
- attributes [ GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE ] = params . frequency_penalty ;
52
- attributes [ GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE ] = params . max_tokens ;
51
+ if ( 'frequency_penalty' in params )
52
+ attributes [ GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE ] = params . frequency_penalty ;
53
+ if ( 'max_tokens' in params ) attributes [ GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE ] = params . max_tokens ;
53
54
} else {
54
55
if ( methodPath === 'models.retrieve' || methodPath === 'models.get' ) {
55
56
// models.retrieve(model-id) and models.get(model-id)
@@ -90,7 +91,10 @@ function addResponseAttributes(span: Span, response: AnthropicAiResponse, record
90
91
if ( 'content' in response ) {
91
92
if ( Array . isArray ( response . content ) ) {
92
93
span . setAttributes ( {
93
- [ GEN_AI_RESPONSE_TEXT_ATTRIBUTE ] : response . content . map ( ( item : { text : string } ) => item . text ) . join ( '' ) ,
94
+ [ GEN_AI_RESPONSE_TEXT_ATTRIBUTE ] : response . content
95
+ . map ( ( item : { text : string | undefined } ) => item . text )
96
+ . filter ( ( text ) : text is string => text !== undefined )
97
+ . join ( '' ) ,
94
98
} ) ;
95
99
}
96
100
}
@@ -114,11 +118,11 @@ function addResponseAttributes(span: Span, response: AnthropicAiResponse, record
114
118
span . setAttributes ( {
115
119
[ ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE ] : new Date ( response . created * 1000 ) . toISOString ( ) ,
116
120
} ) ;
117
- if ( 'created_at' in response && typeof response . created_at === 'number' ) {
118
- span . setAttributes ( {
119
- [ ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE ] : new Date ( response . created_at * 1000 ) . toISOString ( ) ,
120
- } ) ;
121
- }
121
+ }
122
+ if ( 'created_at' in response && typeof response . created_at === 'number' ) {
123
+ span . setAttributes ( {
124
+ [ ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE ] : new Date ( response . created_at * 1000 ) . toISOString ( ) ,
125
+ } ) ;
122
126
}
123
127
124
128
if ( response . usage ) {
0 commit comments