@@ -651,34 +651,23 @@ def test_text_generation_streaming(
651651 assert span ["name" ] == "text_completion test-model"
652652 assert span ["attributes" ]["sentry.origin" ] == "auto.ai.huggingface_hub"
653653
654- if stream_gen_ai_spans :
655- expected_data = {
656- "gen_ai.operation.name" : "text_completion" ,
657- "gen_ai.request.model" : "test-model" ,
658- "gen_ai.response.finish_reasons" : "length" ,
659- "gen_ai.response.streaming" : True ,
660- "gen_ai.usage.total_tokens" : 10 ,
661- "sentry.environment" : "production" ,
662- "sentry.op" : "gen_ai.text_completion" ,
663- "sentry.origin" : "auto.ai.huggingface_hub" ,
664- "sentry.release" : mock .ANY ,
665- "sentry.sdk.name" : "sentry.python" ,
666- "sentry.sdk.version" : mock .ANY ,
667- "sentry.segment.id" : mock .ANY ,
668- "sentry.segment.name" : "test" ,
669- "thread.id" : mock .ANY ,
670- "thread.name" : mock .ANY ,
671- }
672- else :
673- expected_data = {
674- "gen_ai.operation.name" : "text_completion" ,
675- "gen_ai.request.model" : "test-model" ,
676- "gen_ai.response.finish_reasons" : "length" ,
677- "gen_ai.response.streaming" : True ,
678- "gen_ai.usage.total_tokens" : 10 ,
679- "thread.id" : mock .ANY ,
680- "thread.name" : mock .ANY ,
681- }
654+ expected_data = {
655+ "gen_ai.operation.name" : "text_completion" ,
656+ "gen_ai.request.model" : "test-model" ,
657+ "gen_ai.response.finish_reasons" : "length" ,
658+ "gen_ai.response.streaming" : True ,
659+ "gen_ai.usage.total_tokens" : 10 ,
660+ "sentry.environment" : "production" ,
661+ "sentry.op" : "gen_ai.text_completion" ,
662+ "sentry.origin" : "auto.ai.huggingface_hub" ,
663+ "sentry.release" : mock .ANY ,
664+ "sentry.sdk.name" : "sentry.python" ,
665+ "sentry.sdk.version" : mock .ANY ,
666+ "sentry.segment.id" : mock .ANY ,
667+ "sentry.segment.name" : "test" ,
668+ "thread.id" : mock .ANY ,
669+ "thread.name" : mock .ANY ,
670+ }
682671
683672 if send_default_pii and include_prompts :
684673 expected_data ["gen_ai.request.messages" ] = "Hello"
@@ -721,34 +710,15 @@ def test_text_generation_streaming(
721710 assert span ["description" ] == "text_completion test-model"
722711 assert span ["origin" ] == "auto.ai.huggingface_hub"
723712
724- if stream_gen_ai_spans :
725- expected_data = {
726- "gen_ai.operation.name" : "text_completion" ,
727- "gen_ai.request.model" : "test-model" ,
728- "gen_ai.response.finish_reasons" : "length" ,
729- "gen_ai.response.streaming" : True ,
730- "gen_ai.usage.total_tokens" : 10 ,
731- "sentry.environment" : "production" ,
732- "sentry.op" : "gen_ai.text_completion" ,
733- "sentry.origin" : "auto.ai.huggingface_hub" ,
734- "sentry.release" : mock .ANY ,
735- "sentry.sdk.name" : "sentry.python" ,
736- "sentry.sdk.version" : mock .ANY ,
737- "sentry.segment.id" : mock .ANY ,
738- "sentry.segment.name" : "test" ,
739- "thread.id" : mock .ANY ,
740- "thread.name" : mock .ANY ,
741- }
742- else :
743- expected_data = {
744- "gen_ai.operation.name" : "text_completion" ,
745- "gen_ai.request.model" : "test-model" ,
746- "gen_ai.response.finish_reasons" : "length" ,
747- "gen_ai.response.streaming" : True ,
748- "gen_ai.usage.total_tokens" : 10 ,
749- "thread.id" : mock .ANY ,
750- "thread.name" : mock .ANY ,
751- }
713+ expected_data = {
714+ "gen_ai.operation.name" : "text_completion" ,
715+ "gen_ai.request.model" : "test-model" ,
716+ "gen_ai.response.finish_reasons" : "length" ,
717+ "gen_ai.response.streaming" : True ,
718+ "gen_ai.usage.total_tokens" : 10 ,
719+ "thread.id" : mock .ANY ,
720+ "thread.name" : mock .ANY ,
721+ }
752722
753723 if send_default_pii and include_prompts :
754724 expected_data ["gen_ai.request.messages" ] = "Hello"
@@ -783,20 +753,18 @@ def test_chat_completion(
783753 integrations = [HuggingfaceHubIntegration (include_prompts = include_prompts )],
784754 _experiments = {"stream_gen_ai_spans" : stream_gen_ai_spans },
785755 )
786- if stream_gen_ai_spans :
787- items = capture_items ("transaction" , "span" )
788- else :
789- events = capture_events ()
790756
791757 client = get_hf_provider_inference_client ()
792758
793- with sentry_sdk .start_transaction (name = "test" ):
794- client .chat_completion (
795- messages = [{"role" : "user" , "content" : "Hello!" }],
796- stream = False ,
797- )
798-
799759 if stream_gen_ai_spans :
760+ items = capture_items ("transaction" , "span" )
761+
762+ with sentry_sdk .start_transaction (name = "test" ):
763+ client .chat_completion (
764+ messages = [{"role" : "user" , "content" : "Hello!" }],
765+ stream = False ,
766+ )
767+
800768 spans = [item .payload for item in items if item .type == "span" ]
801769 span = None
802770 for sp in spans :
@@ -849,6 +817,14 @@ def test_chat_completion(
849817
850818 assert span ["attributes" ] == expected_data
851819 else :
820+ events = capture_events ()
821+
822+ with sentry_sdk .start_transaction (name = "test" ):
823+ client .chat_completion (
824+ messages = [{"role" : "user" , "content" : "Hello!" }],
825+ stream = False ,
826+ )
827+
852828 (transaction ,) = events
853829
854830 span = None
@@ -863,14 +839,9 @@ def test_chat_completion(
863839
864840 assert span is not None
865841
866- if stream_gen_ai_spans :
867- assert span ["attributes" ]["sentry.op" ] == "gen_ai.chat"
868- assert span ["name" ] == "chat test-model"
869- assert span ["attributes" ]["sentry.origin" ] == "auto.ai.huggingface_hub"
870- else :
871- assert span ["op" ] == "gen_ai.chat"
872- assert span ["description" ] == "chat test-model"
873- assert span ["origin" ] == "auto.ai.huggingface_hub"
842+ assert span ["op" ] == "gen_ai.chat"
843+ assert span ["description" ] == "chat test-model"
844+ assert span ["origin" ] == "auto.ai.huggingface_hub"
874845
875846 expected_data = {
876847 "gen_ai.operation.name" : "chat" ,
@@ -1397,10 +1368,6 @@ def test_chat_completion_streaming_with_tools(
13971368 integrations = [HuggingfaceHubIntegration (include_prompts = include_prompts )],
13981369 _experiments = {"stream_gen_ai_spans" : stream_gen_ai_spans },
13991370 )
1400- if stream_gen_ai_spans :
1401- items = capture_items ("transaction" , "span" )
1402- else :
1403- events = capture_events ()
14041371
14051372 client = get_hf_provider_inference_client ()
14061373
@@ -1419,17 +1386,21 @@ def test_chat_completion_streaming_with_tools(
14191386 }
14201387 ]
14211388
1422- with sentry_sdk .start_transaction (name = "test" ):
1423- _ = list (
1424- client .chat_completion (
1425- messages = [{"role" : "user" , "content" : "What is the weather in Paris?" }],
1426- stream = True ,
1427- tools = tools ,
1428- tool_choice = "auto" ,
1389+ if stream_gen_ai_spans :
1390+ items = capture_items ("transaction" , "span" )
1391+
1392+ with sentry_sdk .start_transaction (name = "test" ):
1393+ _ = list (
1394+ client .chat_completion (
1395+ messages = [
1396+ {"role" : "user" , "content" : "What is the weather in Paris?" }
1397+ ],
1398+ stream = True ,
1399+ tools = tools ,
1400+ tool_choice = "auto" ,
1401+ )
14291402 )
1430- )
14311403
1432- if stream_gen_ai_spans :
14331404 spans = [item .payload for item in items if item .type == "span" ]
14341405 span = None
14351406 for sp in spans :
@@ -1487,6 +1458,20 @@ def test_chat_completion_streaming_with_tools(
14871458
14881459 assert span ["attributes" ] == expected_data
14891460 else :
1461+ events = capture_events ()
1462+
1463+ with sentry_sdk .start_transaction (name = "test" ):
1464+ _ = list (
1465+ client .chat_completion (
1466+ messages = [
1467+ {"role" : "user" , "content" : "What is the weather in Paris?" }
1468+ ],
1469+ stream = True ,
1470+ tools = tools ,
1471+ tool_choice = "auto" ,
1472+ )
1473+ )
1474+
14901475 (transaction ,) = events
14911476
14921477 span = None
0 commit comments