45
45
disabled_ai_monitoring_streaming_settings ,
46
46
events_sans_content ,
47
47
events_sans_llm_metadata ,
48
+ events_with_context_attrs ,
48
49
llm_token_count_callback ,
49
50
set_trace_info ,
50
51
)
58
59
)
59
60
60
61
from newrelic .api .background_task import background_task
62
+ from newrelic .api .llm_custom_attributes import WithLlmCustomAttributes
61
63
from newrelic .api .transaction import add_custom_attribute
62
64
from newrelic .common .object_names import callable_name
63
65
from newrelic .hooks .external_botocore import MODEL_EXTRACTORS
@@ -161,7 +163,7 @@ def expected_invalid_access_key_error_events(model_id):
161
163
def test_bedrock_chat_completion_in_txn_with_llm_metadata (
162
164
set_trace_info , exercise_model , expected_events , expected_metrics
163
165
):
164
- @validate_custom_events (expected_events )
166
+ @validate_custom_events (events_with_context_attrs ( expected_events ) )
165
167
# One summary event, one user message, and one response message from the assistant
166
168
@validate_custom_event_count (count = 3 )
167
169
@validate_transaction_metrics (
@@ -180,7 +182,8 @@ def _test():
180
182
add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
181
183
add_custom_attribute ("llm.foo" , "bar" )
182
184
add_custom_attribute ("non_llm_attr" , "python-agent" )
183
- exercise_model (prompt = _test_bedrock_chat_completion_prompt , temperature = 0.7 , max_tokens = 100 )
185
+ with WithLlmCustomAttributes ({"context" : "attr" }):
186
+ exercise_model (prompt = _test_bedrock_chat_completion_prompt , temperature = 0.7 , max_tokens = 100 )
184
187
185
188
_test ()
186
189
@@ -320,7 +323,7 @@ def _test():
320
323
def test_bedrock_chat_completion_error_invalid_model (
321
324
bedrock_server , set_trace_info , response_streaming , expected_metrics
322
325
):
323
- @validate_custom_events (chat_completion_invalid_model_error_events )
326
+ @validate_custom_events (events_with_context_attrs ( chat_completion_invalid_model_error_events ) )
324
327
@validate_error_trace_attributes (
325
328
"botocore.errorfactory:ValidationException" ,
326
329
exact_attrs = {
@@ -350,22 +353,23 @@ def _test():
350
353
add_custom_attribute ("non_llm_attr" , "python-agent" )
351
354
352
355
with pytest .raises (_client_error ):
353
- if response_streaming :
354
- stream = bedrock_server .invoke_model_with_response_stream (
355
- body = b"{}" ,
356
- modelId = "does-not-exist" ,
357
- accept = "application/json" ,
358
- contentType = "application/json" ,
359
- )
360
- for _ in stream :
361
- pass
362
- else :
363
- bedrock_server .invoke_model (
364
- body = b"{}" ,
365
- modelId = "does-not-exist" ,
366
- accept = "application/json" ,
367
- contentType = "application/json" ,
368
- )
356
+ with WithLlmCustomAttributes ({"context" : "attr" }):
357
+ if response_streaming :
358
+ stream = bedrock_server .invoke_model_with_response_stream (
359
+ body = b"{}" ,
360
+ modelId = "does-not-exist" ,
361
+ accept = "application/json" ,
362
+ contentType = "application/json" ,
363
+ )
364
+ for _ in stream :
365
+ pass
366
+ else :
367
+ bedrock_server .invoke_model (
368
+ body = b"{}" ,
369
+ modelId = "does-not-exist" ,
370
+ accept = "application/json" ,
371
+ contentType = "application/json" ,
372
+ )
369
373
370
374
_test ()
371
375
0 commit comments