@@ -905,3 +905,51 @@ def test_tool_calls_extraction(sentry_init, capture_events, mock_genai_client):
905905 assert tool_calls [1 ]["type" ] == "function_call"
906906 # Arguments are serialized as JSON strings
907907 assert json .loads (tool_calls [1 ]["arguments" ]) == {"timezone" : "PST" }
908+
909+
910+ def test_google_genai_message_truncation (
911+ sentry_init , capture_events , mock_genai_client
912+ ):
913+ """Test that large messages are truncated properly in Google GenAI integration."""
914+ sentry_init (
915+ integrations = [GoogleGenAIIntegration (include_prompts = True )],
916+ traces_sample_rate = 1.0 ,
917+ send_default_pii = True ,
918+ )
919+ events = capture_events ()
920+
921+ large_content = (
922+ "This is a very long message that will exceed our size limits. " * 1000
923+ )
924+ small_content = "This is a small user message"
925+
926+ mock_http_response = create_mock_http_response (EXAMPLE_API_RESPONSE_JSON )
927+
928+ with mock .patch .object (
929+ mock_genai_client ._api_client , "request" , return_value = mock_http_response
930+ ):
931+ with start_transaction (name = "google_genai" ):
932+ mock_genai_client .models .generate_content (
933+ model = "gemini-1.5-flash" ,
934+ contents = small_content ,
935+ config = create_test_config (
936+ system_instruction = large_content ,
937+ ),
938+ )
939+
940+ (event ,) = events
941+ invoke_span = event ["spans" ][0 ]
942+ assert SPANDATA .GEN_AI_REQUEST_MESSAGES in invoke_span ["data" ]
943+
944+ messages_data = invoke_span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
945+ assert isinstance (messages_data , str )
946+
947+ parsed_messages = json .loads (messages_data )
948+ assert isinstance (parsed_messages , list )
949+ assert len (parsed_messages ) == 1
950+ assert parsed_messages [0 ]["role" ] == "user"
951+ assert small_content in parsed_messages [0 ]["content" ]
952+
953+ assert (
954+ event ["_meta" ]["spans" ]["0" ]["data" ]["gen_ai.request.messages" ]["" ]["len" ] == 2
955+ )
0 commit comments