diff --git a/sentry_sdk/integrations/huggingface_hub.py b/sentry_sdk/integrations/huggingface_hub.py index 34ad01df5a..2348f2b155 100644 --- a/sentry_sdk/integrations/huggingface_hub.py +++ b/sentry_sdk/integrations/huggingface_hub.py @@ -36,11 +36,6 @@ class HuggingfaceHubIntegration(Integration): identifier = "huggingface_hub" origin = f"auto.ai.{identifier}" - def __init__( - self: "HuggingfaceHubIntegration", include_prompts: bool = True - ) -> None: - self.include_prompts = include_prompts - @staticmethod def setup_once() -> None: version = parse_version(HUGGINGFACE_HUB_VERSION) @@ -131,7 +126,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": set_data_normalized( span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompt, unpack=False ) - elif should_send_default_pii() and integration.include_prompts: + elif should_send_default_pii(): set_data_normalized( span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompt, unpack=False ) @@ -219,7 +214,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": tool_calls, unpack=False, ) - elif should_send_default_pii() and integration.include_prompts: + elif should_send_default_pii(): set_data_normalized( span, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -237,7 +232,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": SPANDATA.GEN_AI_RESPONSE_TEXT, text_response, ) - elif should_send_default_pii() and integration.include_prompts: + elif should_send_default_pii(): set_data_normalized( span, SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -304,7 +299,7 @@ def new_details_iterator() -> "Iterable[Any]": if has_data_collection_enabled(client.options): if client.options["data_collection"]["gen_ai"]["outputs"]: should_set_response_text = True - elif should_send_default_pii() and integration.include_prompts: + elif should_send_default_pii(): should_set_response_text = True if should_set_response_text: @@ -397,10 +392,7 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]": tool_calls, unpack=False, ) - elif ( - should_send_default_pii() - and integration.include_prompts - ): + elif should_send_default_pii(): set_data_normalized( span, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -420,10 +412,7 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]": SPANDATA.GEN_AI_RESPONSE_TEXT, text_response, ) - elif ( - should_send_default_pii() - and integration.include_prompts - ): + elif should_send_default_pii(): set_data_normalized( span, SPANDATA.GEN_AI_RESPONSE_TEXT, diff --git a/tests/integrations/huggingface_hub/test_huggingface_hub.py b/tests/integrations/huggingface_hub/test_huggingface_hub.py index 0b0eaf328b..de72a586ff 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -469,18 +469,16 @@ def mock_hf_chat_completion_api_streaming_tools(httpx_mock): @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False]) -@pytest.mark.parametrize("include_prompts", [True, False]) def test_text_generation( sentry_init: "Any", capture_items: "Any", send_default_pii: "Any", - include_prompts: "Any", mock_hf_text_generation_api: "Any", ) -> None: sentry_init( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) client = InferenceClient(model="test-model") @@ -522,11 +520,11 @@ def test_text_generation( "sentry.origin": "auto.ai.huggingface_hub", } - if send_default_pii and include_prompts: + if send_default_pii: expected_data["gen_ai.request.messages"] = "Hello" expected_data["gen_ai.response.text"] = "[mocked] Hello! How can i help you?" - if not send_default_pii or not include_prompts: + if not send_default_pii: assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data @@ -538,18 +536,16 @@ def test_text_generation( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False]) -@pytest.mark.parametrize("include_prompts", [True, False]) def test_text_generation_streaming( sentry_init: "Any", capture_items: "Any", send_default_pii: "Any", - include_prompts: "Any", mock_hf_text_generation_api_streaming: "Any", ) -> None: sentry_init( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) client = InferenceClient(model="test-model") @@ -593,11 +589,11 @@ def test_text_generation_streaming( "sentry.origin": "auto.ai.huggingface_hub", } - if send_default_pii and include_prompts: + if send_default_pii: expected_data["gen_ai.request.messages"] = "Hello" expected_data["gen_ai.response.text"] = "the mocked model response" - if not send_default_pii or not include_prompts: + if not send_default_pii: assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data @@ -609,18 +605,16 @@ def test_text_generation_streaming( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False]) -@pytest.mark.parametrize("include_prompts", [True, False]) def test_chat_completion( sentry_init: "Any", capture_items: "Any", send_default_pii: "Any", - include_prompts: "Any", mock_hf_chat_completion_api: "Any", ) -> None: sentry_init( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) client = get_hf_provider_inference_client() @@ -671,7 +665,7 @@ def test_chat_completion( "sentry.origin": "auto.ai.huggingface_hub", } - if send_default_pii and include_prompts: + if send_default_pii: expected_data["gen_ai.request.messages"] = safe_serialize( [ { @@ -685,7 +679,7 @@ def test_chat_completion( "[mocked] Hello! How can I help you today?" ) - if not send_default_pii or not include_prompts: + if not send_default_pii: assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data @@ -694,18 +688,16 @@ def test_chat_completion( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False]) -@pytest.mark.parametrize("include_prompts", [True, False]) def test_chat_completion_streaming( sentry_init: "Any", capture_items: "Any", send_default_pii: "Any", - include_prompts: "Any", mock_hf_chat_completion_api_streaming: "Any", ) -> None: sentry_init( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) client = get_hf_provider_inference_client() @@ -760,7 +752,7 @@ def test_chat_completion_streaming( expected_data["gen_ai.usage.output_tokens"] = 14 expected_data["gen_ai.usage.total_tokens"] = 197 - if send_default_pii and include_prompts: + if send_default_pii: expected_data["gen_ai.request.messages"] = safe_serialize( [ { @@ -772,7 +764,7 @@ def test_chat_completion_streaming( ) expected_data["gen_ai.response.text"] = "the mocked model response" - if not send_default_pii or not include_prompts: + if not send_default_pii: assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data @@ -875,18 +867,16 @@ def test_span_status_error( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False]) -@pytest.mark.parametrize("include_prompts", [True, False]) def test_chat_completion_with_tools( sentry_init: "Any", capture_items: "Any", send_default_pii: "Any", - include_prompts: "Any", mock_hf_chat_completion_api_tools: "Any", ): sentry_init( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) client = get_hf_provider_inference_client() @@ -947,7 +937,7 @@ def test_chat_completion_with_tools( "sentry.origin": "auto.ai.huggingface_hub", } - if send_default_pii and include_prompts: + if send_default_pii: expected_data["gen_ai.request.messages"] = ( '[{"role": "user", "content": "What is the weather in Paris?"}]' ) @@ -955,7 +945,7 @@ def test_chat_completion_with_tools( '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather", "description": "None"}, "id": "call_123", "type": "function"}]' ) - if not send_default_pii or not include_prompts: + if not send_default_pii: assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data assert "gen_ai.response.tool_calls" not in expected_data @@ -965,18 +955,16 @@ def test_chat_completion_with_tools( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False]) -@pytest.mark.parametrize("include_prompts", [True, False]) def test_chat_completion_streaming_with_tools( sentry_init: "Any", capture_items: "Any", send_default_pii: "Any", - include_prompts: "Any", mock_hf_chat_completion_api_streaming_tools: "Any", ) -> None: sentry_init( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) client = get_hf_provider_inference_client() @@ -1043,7 +1031,7 @@ def test_chat_completion_streaming_with_tools( expected_data["gen_ai.usage.output_tokens"] = 14 expected_data["gen_ai.usage.total_tokens"] = 197 - if send_default_pii and include_prompts: + if send_default_pii: expected_data["gen_ai.request.messages"] = ( '[{"role": "user", "content": "What is the weather in Paris?"}]' ) @@ -1052,7 +1040,7 @@ def test_chat_completion_streaming_with_tools( '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather"}, "id": "call_123", "type": "function", "index": "None"}]' ) - if not send_default_pii or not include_prompts: + if not send_default_pii: assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data assert "gen_ai.response.tool_calls" not in expected_data @@ -1078,12 +1066,11 @@ def test_chat_completion_streaming_with_tools( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize( - "data_collection,send_default_pii,include_prompts,expected_present,expected_absent", + "data_collection,send_default_pii,expected_present,expected_absent", [ pytest.param( {"gen_ai": {"inputs": True, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -1094,7 +1081,6 @@ def test_chat_completion_streaming_with_tools( pytest.param( {"gen_ai": {"inputs": False, "outputs": False}}, True, - True, [], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, @@ -1105,7 +1091,6 @@ def test_chat_completion_streaming_with_tools( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, ], @@ -1117,7 +1102,6 @@ def test_chat_completion_streaming_with_tools( pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_RESPONSE_TEXT, ], @@ -1129,7 +1113,6 @@ def test_chat_completion_streaming_with_tools( pytest.param( {"gen_ai": {}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -1140,18 +1123,16 @@ def test_chat_completion_streaming_with_tools( pytest.param( None, True, - True, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TEXT, ], [], - id="no-gen-ai-config-legacy-pii-and-include-prompts-enabled", + id="no-gen-ai-config-legacy-pii", ), pytest.param( None, False, - True, [], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, @@ -1167,14 +1148,13 @@ def test_text_generation_data_collection( mock_hf_text_generation_api: "Any", data_collection: "Any", send_default_pii: "Any", - include_prompts: "Any", expected_present: "Any", expected_absent: "Any", ) -> None: sentry_init_kwargs = dict( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) if data_collection is not None: sentry_init_kwargs["data_collection"] = data_collection @@ -1212,12 +1192,11 @@ def test_text_generation_data_collection( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize( - "data_collection,send_default_pii,include_prompts,expected_present,expected_absent", + "data_collection,send_default_pii,expected_present,expected_absent", [ pytest.param( {"gen_ai": {"inputs": True, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -1228,7 +1207,6 @@ def test_text_generation_data_collection( pytest.param( {"gen_ai": {"inputs": False, "outputs": False}}, True, - True, [], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, @@ -1239,7 +1217,6 @@ def test_text_generation_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, ], @@ -1251,7 +1228,6 @@ def test_text_generation_data_collection( pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_RESPONSE_TEXT, ], @@ -1263,7 +1239,6 @@ def test_text_generation_data_collection( pytest.param( {"gen_ai": {}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -1274,18 +1249,16 @@ def test_text_generation_data_collection( pytest.param( None, True, - True, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TEXT, ], [], - id="no-gen-ai-config-legacy-pii-and-include-prompts-enabled", + id="no-gen-ai-config-legacy-pii", ), pytest.param( None, False, - True, [], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, @@ -1301,14 +1274,13 @@ def test_text_generation_streaming_data_collection( mock_hf_text_generation_api_streaming: "Any", data_collection: "Any", send_default_pii: "Any", - include_prompts: "Any", expected_present: "Any", expected_absent: "Any", ) -> None: sentry_init_kwargs = dict( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) if data_collection is not None: sentry_init_kwargs["data_collection"] = data_collection @@ -1348,12 +1320,11 @@ def test_text_generation_streaming_data_collection( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize( - "data_collection,send_default_pii,include_prompts,expected_present,expected_absent", + "data_collection,send_default_pii,expected_present,expected_absent", [ pytest.param( {"gen_ai": {"inputs": True, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -1365,7 +1336,6 @@ def test_text_generation_streaming_data_collection( pytest.param( {"gen_ai": {"inputs": False, "outputs": False}}, True, - True, [], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, @@ -1377,7 +1347,6 @@ def test_text_generation_streaming_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, @@ -1390,7 +1359,6 @@ def test_text_generation_streaming_data_collection( pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, ], @@ -1403,7 +1371,6 @@ def test_text_generation_streaming_data_collection( pytest.param( {"gen_ai": {}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -1415,19 +1382,17 @@ def test_text_generation_streaming_data_collection( pytest.param( None, True, - True, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], [], - id="no-gen-ai-config-legacy-pii-and-include-prompts-enabled", + id="no-gen-ai-config-legacy-pii", ), pytest.param( None, False, - True, [ SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], @@ -1445,14 +1410,13 @@ def test_chat_completion_data_collection_tools( mock_hf_chat_completion_api_tools: "Any", data_collection: "Any", send_default_pii: "Any", - include_prompts: "Any", expected_present: "Any", expected_absent: "Any", ) -> None: sentry_init_kwargs = dict( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) if data_collection is not None: sentry_init_kwargs["data_collection"] = data_collection @@ -1501,12 +1465,11 @@ def test_chat_completion_data_collection_tools( @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize( - "data_collection,send_default_pii,include_prompts,expected_present,expected_absent", + "data_collection,send_default_pii,expected_present,expected_absent", [ pytest.param( {"gen_ai": {"inputs": True, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -1519,7 +1482,6 @@ def test_chat_completion_data_collection_tools( pytest.param( {"gen_ai": {"inputs": False, "outputs": False}}, True, - True, [], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, @@ -1532,7 +1494,6 @@ def test_chat_completion_data_collection_tools( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, @@ -1546,7 +1507,6 @@ def test_chat_completion_data_collection_tools( pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, False, - False, [ SPANDATA.GEN_AI_RESPONSE_TEXT, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -1560,7 +1520,6 @@ def test_chat_completion_data_collection_tools( pytest.param( {"gen_ai": {}}, False, - False, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -1573,7 +1532,6 @@ def test_chat_completion_data_collection_tools( pytest.param( None, True, - True, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, @@ -1581,12 +1539,11 @@ def test_chat_completion_data_collection_tools( SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], [], - id="no-gen-ai-config-legacy-pii-and-include-prompts-enabled", + id="no-gen-ai-config-legacy-pii", ), pytest.param( None, False, - True, [ SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], @@ -1605,14 +1562,13 @@ def test_chat_completion_streaming_data_collection_tools( mock_hf_chat_completion_api_streaming_tools: "Any", data_collection: "Any", send_default_pii: "Any", - include_prompts: "Any", expected_present: "Any", expected_absent: "Any", ) -> None: sentry_init_kwargs = dict( traces_sample_rate=1.0, send_default_pii=send_default_pii, - integrations=[HuggingfaceHubIntegration(include_prompts=include_prompts)], + integrations=[HuggingfaceHubIntegration()], ) if data_collection is not None: sentry_init_kwargs["data_collection"] = data_collection