44
55from google import genai
66from google .genai import types as genai_types
7+ from google .genai .types import Content , Part
78
89from sentry_sdk import start_transaction
910from sentry_sdk ._types import BLOB_DATA_SUBSTITUTE
@@ -106,11 +107,6 @@ def create_test_config(
106107 if seed is not None :
107108 config_dict ["seed" ] = seed
108109 if system_instruction is not None :
109- # Convert string to Content for system instruction
110- if isinstance (system_instruction , str ):
111- system_instruction = genai_types .Content (
112- parts = [genai_types .Part (text = system_instruction )], role = "system"
113- )
114110 config_dict ["system_instruction" ] = system_instruction
115111 if tools is not None :
116112 config_dict ["tools" ] = tools
@@ -186,6 +182,7 @@ def test_nonstreaming_generate_content(
186182 response_texts = json .loads (response_text )
187183 assert response_texts == ["Hello! How can I help you today?" ]
188184 else :
185+ assert SPANDATA .GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_span ["data" ]
189186 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in invoke_span ["data" ]
190187 assert SPANDATA .GEN_AI_RESPONSE_TEXT not in chat_span ["data" ]
191188
@@ -202,8 +199,41 @@ def test_nonstreaming_generate_content(
202199 assert invoke_span ["data" ][SPANDATA .GEN_AI_REQUEST_MAX_TOKENS ] == 100
203200
204201
202+ @pytest .mark .parametrize ("generate_content_config" , (False , True ))
203+ @pytest .mark .parametrize (
204+ "system_instructions,expected_texts" ,
205+ [
206+ (None , None ),
207+ ({}, []),
208+ (Content (role = "system" , parts = []), []),
209+ ({"parts" : []}, []),
210+ ("You are a helpful assistant." , ["You are a helpful assistant." ]),
211+ (Part (text = "You are a helpful assistant." ), ["You are a helpful assistant." ]),
212+ (
213+ Content (role = "system" , parts = [Part (text = "You are a helpful assistant." )]),
214+ ["You are a helpful assistant." ],
215+ ),
216+ ({"text" : "You are a helpful assistant." }, ["You are a helpful assistant." ]),
217+ (
218+ {"parts" : [Part (text = "You are a helpful assistant." )]},
219+ ["You are a helpful assistant." ],
220+ ),
221+ (
222+ {"parts" : [{"text" : "You are a helpful assistant." }]},
223+ ["You are a helpful assistant." ],
224+ ),
225+ (["You are a helpful assistant." ], ["You are a helpful assistant." ]),
226+ ([Part (text = "You are a helpful assistant." )], ["You are a helpful assistant." ]),
227+ ([{"text" : "You are a helpful assistant." }], ["You are a helpful assistant." ]),
228+ ],
229+ )
205230def test_generate_content_with_system_instruction (
206- sentry_init , capture_events , mock_genai_client
231+ sentry_init ,
232+ capture_events ,
233+ mock_genai_client ,
234+ generate_content_config ,
235+ system_instructions ,
236+ expected_texts ,
207237):
208238 sentry_init (
209239 integrations = [GoogleGenAIIntegration (include_prompts = True )],
@@ -218,25 +248,35 @@ def test_generate_content_with_system_instruction(
218248 mock_genai_client ._api_client , "request" , return_value = mock_http_response
219249 ):
220250 with start_transaction (name = "google_genai" ):
221- config = create_test_config (
222- system_instruction = "You are a helpful assistant" ,
223- temperature = 0.5 ,
224- )
251+ config = {
252+ "system_instruction" : system_instructions ,
253+ "temperature" : 0.5 ,
254+ }
255+
256+ if generate_content_config :
257+ config = create_test_config (** config )
258+
225259 mock_genai_client .models .generate_content (
226- model = "gemini-1.5-flash" , contents = "What is 2+2?" , config = config
260+ model = "gemini-1.5-flash" ,
261+ contents = "What is 2+2?" ,
262+ config = config ,
227263 )
228264
229265 (event ,) = events
230266 invoke_span = event ["spans" ][0 ]
231267
232- # Check that system instruction is included in messages
268+ if expected_texts is None :
269+ assert SPANDATA .GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_span ["data" ]
270+ return
271+
233272 # (PII is enabled and include_prompts is True in this test)
234- messages_str = invoke_span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
235- # Parse the JSON string to verify content
236- messages = json .loads (messages_str )
237- assert len (messages ) == 2
238- assert messages [0 ] == {"role" : "system" , "content" : "You are a helpful assistant" }
239- assert messages [1 ] == {"role" : "user" , "content" : "What is 2+2?" }
273+ system_instructions = json .loads (
274+ invoke_span ["data" ][SPANDATA .GEN_AI_SYSTEM_INSTRUCTIONS ]
275+ )
276+
277+ assert system_instructions == [
278+ {"type" : "text" , "content" : text } for text in expected_texts
279+ ]
240280
241281
242282def test_generate_content_with_tools (sentry_init , capture_events , mock_genai_client ):
@@ -933,10 +973,8 @@ def test_google_genai_message_truncation(
933973 with start_transaction (name = "google_genai" ):
934974 mock_genai_client .models .generate_content (
935975 model = "gemini-1.5-flash" ,
936- contents = small_content ,
937- config = create_test_config (
938- system_instruction = large_content ,
939- ),
976+ contents = [large_content , small_content ],
977+ config = create_test_config (),
940978 )
941979
942980 (event ,) = events
0 commit comments