Skip to content

Commit b79c414

Browse files
hkt74MarkDaoust
authored andcommitted
feat: add time range filter to Google Search Tool
PiperOrigin-RevId: 758986641
1 parent cbd1ea6 commit b79c414

File tree

5 files changed

+199
-2
lines changed

5 files changed

+199
-2
lines changed

google/genai/_live_converters.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,50 @@ def _FunctionDeclaration_to_vertex(
250250
return to_object
251251

252252

253+
def _Interval_to_mldev(
254+
api_client: BaseApiClient,
255+
from_object: Union[dict[str, Any], object],
256+
parent_object: Optional[dict[str, Any]] = None,
257+
) -> dict[str, Any]:
258+
to_object: dict[str, Any] = {}
259+
if getv(from_object, ['start_time']) is not None:
260+
setv(to_object, ['startTime'], getv(from_object, ['start_time']))
261+
262+
if getv(from_object, ['end_time']) is not None:
263+
setv(to_object, ['endTime'], getv(from_object, ['end_time']))
264+
265+
return to_object
266+
267+
268+
def _Interval_to_vertex(
269+
api_client: BaseApiClient,
270+
from_object: Union[dict[str, Any], object],
271+
parent_object: Optional[dict[str, Any]] = None,
272+
) -> dict[str, Any]:
273+
to_object: dict[str, Any] = {}
274+
if getv(from_object, ['start_time']) is not None:
275+
setv(to_object, ['startTime'], getv(from_object, ['start_time']))
276+
277+
if getv(from_object, ['end_time']) is not None:
278+
setv(to_object, ['endTime'], getv(from_object, ['end_time']))
279+
280+
return to_object
281+
282+
253283
def _GoogleSearch_to_mldev(
254284
api_client: BaseApiClient,
255285
from_object: Union[dict[str, Any], object],
256286
parent_object: Optional[dict[str, Any]] = None,
257287
) -> dict[str, Any]:
258288
to_object: dict[str, Any] = {}
289+
if getv(from_object, ['time_range_filter']) is not None:
290+
setv(
291+
to_object,
292+
['timeRangeFilter'],
293+
_Interval_to_mldev(
294+
api_client, getv(from_object, ['time_range_filter']), to_object
295+
),
296+
)
259297

260298
return to_object
261299

@@ -266,6 +304,14 @@ def _GoogleSearch_to_vertex(
266304
parent_object: Optional[dict[str, Any]] = None,
267305
) -> dict[str, Any]:
268306
to_object: dict[str, Any] = {}
307+
if getv(from_object, ['time_range_filter']) is not None:
308+
setv(
309+
to_object,
310+
['timeRangeFilter'],
311+
_Interval_to_vertex(
312+
api_client, getv(from_object, ['time_range_filter']), to_object
313+
),
314+
)
269315

270316
return to_object
271317

google/genai/caches.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,35 @@ def _FunctionDeclaration_to_mldev(
144144
return to_object
145145

146146

147+
def _Interval_to_mldev(
148+
api_client: BaseApiClient,
149+
from_object: Union[dict[str, Any], object],
150+
parent_object: Optional[dict[str, Any]] = None,
151+
) -> dict[str, Any]:
152+
to_object: dict[str, Any] = {}
153+
if getv(from_object, ['start_time']) is not None:
154+
setv(to_object, ['startTime'], getv(from_object, ['start_time']))
155+
156+
if getv(from_object, ['end_time']) is not None:
157+
setv(to_object, ['endTime'], getv(from_object, ['end_time']))
158+
159+
return to_object
160+
161+
147162
def _GoogleSearch_to_mldev(
148163
api_client: BaseApiClient,
149164
from_object: Union[dict[str, Any], object],
150165
parent_object: Optional[dict[str, Any]] = None,
151166
) -> dict[str, Any]:
152167
to_object: dict[str, Any] = {}
168+
if getv(from_object, ['time_range_filter']) is not None:
169+
setv(
170+
to_object,
171+
['timeRangeFilter'],
172+
_Interval_to_mldev(
173+
api_client, getv(from_object, ['time_range_filter']), to_object
174+
),
175+
)
153176

154177
return to_object
155178

@@ -724,12 +747,35 @@ def _FunctionDeclaration_to_vertex(
724747
return to_object
725748

726749

750+
def _Interval_to_vertex(
751+
api_client: BaseApiClient,
752+
from_object: Union[dict[str, Any], object],
753+
parent_object: Optional[dict[str, Any]] = None,
754+
) -> dict[str, Any]:
755+
to_object: dict[str, Any] = {}
756+
if getv(from_object, ['start_time']) is not None:
757+
setv(to_object, ['startTime'], getv(from_object, ['start_time']))
758+
759+
if getv(from_object, ['end_time']) is not None:
760+
setv(to_object, ['endTime'], getv(from_object, ['end_time']))
761+
762+
return to_object
763+
764+
727765
def _GoogleSearch_to_vertex(
728766
api_client: BaseApiClient,
729767
from_object: Union[dict[str, Any], object],
730768
parent_object: Optional[dict[str, Any]] = None,
731769
) -> dict[str, Any]:
732770
to_object: dict[str, Any] = {}
771+
if getv(from_object, ['time_range_filter']) is not None:
772+
setv(
773+
to_object,
774+
['timeRangeFilter'],
775+
_Interval_to_vertex(
776+
api_client, getv(from_object, ['time_range_filter']), to_object
777+
),
778+
)
733779

734780
return to_object
735781

google/genai/models.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,35 @@ def _FunctionDeclaration_to_mldev(
177177
return to_object
178178

179179

180+
def _Interval_to_mldev(
181+
api_client: BaseApiClient,
182+
from_object: Union[dict[str, Any], object],
183+
parent_object: Optional[dict[str, Any]] = None,
184+
) -> dict[str, Any]:
185+
to_object: dict[str, Any] = {}
186+
if getv(from_object, ['start_time']) is not None:
187+
setv(to_object, ['startTime'], getv(from_object, ['start_time']))
188+
189+
if getv(from_object, ['end_time']) is not None:
190+
setv(to_object, ['endTime'], getv(from_object, ['end_time']))
191+
192+
return to_object
193+
194+
180195
def _GoogleSearch_to_mldev(
181196
api_client: BaseApiClient,
182197
from_object: Union[dict[str, Any], object],
183198
parent_object: Optional[dict[str, Any]] = None,
184199
) -> dict[str, Any]:
185200
to_object: dict[str, Any] = {}
201+
if getv(from_object, ['time_range_filter']) is not None:
202+
setv(
203+
to_object,
204+
['timeRangeFilter'],
205+
_Interval_to_mldev(
206+
api_client, getv(from_object, ['time_range_filter']), to_object
207+
),
208+
)
186209

187210
return to_object
188211

@@ -1374,12 +1397,35 @@ def _FunctionDeclaration_to_vertex(
13741397
return to_object
13751398

13761399

1400+
def _Interval_to_vertex(
1401+
api_client: BaseApiClient,
1402+
from_object: Union[dict[str, Any], object],
1403+
parent_object: Optional[dict[str, Any]] = None,
1404+
) -> dict[str, Any]:
1405+
to_object: dict[str, Any] = {}
1406+
if getv(from_object, ['start_time']) is not None:
1407+
setv(to_object, ['startTime'], getv(from_object, ['start_time']))
1408+
1409+
if getv(from_object, ['end_time']) is not None:
1410+
setv(to_object, ['endTime'], getv(from_object, ['end_time']))
1411+
1412+
return to_object
1413+
1414+
13771415
def _GoogleSearch_to_vertex(
13781416
api_client: BaseApiClient,
13791417
from_object: Union[dict[str, Any], object],
13801418
parent_object: Optional[dict[str, Any]] = None,
13811419
) -> dict[str, Any]:
13821420
to_object: dict[str, Any] = {}
1421+
if getv(from_object, ['time_range_filter']) is not None:
1422+
setv(
1423+
to_object,
1424+
['timeRangeFilter'],
1425+
_Interval_to_vertex(
1426+
api_client, getv(from_object, ['time_range_filter']), to_object
1427+
),
1428+
)
13831429

13841430
return to_object
13851431

google/genai/tests/models/test_generate_content.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ class InstrumentEnum(Enum):
227227
tools=[types.Tool(google_search=types.GoogleSearch())]
228228
),
229229
),
230+
),
231+
pytest_helper.TestTableItem(
232+
name='test_google_search_tool_with_time_range_filter',
233+
parameters=types._GenerateContentParameters(
234+
model='gemini-2.0-flash-exp',
235+
contents=t.t_contents(None, 'What is the QQQ stock price?'),
236+
config=types.GenerateContentConfig(
237+
tools=[types.Tool(google_search=types.GoogleSearch(
238+
time_range_filter=types.Interval(
239+
start_time=datetime.fromisoformat('2025-05-01T00:00:00Z'),
240+
end_time=datetime.fromisoformat('2025-05-03T00:00:00Z'),
241+
)
242+
))]
243+
),
244+
),
230245
),
231246
pytest_helper.TestTableItem(
232247
name='test_speech_with_config',

google/genai/types.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,16 +1792,60 @@ class FunctionDeclarationDict(TypedDict, total=False):
17921792
FunctionDeclarationOrDict = Union[FunctionDeclaration, FunctionDeclarationDict]
17931793

17941794

1795+
class Interval(_common.BaseModel):
1796+
"""Represents a time interval, encoded as a start time (inclusive) and an end time (exclusive).
1797+
1798+
The start time must be less than or equal to the end time.
1799+
When the start equals the end time, the interval is an empty interval.
1800+
(matches no time)
1801+
When both start and end are unspecified, the interval matches any time.
1802+
"""
1803+
1804+
start_time: Optional[datetime.datetime] = Field(
1805+
default=None, description="""The start time of the interval."""
1806+
)
1807+
end_time: Optional[datetime.datetime] = Field(
1808+
default=None, description="""The end time of the interval."""
1809+
)
1810+
1811+
1812+
class IntervalDict(TypedDict, total=False):
1813+
"""Represents a time interval, encoded as a start time (inclusive) and an end time (exclusive).
1814+
1815+
The start time must be less than or equal to the end time.
1816+
When the start equals the end time, the interval is an empty interval.
1817+
(matches no time)
1818+
When both start and end are unspecified, the interval matches any time.
1819+
"""
1820+
1821+
start_time: Optional[datetime.datetime]
1822+
"""The start time of the interval."""
1823+
1824+
end_time: Optional[datetime.datetime]
1825+
"""The end time of the interval."""
1826+
1827+
1828+
IntervalOrDict = Union[Interval, IntervalDict]
1829+
1830+
17951831
class GoogleSearch(_common.BaseModel):
17961832
"""Tool to support Google Search in Model. Powered by Google."""
17971833

1798-
pass
1834+
time_range_filter: Optional[Interval] = Field(
1835+
default=None,
1836+
description="""Optional. Filter search results to a specific time range.
1837+
If customers set a start time, they must set an end time (and vice versa).
1838+
""",
1839+
)
17991840

18001841

18011842
class GoogleSearchDict(TypedDict, total=False):
18021843
"""Tool to support Google Search in Model. Powered by Google."""
18031844

1804-
pass
1845+
time_range_filter: Optional[IntervalDict]
1846+
"""Optional. Filter search results to a specific time range.
1847+
If customers set a start time, they must set an end time (and vice versa).
1848+
"""
18051849

18061850

18071851
GoogleSearchOrDict = Union[GoogleSearch, GoogleSearchDict]

0 commit comments

Comments
 (0)