@@ -207,9 +207,26 @@ def test_bedrock_chat_completion_disabled_ai_monitoring_settings(set_trace_info,
207207_client_error_name = callable_name (_client_error )
208208
209209
210+ @pytest .fixture
211+ def exercise_converse_incorrect_access_key (bedrock_converse_server , response_streaming , monkeypatch ):
212+ def _exercise_converse_incorrect_access_key ():
213+ monkeypatch .setattr (bedrock_converse_server ._request_signer ._credentials , "access_key" , "INVALID-ACCESS-KEY" )
214+
215+ message = [{"role" : "user" , "content" : [{"text" : "Invalid Token" }]}]
216+ request = bedrock_converse_server .converse_stream if response_streaming else bedrock_converse_server .converse
217+ with pytest .raises (_client_error ):
218+ request (
219+ modelId = "anthropic.claude-3-sonnet-20240229-v1:0" ,
220+ messages = message ,
221+ inferenceConfig = {"temperature" : 0.7 , "maxTokens" : 100 },
222+ )
223+
224+ return _exercise_converse_incorrect_access_key
225+
226+
210227@reset_core_stats_engine ()
211228def test_bedrock_chat_completion_error_incorrect_access_key (
212- monkeypatch , bedrock_converse_server , response_streaming , set_trace_info , expected_metric
229+ exercise_converse_incorrect_access_key , set_trace_info , expected_metric
213230):
214231 """
215232 A request is made to the server with invalid credentials. botocore will reach out to the server and receive an
@@ -239,79 +256,75 @@ def test_bedrock_chat_completion_error_incorrect_access_key(
239256 )
240257 @background_task (name = "test_bedrock_chat_completion" )
241258 def _test ():
242- monkeypatch .setattr (bedrock_converse_server ._request_signer ._credentials , "access_key" , "INVALID-ACCESS-KEY" )
243-
244- with pytest .raises (_client_error ):
245- set_trace_info ()
246- add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
247- add_custom_attribute ("llm.foo" , "bar" )
248- add_custom_attribute ("non_llm_attr" , "python-agent" )
259+ set_trace_info ()
260+ add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
261+ add_custom_attribute ("llm.foo" , "bar" )
262+ add_custom_attribute ("non_llm_attr" , "python-agent" )
249263
250- message = [{"role" : "user" , "content" : [{"text" : "Invalid Token" }]}]
251-
252- request = (
253- bedrock_converse_server .converse_stream if response_streaming else bedrock_converse_server .converse
254- )
255- request (
256- modelId = "anthropic.claude-3-sonnet-20240229-v1:0" ,
257- messages = message ,
258- inferenceConfig = {"temperature" : 0.7 , "maxTokens" : 100 },
259- )
264+ exercise_converse_incorrect_access_key ()
260265
261266 _test ()
262267
263268
264269@reset_core_stats_engine ()
265- def test_bedrock_chat_completion_error_invalid_model (
266- bedrock_converse_server , response_streaming , set_trace_info , expected_metric
270+ @override_llm_token_callback_settings (llm_token_count_callback )
271+ def test_bedrock_chat_completion_error_incorrect_access_key_with_token_count (
272+ exercise_converse_incorrect_access_key , set_trace_info , expected_metric
267273):
268- @validate_custom_events (events_with_context_attrs (chat_completion_invalid_model_error_events ))
274+ """
275+ A request is made to the server with invalid credentials. botocore will reach out to the server and receive an
276+ UnrecognizedClientException as a response. Information from the request will be parsed and reported in customer
277+ events. The error response can also be parsed, and will be included as attributes on the recorded exception.
278+ """
279+
280+ @validate_custom_events (add_token_count_to_events (chat_completion_invalid_access_key_error_events ))
269281 @validate_error_trace_attributes (
270- "botocore.errorfactory:ValidationException" ,
282+ _client_error_name ,
271283 exact_attrs = {
272284 "agent" : {},
273285 "intrinsic" : {},
274286 "user" : {
275- "http.statusCode" : 400 ,
276- "error.message" : "The provided model identifier is invalid." ,
277- "error.code" : "ValidationException " ,
287+ "http.statusCode" : 403 ,
288+ "error.message" : "The security token included in the request is invalid." ,
289+ "error.code" : "UnrecognizedClientException " ,
278290 },
279291 },
280292 )
281293 @validate_transaction_metrics (
282- name = "test_bedrock_chat_completion_error_invalid_model " ,
294+ name = "test_bedrock_chat_completion_incorrect_access_key_with_token_count " ,
283295 scoped_metrics = [expected_metric ],
284296 rollup_metrics = [expected_metric ],
285297 custom_metrics = [(f"Supportability/Python/ML/Bedrock/{ BOTOCORE_VERSION } " , 1 )],
286298 background_task = True ,
287299 )
288- @background_task (name = "test_bedrock_chat_completion_error_invalid_model " )
300+ @background_task (name = "test_bedrock_chat_completion_incorrect_access_key_with_token_count " )
289301 def _test ():
290302 set_trace_info ()
291303 add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
292304 add_custom_attribute ("llm.foo" , "bar" )
293305 add_custom_attribute ("non_llm_attr" , "python-agent" )
294306
295- with pytest .raises (_client_error ):
296- with WithLlmCustomAttributes ({"context" : "attr" }):
297- message = [{"role" : "user" , "content" : [{"text" : "Model does not exist." }]}]
298-
299- request = (
300- bedrock_converse_server .converse_stream if response_streaming else bedrock_converse_server .converse
301- )
302- request (
303- modelId = "does-not-exist" , messages = message , inferenceConfig = {"temperature" : 0.7 , "maxTokens" : 100 }
304- )
307+ exercise_converse_incorrect_access_key ()
305308
306309 _test ()
307310
308311
312+ @pytest .fixture
313+ def exercise_converse_invalid_model (bedrock_converse_server , response_streaming , monkeypatch ):
314+ def _exercise_converse_invalid_model ():
315+ monkeypatch .setattr (bedrock_converse_server ._request_signer ._credentials , "access_key" , "INVALID-ACCESS-KEY" )
316+
317+ message = [{"role" : "user" , "content" : [{"text" : "Model does not exist." }]}]
318+ request = bedrock_converse_server .converse_stream if response_streaming else bedrock_converse_server .converse
319+ with pytest .raises (_client_error ):
320+ request (modelId = "does-not-exist" , messages = message , inferenceConfig = {"temperature" : 0.7 , "maxTokens" : 100 })
321+
322+ return _exercise_converse_invalid_model
323+
324+
309325@reset_core_stats_engine ()
310- @disabled_ai_monitoring_record_content_settings
311- def test_bedrock_chat_completion_error_invalid_model_no_content (
312- bedrock_converse_server , response_streaming , set_trace_info , expected_metric
313- ):
314- @validate_custom_events (events_sans_content (chat_completion_invalid_model_error_events ))
326+ def test_bedrock_chat_completion_error_invalid_model (exercise_converse_invalid_model , set_trace_info , expected_metric ):
327+ @validate_custom_events (events_with_context_attrs (chat_completion_invalid_model_error_events ))
315328 @validate_error_trace_attributes (
316329 "botocore.errorfactory:ValidationException" ,
317330 exact_attrs = {
@@ -325,80 +338,57 @@ def test_bedrock_chat_completion_error_invalid_model_no_content(
325338 },
326339 )
327340 @validate_transaction_metrics (
328- name = "test_bedrock_chat_completion_error_invalid_model_no_content " ,
341+ name = "test_bedrock_chat_completion_error_invalid_model " ,
329342 scoped_metrics = [expected_metric ],
330343 rollup_metrics = [expected_metric ],
331344 custom_metrics = [(f"Supportability/Python/ML/Bedrock/{ BOTOCORE_VERSION } " , 1 )],
332345 background_task = True ,
333346 )
334- @background_task (name = "test_bedrock_chat_completion_error_invalid_model_no_content " )
347+ @background_task (name = "test_bedrock_chat_completion_error_invalid_model " )
335348 def _test ():
336349 set_trace_info ()
337350 add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
338351 add_custom_attribute ("llm.foo" , "bar" )
339352 add_custom_attribute ("non_llm_attr" , "python-agent" )
340353
341- with pytest .raises (_client_error ):
342- message = [{"role" : "user" , "content" : [{"text" : "Model does not exist." }]}]
343-
344- request = (
345- bedrock_converse_server .converse_stream if response_streaming else bedrock_converse_server .converse
346- )
347- request (modelId = "does-not-exist" , messages = message , inferenceConfig = {"temperature" : 0.7 , "maxTokens" : 100 })
354+ with WithLlmCustomAttributes ({"context" : "attr" }):
355+ exercise_converse_invalid_model ()
348356
349357 _test ()
350358
351359
352360@reset_core_stats_engine ()
353- @override_llm_token_callback_settings ( llm_token_count_callback )
354- def test_bedrock_chat_completion_error_incorrect_access_key_with_token_count (
355- monkeypatch , bedrock_converse_server , response_streaming , set_trace_info , expected_metric
361+ @disabled_ai_monitoring_record_content_settings
362+ def test_bedrock_chat_completion_error_invalid_model_no_content (
363+ exercise_converse_invalid_model , set_trace_info , expected_metric
356364):
357- """
358- A request is made to the server with invalid credentials. botocore will reach out to the server and receive an
359- UnrecognizedClientException as a response. Information from the request will be parsed and reported in customer
360- events. The error response can also be parsed, and will be included as attributes on the recorded exception.
361- """
362-
363- @validate_custom_events (add_token_count_to_events (chat_completion_invalid_access_key_error_events ))
365+ @validate_custom_events (events_sans_content (chat_completion_invalid_model_error_events ))
364366 @validate_error_trace_attributes (
365- _client_error_name ,
367+ "botocore.errorfactory:ValidationException" ,
366368 exact_attrs = {
367369 "agent" : {},
368370 "intrinsic" : {},
369371 "user" : {
370- "http.statusCode" : 403 ,
371- "error.message" : "The security token included in the request is invalid." ,
372- "error.code" : "UnrecognizedClientException " ,
372+ "http.statusCode" : 400 ,
373+ "error.message" : "The provided model identifier is invalid." ,
374+ "error.code" : "ValidationException " ,
373375 },
374376 },
375377 )
376378 @validate_transaction_metrics (
377- name = "test_bedrock_chat_completion_incorrect_access_key_with_token_count " ,
379+ name = "test_bedrock_chat_completion_error_invalid_model_no_content " ,
378380 scoped_metrics = [expected_metric ],
379381 rollup_metrics = [expected_metric ],
380382 custom_metrics = [(f"Supportability/Python/ML/Bedrock/{ BOTOCORE_VERSION } " , 1 )],
381383 background_task = True ,
382384 )
383- @background_task (name = "test_bedrock_chat_completion_incorrect_access_key_with_token_count " )
385+ @background_task (name = "test_bedrock_chat_completion_error_invalid_model_no_content " )
384386 def _test ():
385- monkeypatch .setattr (bedrock_converse_server ._request_signer ._credentials , "access_key" , "INVALID-ACCESS-KEY" )
386-
387- with pytest .raises (_client_error ):
388- set_trace_info ()
389- add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
390- add_custom_attribute ("llm.foo" , "bar" )
391- add_custom_attribute ("non_llm_attr" , "python-agent" )
392-
393- message = [{"role" : "user" , "content" : [{"text" : "Invalid Token" }]}]
387+ set_trace_info ()
388+ add_custom_attribute ("llm.conversation_id" , "my-awesome-id" )
389+ add_custom_attribute ("llm.foo" , "bar" )
390+ add_custom_attribute ("non_llm_attr" , "python-agent" )
394391
395- request = (
396- bedrock_converse_server .converse_stream if response_streaming else bedrock_converse_server .converse
397- )
398- request (
399- modelId = "anthropic.claude-3-sonnet-20240229-v1:0" ,
400- messages = message ,
401- inferenceConfig = {"temperature" : 0.7 , "maxTokens" : 100 },
402- )
392+ exercise_converse_invalid_model ()
403393
404394 _test ()
0 commit comments