@@ -834,6 +834,96 @@ def test_converse_input_attributes(
834834 )
835835
836836
837+ @pytest .mark .parametrize ("data_collection" , [True , False ])
838+ @pytest .mark .parametrize ("span_streaming" , [True , False ])
839+ @pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
840+ def test_converse_output_attributes (
841+ sentry_init ,
842+ capture_items ,
843+ capture_events ,
844+ data_collection ,
845+ span_streaming ,
846+ stream_gen_ai_spans ,
847+ converse_response ,
848+ content_block_messages ,
849+ ):
850+ if data_collection :
851+ sentry_init (
852+ traces_sample_rate = 1.0 ,
853+ integrations = [Boto3Integration ()],
854+ data_collection = {"gen_ai" : {"outputs" : True }},
855+ stream_gen_ai_spans = stream_gen_ai_spans ,
856+ trace_lifecycle = "stream" if span_streaming else "static" ,
857+ )
858+ else :
859+ sentry_init (
860+ traces_sample_rate = 1.0 ,
861+ integrations = [Boto3Integration ()],
862+ send_default_pii = True ,
863+ stream_gen_ai_spans = stream_gen_ai_spans ,
864+ trace_lifecycle = "stream" if span_streaming else "static" ,
865+ )
866+
867+ messages , expected_messages = content_block_messages
868+ converse_response ["output" ]["message" ] = {
869+ "role" : "assistant" ,
870+ "content" : [block for message in messages for block in message ["content" ]],
871+ }
872+ expected_output_messages = [
873+ {
874+ "role" : "assistant" ,
875+ "parts" : [
876+ part for message in expected_messages for part in message ["parts" ]
877+ ],
878+ }
879+ ]
880+
881+ session = boto3 .Session (
882+ aws_access_key_id = "-" , aws_secret_access_key = "-" , region_name = "us-east-1"
883+ )
884+ client = session .client ("bedrock-runtime" )
885+ stubber = Stubber (client )
886+ stubber .add_response ("converse" , converse_response )
887+
888+ if span_streaming or stream_gen_ai_spans :
889+ items = capture_items ("span" )
890+
891+ with stubber , sentry_sdk .start_transaction (name = "parent" ):
892+ client .converse (
893+ modelId = "anthropic.claude-3-sonnet-20240229-v1:0" ,
894+ messages = [{"role" : "user" , "content" : [{"text" : "Hi" }]}],
895+ )
896+
897+ sentry_sdk .flush ()
898+ spans = [item .payload for item in items ]
899+ (span ,) = (
900+ span
901+ for span in spans
902+ if span ["attributes" ].get ("sentry.op" ) == OP .GEN_AI_CHAT
903+ )
904+
905+ assert (
906+ json .loads (span ["attributes" ][SPANDATA .GEN_AI_OUTPUT_MESSAGES ])
907+ == expected_output_messages
908+ )
909+ else :
910+ events = capture_events ()
911+
912+ with stubber , sentry_sdk .start_transaction (name = "parent" ):
913+ client .converse (
914+ modelId = "anthropic.claude-3-sonnet-20240229-v1:0" ,
915+ messages = [{"role" : "user" , "content" : [{"text" : "Hi" }]}],
916+ )
917+
918+ (transaction ,) = events
919+ (span ,) = transaction ["spans" ]
920+
921+ assert (
922+ json .loads (span ["data" ][SPANDATA .GEN_AI_OUTPUT_MESSAGES ])
923+ == expected_output_messages
924+ )
925+
926+
837927@pytest .mark .parametrize ("span_streaming" , [True , False ])
838928@pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
839929def test_converse_stream (
0 commit comments