Skip to content

Commit 9abe620

Browse files
manually write test cases to be exhaustive
1 parent 8208252 commit 9abe620

File tree

2 files changed

+22
-87
lines changed

2 files changed

+22
-87
lines changed

sentry_sdk/integrations/google_genai/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ def extract_finish_reasons(
725725

726726
def _transform_system_instruction_one_level(
727727
system_instructions: "ContentUnionDict",
728+
can_be_content: bool,
728729
) -> "list[TextPart]":
729730
text_parts: "list[TextPart]" = []
730731

@@ -734,7 +735,7 @@ def _transform_system_instruction_one_level(
734735
if isinstance(system_instructions, Part) and system_instructions.text:
735736
return [{"type": "text", "content": system_instructions.text}]
736737

737-
if isinstance(system_instructions, Content):
738+
if can_be_content and isinstance(system_instructions, Content):
738739
for part in system_instructions.parts or []:
739740
if part.text:
740741
text_parts.append({"type": "text", "content": part.text})
@@ -744,6 +745,7 @@ def _transform_system_instruction_one_level(
744745
if system_instructions.get("text"):
745746
return [{"type": "text", "content": system_instructions["text"]}]
746747

748+
elif can_be_content and isinstance(system_instructions, dict):
747749
parts = system_instructions.get("parts", [])
748750
for part in parts:
749751
if isinstance(part, Part) and part.text:
@@ -763,14 +765,18 @@ def _transform_system_instructions(
763765
if isinstance(system_instructions, list):
764766
text_parts = list(
765767
chain.from_iterable(
766-
_transform_system_instruction_one_level(instructions)
768+
_transform_system_instruction_one_level(
769+
instructions, can_be_content=False
770+
)
767771
for instructions in system_instructions
768772
)
769773
)
770774

771775
return text_parts
772776

773-
return _transform_system_instruction_one_level(system_instructions)
777+
return _transform_system_instruction_one_level(
778+
system_instructions, can_be_content=True
779+
)
774780

775781

776782
def set_span_data_for_request(

tests/integrations/google_genai/test_google_genai.py

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

206206

207-
# Threatened bot to generate as many cases as possible
208207
@pytest.mark.parametrize(
209208
"system_instructions,expected_texts",
210209
[
211-
("You are a helpful assistant", ["You are a helpful assistant"]),
212-
(
213-
["You are a translator", "Translate to French"],
214-
["You are a translator", "Translate to French"],
215-
),
216-
(
217-
Content(role="user", parts=[Part(text="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"],
229-
),
230-
(
231-
{"parts": [{"text": "You are a helpful assistant"}], "role": "user"},
232-
["You are a helpful assistant"],
233-
),
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"]),
246-
(
247-
[Part(text="You are a translator"), Part(text="Translate to French")],
248-
["You are a translator", "Translate to French"],
249-
),
250-
(
251-
[{"text": "You are a translator"}, {"text": "Translate to French"}],
252-
["You are a translator", "Translate to French"],
253-
),
254-
(
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"},
263-
],
264-
"role": "user",
265-
},
266-
["First instruction", "Second instruction"],
267-
),
268210
(None, None),
269211
("", []),
270212
({}, []),
213+
(Content(role="system", parts=[]), []),
271214
({"parts": []}, []),
272-
(Content(role="user", parts=[]), []),
215+
("You are a helpful assistant.", ["You are a helpful assistant."]),
216+
(Part(text="You are a helpful assistant."), ["You are a helpful assistant."]),
273217
(
274-
{
275-
"parts": [
276-
{"text": "Text part"},
277-
{"file_data": {"file_uri": "gs://bucket/file.pdf"}},
278-
],
279-
"role": "user",
280-
},
281-
["Text part"],
218+
Content(role="system", parts=[Part(text="You are a helpful assistant.")]),
219+
["You are a helpful assistant."],
282220
),
221+
({"text": "You are a helpful assistant."}, ["You are a helpful assistant."]),
283222
(
284-
{
285-
"parts": [
286-
{"text": "First"},
287-
Part(text="Second"),
288-
{"text": "Third"},
289-
],
290-
"role": "user",
291-
},
292-
["First", "Second", "Third"],
223+
{"parts": [Part(text="You are a helpful assistant.")]},
224+
["You are a helpful assistant."],
293225
),
294226
(
295-
{
296-
"parts": [
297-
Part(text="First"),
298-
Part(text="Second"),
299-
Part(text="Third"),
300-
],
301-
},
302-
["First", "Second", "Third"],
227+
{"parts": [{"text": "You are a helpful assistant."}]},
228+
["You are a helpful assistant."],
303229
),
230+
(["You are a helpful assistant."], ["You are a helpful assistant."]),
231+
([Part(text="You are a helpful assistant.")], ["You are a helpful assistant."]),
232+
([{"text": "You are a helpful assistant."}], ["You are a helpful assistant."]),
304233
],
305234
)
306235
def test_generate_content_with_system_instruction(

0 commit comments

Comments
 (0)