4545 disabled_ai_monitoring_streaming_settings ,
4646 events_sans_content ,
4747 events_sans_llm_metadata ,
48+ events_with_context_attrs ,
4849 llm_token_count_callback ,
4950 set_trace_info ,
5051)
5859)
5960
6061from newrelic .api .background_task import background_task
62+ from newrelic .api .llm_custom_attributes import WithLlmCustomAttributes
6163from newrelic .api .transaction import add_custom_attribute
6264from newrelic .common .object_names import callable_name
6365from newrelic .hooks .external_botocore import MODEL_EXTRACTORS
@@ -161,7 +163,7 @@ def expected_invalid_access_key_error_events(model_id):
161163def test_bedrock_chat_completion_in_txn_with_llm_metadata (
162164 set_trace_info , exercise_model , expected_events , expected_metrics
163165):
164- @validate_custom_events (expected_events )
166+ @validate_custom_events (events_with_context_attrs ( expected_events ) )
165167 # One summary event, one user message, and one response message from the assistant
166168 @validate_custom_event_count (count = 3 )
167169 @validate_transaction_metrics (
@@ -180,7 +182,8 @@ def _test():
180182 add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
181183 add_custom_attribute ("llm.foo" , "bar" )
182184 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 )
184187
185188 _test ()
186189
@@ -320,7 +323,7 @@ def _test():
320323def test_bedrock_chat_completion_error_invalid_model (
321324 bedrock_server , set_trace_info , response_streaming , expected_metrics
322325):
323- @validate_custom_events (chat_completion_invalid_model_error_events )
326+ @validate_custom_events (events_with_context_attrs ( chat_completion_invalid_model_error_events ) )
324327 @validate_error_trace_attributes (
325328 "botocore.errorfactory:ValidationException" ,
326329 exact_attrs = {
@@ -350,22 +353,23 @@ def _test():
350353 add_custom_attribute ("non_llm_attr" , "python-agent" )
351354
352355 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+ )
369373
370374 _test ()
371375
0 commit comments