11import re
22from typing import TYPE_CHECKING
3- from unittest import mock
43
54import pytest
65import responses
109from sentry_sdk .consts import SPANDATA
1110from sentry_sdk .integrations .huggingface_hub import HuggingfaceHubIntegration
1211from sentry_sdk .utils import package_version , safe_serialize
12+ from tests .conftest import ApproxDict
1313
1414try :
1515 from huggingface_hub .utils ._errors import HfHubHTTPError
@@ -522,19 +522,8 @@ def test_text_generation(
522522 "gen_ai.response.finish_reasons" : "length" ,
523523 "gen_ai.response.streaming" : False ,
524524 "gen_ai.usage.total_tokens" : 10 ,
525- "process.runtime.name" : mock .ANY ,
526- "process.runtime.version" : mock .ANY ,
527- "sentry.environment" : "production" ,
528525 "sentry.op" : "gen_ai.text_completion" ,
529526 "sentry.origin" : "auto.ai.huggingface_hub" ,
530- "sentry.release" : mock .ANY ,
531- "sentry.sdk.name" : "sentry.python" ,
532- "sentry.sdk.version" : mock .ANY ,
533- "sentry.segment.id" : mock .ANY ,
534- "sentry.segment.name" : "test" ,
535- "server.address" : mock .ANY ,
536- "thread.id" : mock .ANY ,
537- "thread.name" : mock .ANY ,
538527 }
539528
540529 if send_default_pii and include_prompts :
@@ -547,7 +536,7 @@ def test_text_generation(
547536 assert "gen_ai.request.messages" not in expected_data
548537 assert "gen_ai.response.text" not in expected_data
549538
550- assert span ["attributes" ] == expected_data
539+ assert span ["attributes" ] == ApproxDict ( expected_data )
551540
552541 # text generation does not set the response model
553542 assert "gen_ai.response.model" not in span ["attributes" ]
@@ -585,8 +574,6 @@ def test_text_generation(
585574 "gen_ai.response.finish_reasons" : "length" ,
586575 "gen_ai.response.streaming" : False ,
587576 "gen_ai.usage.total_tokens" : 10 ,
588- "thread.id" : mock .ANY ,
589- "thread.name" : mock .ANY ,
590577 }
591578
592579 if send_default_pii and include_prompts :
@@ -599,7 +586,7 @@ def test_text_generation(
599586 assert "gen_ai.request.messages" not in expected_data
600587 assert "gen_ai.response.text" not in expected_data
601588
602- assert span ["data" ] == expected_data
589+ assert span ["data" ] == ApproxDict ( expected_data )
603590
604591 # text generation does not set the response model
605592 assert "gen_ai.response.model" not in span ["data" ]
@@ -661,19 +648,9 @@ def test_text_generation_streaming(
661648 "gen_ai.response.finish_reasons" : "length" ,
662649 "gen_ai.response.streaming" : True ,
663650 "gen_ai.usage.total_tokens" : 10 ,
664- "process.runtime.name" : mock .ANY ,
665- "process.runtime.version" : mock .ANY ,
666651 "sentry.environment" : "production" ,
667652 "sentry.op" : "gen_ai.text_completion" ,
668653 "sentry.origin" : "auto.ai.huggingface_hub" ,
669- "sentry.release" : mock .ANY ,
670- "sentry.sdk.name" : "sentry.python" ,
671- "sentry.sdk.version" : mock .ANY ,
672- "sentry.segment.id" : mock .ANY ,
673- "sentry.segment.name" : "test" ,
674- "server.address" : mock .ANY ,
675- "thread.id" : mock .ANY ,
676- "thread.name" : mock .ANY ,
677654 }
678655
679656 if send_default_pii and include_prompts :
@@ -684,7 +661,7 @@ def test_text_generation_streaming(
684661 assert "gen_ai.request.messages" not in expected_data
685662 assert "gen_ai.response.text" not in expected_data
686663
687- assert span ["attributes" ] == expected_data
664+ assert span ["attributes" ] == ApproxDict ( expected_data )
688665
689666 # text generation does not set the response model
690667 assert "gen_ai.response.model" not in span ["attributes" ]
@@ -723,8 +700,6 @@ def test_text_generation_streaming(
723700 "gen_ai.response.finish_reasons" : "length" ,
724701 "gen_ai.response.streaming" : True ,
725702 "gen_ai.usage.total_tokens" : 10 ,
726- "thread.id" : mock .ANY ,
727- "thread.name" : mock .ANY ,
728703 }
729704
730705 if send_default_pii and include_prompts :
@@ -735,7 +710,7 @@ def test_text_generation_streaming(
735710 assert "gen_ai.request.messages" not in expected_data
736711 assert "gen_ai.response.text" not in expected_data
737712
738- assert span ["data" ] == expected_data
713+ assert span ["data" ] == ApproxDict ( expected_data )
739714
740715 # text generation does not set the response model
741716 assert "gen_ai.response.model" not in span ["data" ]
@@ -804,19 +779,9 @@ def test_chat_completion(
804779 "gen_ai.usage.input_tokens" : 10 ,
805780 "gen_ai.usage.output_tokens" : 8 ,
806781 "gen_ai.usage.total_tokens" : 18 ,
807- "process.runtime.name" : mock .ANY ,
808- "process.runtime.version" : mock .ANY ,
809782 "sentry.environment" : "production" ,
810783 "sentry.op" : "gen_ai.chat" ,
811784 "sentry.origin" : "auto.ai.huggingface_hub" ,
812- "sentry.release" : mock .ANY ,
813- "sentry.sdk.name" : "sentry.python" ,
814- "sentry.sdk.version" : mock .ANY ,
815- "sentry.segment.id" : mock .ANY ,
816- "sentry.segment.name" : "test" ,
817- "server.address" : mock .ANY ,
818- "thread.id" : mock .ANY ,
819- "thread.name" : mock .ANY ,
820785 }
821786
822787 if send_default_pii and include_prompts :
@@ -837,7 +802,7 @@ def test_chat_completion(
837802 assert "gen_ai.request.messages" not in expected_data
838803 assert "gen_ai.response.text" not in expected_data
839804
840- assert span ["attributes" ] == expected_data
805+ assert span ["attributes" ] == ApproxDict ( expected_data )
841806 else :
842807 events = capture_events ()
843808
@@ -874,8 +839,6 @@ def test_chat_completion(
874839 "gen_ai.usage.input_tokens" : 10 ,
875840 "gen_ai.usage.output_tokens" : 8 ,
876841 "gen_ai.usage.total_tokens" : 18 ,
877- "thread.id" : mock .ANY ,
878- "thread.name" : mock .ANY ,
879842 }
880843
881844 if send_default_pii and include_prompts :
@@ -890,7 +853,7 @@ def test_chat_completion(
890853 assert "gen_ai.request.messages" not in expected_data
891854 assert "gen_ai.response.text" not in expected_data
892855
893- assert span ["data" ] == expected_data
856+ assert span ["data" ] == ApproxDict ( expected_data )
894857
895858
896859@pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
@@ -955,19 +918,9 @@ def test_chat_completion_streaming(
955918 "gen_ai.response.finish_reasons" : "stop" ,
956919 "gen_ai.response.model" : "test-model-123" ,
957920 "gen_ai.response.streaming" : True ,
958- "process.runtime.name" : mock .ANY ,
959- "process.runtime.version" : mock .ANY ,
960921 "sentry.environment" : "production" ,
961922 "sentry.op" : "gen_ai.chat" ,
962923 "sentry.origin" : "auto.ai.huggingface_hub" ,
963- "sentry.release" : mock .ANY ,
964- "sentry.sdk.name" : "sentry.python" ,
965- "sentry.sdk.version" : mock .ANY ,
966- "sentry.segment.id" : mock .ANY ,
967- "sentry.segment.name" : "test" ,
968- "server.address" : mock .ANY ,
969- "thread.id" : mock .ANY ,
970- "thread.name" : mock .ANY ,
971924 }
972925 # usage is not available in older versions of the library
973926 if HF_VERSION and HF_VERSION >= (0 , 26 , 0 ):
@@ -991,7 +944,7 @@ def test_chat_completion_streaming(
991944 assert "gen_ai.request.messages" not in expected_data
992945 assert "gen_ai.response.text" not in expected_data
993946
994- assert span ["attributes" ] == expected_data
947+ assert span ["attributes" ] == ApproxDict ( expected_data )
995948 else :
996949 events = capture_events ()
997950
@@ -1027,8 +980,6 @@ def test_chat_completion_streaming(
1027980 "gen_ai.response.finish_reasons" : "stop" ,
1028981 "gen_ai.response.model" : "test-model-123" ,
1029982 "gen_ai.response.streaming" : True ,
1030- "thread.id" : mock .ANY ,
1031- "thread.name" : mock .ANY ,
1032983 }
1033984 # usage is not available in older versions of the library
1034985 if HF_VERSION and HF_VERSION >= (0 , 26 , 0 ):
@@ -1046,7 +997,7 @@ def test_chat_completion_streaming(
1046997 assert "gen_ai.request.messages" not in expected_data
1047998 assert "gen_ai.response.text" not in expected_data
1048999
1049- assert span ["data" ] == expected_data
1000+ assert span ["data" ] == ApproxDict ( expected_data )
10501001
10511002
10521003@pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
@@ -1104,21 +1055,11 @@ def test_chat_completion_api_error(
11041055 expected_data = {
11051056 "gen_ai.operation.name" : "chat" ,
11061057 "gen_ai.request.model" : "test-model" ,
1107- "process.runtime.name" : mock .ANY ,
1108- "process.runtime.version" : mock .ANY ,
11091058 "sentry.environment" : "production" ,
11101059 "sentry.op" : "gen_ai.chat" ,
11111060 "sentry.origin" : "auto.ai.huggingface_hub" ,
1112- "sentry.release" : mock .ANY ,
1113- "sentry.sdk.name" : "sentry.python" ,
1114- "sentry.sdk.version" : mock .ANY ,
1115- "sentry.segment.id" : mock .ANY ,
1116- "sentry.segment.name" : "test" ,
1117- "server.address" : mock .ANY ,
1118- "thread.id" : mock .ANY ,
1119- "thread.name" : mock .ANY ,
11201061 }
1121- assert span ["attributes" ] == expected_data
1062+ assert span ["attributes" ] == ApproxDict ( expected_data )
11221063 else :
11231064 events = capture_events ()
11241065
@@ -1160,10 +1101,8 @@ def test_chat_completion_api_error(
11601101 expected_data = {
11611102 "gen_ai.operation.name" : "chat" ,
11621103 "gen_ai.request.model" : "test-model" ,
1163- "thread.id" : mock .ANY ,
1164- "thread.name" : mock .ANY ,
11651104 }
1166- assert span ["data" ] == expected_data
1105+ assert span ["data" ] == ApproxDict ( expected_data )
11671106
11681107
11691108@pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
@@ -1305,19 +1244,9 @@ def test_chat_completion_with_tools(
13051244 "gen_ai.usage.input_tokens" : 10 ,
13061245 "gen_ai.usage.output_tokens" : 8 ,
13071246 "gen_ai.usage.total_tokens" : 18 ,
1308- "process.runtime.name" : mock .ANY ,
1309- "process.runtime.version" : mock .ANY ,
13101247 "sentry.environment" : "production" ,
13111248 "sentry.op" : "gen_ai.chat" ,
13121249 "sentry.origin" : "auto.ai.huggingface_hub" ,
1313- "sentry.release" : mock .ANY ,
1314- "sentry.sdk.name" : "sentry.python" ,
1315- "sentry.sdk.version" : mock .ANY ,
1316- "sentry.segment.id" : mock .ANY ,
1317- "sentry.segment.name" : "test" ,
1318- "server.address" : mock .ANY ,
1319- "thread.id" : mock .ANY ,
1320- "thread.name" : mock .ANY ,
13211250 }
13221251
13231252 if send_default_pii and include_prompts :
@@ -1333,7 +1262,7 @@ def test_chat_completion_with_tools(
13331262 assert "gen_ai.response.text" not in expected_data
13341263 assert "gen_ai.response.tool_calls" not in expected_data
13351264
1336- assert span ["attributes" ] == expected_data
1265+ assert span ["attributes" ] == ApproxDict ( expected_data )
13371266 else :
13381267 events = capture_events ()
13391268
@@ -1371,8 +1300,6 @@ def test_chat_completion_with_tools(
13711300 "gen_ai.usage.input_tokens" : 10 ,
13721301 "gen_ai.usage.output_tokens" : 8 ,
13731302 "gen_ai.usage.total_tokens" : 18 ,
1374- "thread.id" : mock .ANY ,
1375- "thread.name" : mock .ANY ,
13761303 }
13771304
13781305 if send_default_pii and include_prompts :
@@ -1388,7 +1315,7 @@ def test_chat_completion_with_tools(
13881315 assert "gen_ai.response.text" not in expected_data
13891316 assert "gen_ai.response.tool_calls" not in expected_data
13901317
1391- assert span ["data" ] == expected_data
1318+ assert span ["data" ] == ApproxDict ( expected_data )
13921319
13931320
13941321@pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
@@ -1467,19 +1394,9 @@ def test_chat_completion_streaming_with_tools(
14671394 "gen_ai.response.finish_reasons" : "tool_calls" ,
14681395 "gen_ai.response.model" : "test-model-123" ,
14691396 "gen_ai.response.streaming" : True ,
1470- "process.runtime.name" : mock .ANY ,
1471- "process.runtime.version" : mock .ANY ,
14721397 "sentry.environment" : "production" ,
14731398 "sentry.op" : "gen_ai.chat" ,
14741399 "sentry.origin" : "auto.ai.huggingface_hub" ,
1475- "sentry.release" : mock .ANY ,
1476- "sentry.sdk.name" : "sentry.python" ,
1477- "sentry.sdk.version" : mock .ANY ,
1478- "sentry.segment.id" : mock .ANY ,
1479- "sentry.segment.name" : "test" ,
1480- "server.address" : mock .ANY ,
1481- "thread.id" : mock .ANY ,
1482- "thread.name" : mock .ANY ,
14831400 }
14841401
14851402 if HF_VERSION and HF_VERSION >= (0 , 26 , 0 ):
@@ -1501,7 +1418,7 @@ def test_chat_completion_streaming_with_tools(
15011418 assert "gen_ai.response.text" not in expected_data
15021419 assert "gen_ai.response.tool_calls" not in expected_data
15031420
1504- assert span ["attributes" ] == expected_data
1421+ assert span ["attributes" ] == ApproxDict ( expected_data )
15051422 else :
15061423 events = capture_events ()
15071424
@@ -1542,8 +1459,6 @@ def test_chat_completion_streaming_with_tools(
15421459 "gen_ai.response.finish_reasons" : "tool_calls" ,
15431460 "gen_ai.response.model" : "test-model-123" ,
15441461 "gen_ai.response.streaming" : True ,
1545- "thread.id" : mock .ANY ,
1546- "thread.name" : mock .ANY ,
15471462 }
15481463
15491464 if HF_VERSION and HF_VERSION >= (0 , 26 , 0 ):
@@ -1565,7 +1480,7 @@ def test_chat_completion_streaming_with_tools(
15651480 assert "gen_ai.response.text" not in expected_data
15661481 assert "gen_ai.response.tool_calls" not in expected_data
15671482
1568- assert span ["data" ] == expected_data
1483+ assert span ["data" ] == ApproxDict ( expected_data )
15691484
15701485
15711486DATA_COLLECTION_TOOLS = [
0 commit comments