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..6433b3ab21 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -229,14 +229,11 @@ 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"][0]["text"] == "You are a helpful assistant" def test_generate_content_with_tools(sentry_init, capture_events, mock_genai_client): @@ -933,10 +930,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