From c4ebb20f70ada52f53f9b60c6f7f5114fd7ed38d Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 21 Jan 2026 11:14:06 +0100 Subject: [PATCH 1/3] feat(google-genai): Set system instruction attribute --- sentry_sdk/consts.py | 6 ++++ sentry_sdk/integrations/google_genai/utils.py | 26 ++++---------- .../google_genai/test_google_genai.py | 34 +++++++++++++------ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 93fca6ba3e..4b61a317fb 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -542,6 +542,12 @@ class SPANDATA: Example: 2048 """ + GEN_AI_SYSTEM_INSTRUCTIONS = "gen_ai.system_instructions" + """ + The system instructions passed to the model. + Example: [{"type": "text", "text": "You are a helpful assistant."},{"type": "text", "text": "Be concise and clear."}] + """ + GEN_AI_REQUEST_MESSAGES = "gen_ai.request.messages" """ The messages passed to the model. The "content" can be a string or an array of objects. diff --git a/sentry_sdk/integrations/google_genai/utils.py b/sentry_sdk/integrations/google_genai/utils.py index 3b18712d3e..aed0aeb18d 100644 --- a/sentry_sdk/integrations/google_genai/utils.py +++ b/sentry_sdk/integrations/google_genai/utils.py @@ -743,25 +743,13 @@ def set_span_data_for_request( # Add system instruction if present if config and hasattr(config, "system_instruction"): system_instruction = config.system_instruction - if system_instruction: - system_messages = extract_contents_messages(system_instruction) - # System instruction should be a single system message - # Extract text from all messages and combine into one system message - system_texts = [] - for msg in system_messages: - content = msg.get("content") - if isinstance(content, list): - # Extract text from content parts - for part in content: - if isinstance(part, dict) and part.get("type") == "text": - system_texts.append(part.get("text", "")) - elif isinstance(content, str): - system_texts.append(content) - - if system_texts: - messages.append( - {"role": "system", "content": " ".join(system_texts)} - ) + + set_data_normalized( + span, + SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + system_instruction, + unpack=False, + ) # Extract messages from contents contents_messages = extract_contents_messages(contents) diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index ad89b878ea..eee61fbc12 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -229,14 +229,28 @@ def test_generate_content_with_system_instruction( (event,) = events invoke_span = event["spans"][0] - # Check that system instruction is included in messages # (PII is enabled and include_prompts is True in this test) - messages_str = invoke_span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] - # Parse the JSON string to verify content - messages = json.loads(messages_str) - assert len(messages) == 2 - assert messages[0] == {"role": "system", "content": "You are a helpful assistant"} - assert messages[1] == {"role": "user", "content": "What is 2+2?"} + system_instructions = json.loads( + invoke_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + ) + assert system_instructions == { + "parts": [ + { + "media_resolution": "None", + "code_execution_result": "None", + "executable_code": "None", + "file_data": "None", + "function_call": "None", + "function_response": "None", + "inline_data": "None", + "text": "You are a helpful assistant", + "thought": "None", + "thought_signature": "None", + "video_metadata": "None", + } + ], + "role": "system", + } def test_generate_content_with_tools(sentry_init, capture_events, mock_genai_client): @@ -933,10 +947,8 @@ def test_google_genai_message_truncation( with start_transaction(name="google_genai"): mock_genai_client.models.generate_content( model="gemini-1.5-flash", - contents=small_content, - config=create_test_config( - system_instruction=large_content, - ), + contents=[large_content, small_content], + config=create_test_config(), ) (event,) = events From 87687029e69173be67e0ad0870cab36dbd41b516 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 21 Jan 2026 14:21:02 +0100 Subject: [PATCH 2/3] . --- .../google_genai/test_google_genai.py | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index eee61fbc12..984a339d6e 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -233,24 +233,7 @@ def test_generate_content_with_system_instruction( system_instructions = json.loads( invoke_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] ) - assert system_instructions == { - "parts": [ - { - "media_resolution": "None", - "code_execution_result": "None", - "executable_code": "None", - "file_data": "None", - "function_call": "None", - "function_response": "None", - "inline_data": "None", - "text": "You are a helpful assistant", - "thought": "None", - "thought_signature": "None", - "video_metadata": "None", - } - ], - "role": "system", - } + assert system_instructions["parts"]["text"] == "You are a helpful assistant" def test_generate_content_with_tools(sentry_init, capture_events, mock_genai_client): From eb4a9ae1cdfc1f0c4870edce9cc7fc7b64adc54a Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 21 Jan 2026 14:26:36 +0100 Subject: [PATCH 3/3] . --- tests/integrations/google_genai/test_google_genai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index 984a339d6e..6433b3ab21 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -233,7 +233,7 @@ def test_generate_content_with_system_instruction( system_instructions = json.loads( invoke_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] ) - assert system_instructions["parts"]["text"] == "You are a helpful assistant" + assert system_instructions["parts"][0]["text"] == "You are a helpful assistant" def test_generate_content_with_tools(sentry_init, capture_events, mock_genai_client):