Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 7 additions & 19 deletions sentry_sdk/integrations/google_genai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 6 additions & 11 deletions tests/integrations/google_genai/test_google_genai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Loading