1616import os
1717import unittest
1818
19- from google .genai .types import GenerateContentConfig
20-
2119from .base import TestCase
2220
2321
@@ -37,7 +35,7 @@ def generate_content(self, *args, **kwargs):
3735 def expected_function_name (self ):
3836 raise NotImplementedError ("Must implement 'expected_function_name'." )
3937
40- def generate_and_get_span (self , config ):
38+ def _generate_and_get_span (self , config ):
4139 self .generate_content (
4240 model = "gemini-2.0-flash" ,
4341 contents = "Some input prompt" ,
@@ -105,125 +103,6 @@ def test_generated_span_has_vertex_ai_system_when_configured(self):
105103 span .attributes ["gen_ai.operation.name" ], "generate_content"
106104 )
107105
108- def test_option_reflected_to_span_attribute_choice_count_config_dict (self ):
109- self .configure_valid_response (text = "Some response" )
110- span = self .generate_and_get_span (config = {"candidate_count" : 2 })
111- self .assertEqual (span .attributes ["gen_ai.request.choice.count" ], 2 )
112-
113- def test_option_reflected_to_span_attribute_choice_count_config_obj (self ):
114- self .configure_valid_response (text = "Some response" )
115- span = self .generate_and_get_span (
116- config = GenerateContentConfig (candidate_count = 2 )
117- )
118- self .assertEqual (span .attributes ["gen_ai.request.choice.count" ], 2 )
119-
120- def test_option_reflected_to_span_attribute_seed_config_dict (self ):
121- self .configure_valid_response (text = "Some response" )
122- span = self .generate_and_get_span (config = {"seed" : 12345 })
123- self .assertEqual (span .attributes ["gen_ai.request.seed" ], 12345 )
124-
125- def test_option_reflected_to_span_attribute_seed_config_obj (self ):
126- self .configure_valid_response (text = "Some response" )
127- span = self .generate_and_get_span (
128- config = GenerateContentConfig (seed = 12345 )
129- )
130- self .assertEqual (span .attributes ["gen_ai.request.seed" ], 12345 )
131-
132- def test_option_reflected_to_span_attribute_frequency_penalty (self ):
133- self .configure_valid_response (text = "Some response" )
134- span = self .generate_and_get_span (config = {"frequency_penalty" : 1.0 })
135- self .assertEqual (
136- span .attributes ["gen_ai.request.frequency_penalty" ], 1.0
137- )
138-
139- def test_option_reflected_to_span_attribute_max_tokens (self ):
140- self .configure_valid_response (text = "Some response" )
141- span = self .generate_and_get_span (
142- config = GenerateContentConfig (max_output_tokens = 5000 )
143- )
144- self .assertEqual (span .attributes ["gen_ai.request.max_tokens" ], 5000 )
145-
146- def test_option_reflected_to_span_attribute_presence_penalty (self ):
147- self .configure_valid_response (text = "Some response" )
148- span = self .generate_and_get_span (
149- config = GenerateContentConfig (presence_penalty = 0.5 )
150- )
151- self .assertEqual (
152- span .attributes ["gen_ai.request.presence_penalty" ], 0.5
153- )
154-
155- def test_option_reflected_to_span_attribute_stop_sequences (self ):
156- self .configure_valid_response (text = "Some response" )
157- span = self .generate_and_get_span (
158- config = {"stop_sequences" : ["foo" , "bar" ]}
159- )
160- stop_sequences = span .attributes ["gen_ai.request.stop_sequences" ]
161- self .assertEqual (len (stop_sequences ), 2 )
162- self .assertEqual (stop_sequences [0 ], "foo" )
163- self .assertEqual (stop_sequences [1 ], "bar" )
164-
165- def test_option_reflected_to_span_attribute_top_k (self ):
166- self .configure_valid_response (text = "Some response" )
167- span = self .generate_and_get_span (
168- config = GenerateContentConfig (top_k = 20 )
169- )
170- self .assertEqual (span .attributes ["gen_ai.request.top_k" ], 20 )
171-
172- def test_option_reflected_to_span_attribute_top_p (self ):
173- self .configure_valid_response (text = "Some response" )
174- span = self .generate_and_get_span (config = {"top_p" : 10 })
175- self .assertEqual (span .attributes ["gen_ai.request.top_p" ], 10 )
176-
177- def test_option_not_reflected_to_span_attribute_system_instruction (self ):
178- self .configure_valid_response (text = "Some response" )
179- span = self .generate_and_get_span (
180- config = {"system_instruction" : "Yadda yadda yadda" }
181- )
182- self .assertNotIn (
183- "gen_ai.gcp.request.system_instruction" , span .attributes
184- )
185- self .assertNotIn ("gen_ai.request.system_instruction" , span .attributes )
186- for key in span .attributes :
187- value = span .attributes [key ]
188- if isinstance (value , str ):
189- self .assertNotIn ("Yadda yadda yadda" , value )
190-
191- def test_option_not_reflected_to_span_attribute_http_headers (self ):
192- self .configure_valid_response (text = "Some response" )
193- span = self .generate_and_get_span (
194- config = {
195- "http_options" : {
196- "base_url" : "my.backend.override" ,
197- "headers" : {
198- "sensitive" : 12345 ,
199- },
200- }
201- }
202- )
203- self .assertEqual (
204- span .attributes ["gen_ai.gcp.request.http_options.base_url" ],
205- "my.backend.override" ,
206- )
207- self .assertNotIn (
208- "gen_ai.gcp.request.http_options.headers.sensitive" ,
209- span .attributes ,
210- )
211-
212- def test_option_reflected_to_span_attribute_automatic_func_calling (self ):
213- self .configure_valid_response (text = "Some response" )
214- span = self .generate_and_get_span (
215- config = {
216- "automatic_function_calling" : {
217- "ignore_call_history" : True ,
218- }
219- }
220- )
221- self .assertTrue (
222- span .attributes [
223- "gen_ai.gcp.request.automatic_function_calling.ignore_call_history"
224- ]
225- )
226-
227106 def test_generated_span_counts_tokens (self ):
228107 self .configure_valid_response (input_tokens = 123 , output_tokens = 456 )
229108 self .generate_content (model = "gemini-2.0-flash" , contents = "Some input" )
0 commit comments