Skip to content

Commit 2b9bd4e

Browse files
more tests
1 parent 9031f00 commit 2b9bd4e

2 files changed

Lines changed: 70 additions & 16 deletions

File tree

sentry_sdk/integrations/google_genai/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def _transform_system_instruction_one_level(
735735
return [{"type": "text", "content": system_instructions.text}]
736736

737737
if isinstance(system_instructions, Content):
738-
for part in system_instructions.parts:
738+
for part in system_instructions.parts or []:
739739
if part.text:
740740
text_parts.append({"type": "text", "content": part.text})
741741
return text_parts
@@ -747,9 +747,9 @@ def _transform_system_instruction_one_level(
747747
parts = system_instructions.get("parts", [])
748748
for part in parts:
749749
if part.get("text"):
750-
part.append({"type": "text", "content": part["text"]})
750+
text_parts.append({"type": "text", "content": part["text"]})
751751
elif isinstance(part, Part) and part.text:
752-
part.append({"type": "text", "content": part.text})
752+
text_parts.append({"type": "text", "content": part.text})
753753
return text_parts
754754

755755
return text_parts

tests/integrations/google_genai/test_google_genai.py

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,45 @@ def test_nonstreaming_generate_content(
204204
assert invoke_span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100
205205

206206

207-
# Vibed test cases
207+
# Threatened bot to generate as many cases as possible
208208
@pytest.mark.parametrize(
209209
"system_instructions,expected_texts",
210210
[
211-
("You are a helpful assistant", "You are a helpful assistant"),
211+
("You are a helpful assistant", ["You are a helpful assistant"]),
212212
(
213213
["You are a translator", "Translate to French"],
214214
["You are a translator", "Translate to French"],
215215
),
216216
(
217217
Content(role="user", parts=[Part(text="You are a helpful assistant")]),
218-
"You are a helpful assistant",
218+
["You are a helpful assistant"],
219+
),
220+
(
221+
Content(
222+
role="user",
223+
parts=[
224+
Part(text="You are a translator"),
225+
Part(text="Translate to French"),
226+
],
227+
),
228+
["You are a translator", "Translate to French"],
219229
),
220230
(
221231
{"parts": [{"text": "You are a helpful assistant"}], "role": "user"},
222-
"You are a helpful assistant",
232+
["You are a helpful assistant"],
223233
),
224-
(Part(text="You are a helpful assistant"), "You are a helpful assistant"),
225-
({"text": "You are a helpful assistant"}, "You are a helpful assistant"),
234+
(
235+
{
236+
"parts": [
237+
{"text": "You are a translator"},
238+
{"text": "Translate to French"},
239+
],
240+
"role": "user",
241+
},
242+
["You are a translator", "Translate to French"],
243+
),
244+
(Part(text="You are a helpful assistant"), ["You are a helpful assistant"]),
245+
({"text": "You are a helpful assistant"}, ["You are a helpful assistant"]),
226246
(
227247
[Part(text="You are a translator"), Part(text="Translate to French")],
228248
["You are a translator", "Translate to French"],
@@ -232,14 +252,44 @@ def test_nonstreaming_generate_content(
232252
["You are a translator", "Translate to French"],
233253
),
234254
(
235-
Content(
236-
role="user",
237-
parts=[
238-
Part(text="You are a translator"),
239-
Part(text="Translate to French"),
255+
[Part(text="First instruction"), {"text": "Second instruction"}],
256+
["First instruction", "Second instruction"],
257+
),
258+
(
259+
{
260+
"parts": [
261+
Part(text="First instruction"),
262+
{"text": "Second instruction"},
240263
],
241-
),
242-
["You are a translator", "Translate to French"],
264+
"role": "user",
265+
},
266+
["First instruction", "Second instruction"],
267+
),
268+
(None, None),
269+
("", []),
270+
({}, []),
271+
({"parts": []}, []),
272+
(Content(role="user", parts=[]), []),
273+
(
274+
{
275+
"parts": [
276+
{"text": "Text part"},
277+
{"file_data": {"file_uri": "gs://bucket/file.pdf"}},
278+
],
279+
"role": "user",
280+
},
281+
["Text part"],
282+
),
283+
(
284+
{
285+
"parts": [
286+
{"text": "First"},
287+
Part(text="Second"),
288+
{"text": "Third"},
289+
],
290+
"role": "user",
291+
},
292+
["First", "Second", "Third"],
243293
),
244294
],
245295
)
@@ -270,6 +320,10 @@ def test_generate_content_with_system_instruction(
270320
(event,) = events
271321
invoke_span = event["spans"][0]
272322

323+
if expected_texts is None:
324+
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_span["data"]
325+
return
326+
273327
# (PII is enabled and include_prompts is True in this test)
274328
system_instructions = json.loads(
275329
invoke_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]

0 commit comments

Comments
 (0)