Skip to content

Commit 6cf31ed

Browse files
matthew29tangcopybara-github
authored andcommitted
fix: Fix models.list() filter parameter
PiperOrigin-RevId: 827662887
1 parent 2ff9886 commit 6cf31ed

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

google/genai/models.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,7 +4536,12 @@ def _list(
45364536
else:
45374537
path = '{models_url}'
45384538
query_params = request_dict.get('_query')
4539-
if query_params:
4539+
if query_params and query_params.get('filter'):
4540+
query_param_filter = query_params.pop('filter')
4541+
path = f'{path}?filter={query_param_filter}'
4542+
if query_params:
4543+
path += f'&{urlencode(query_params)}'
4544+
elif query_params:
45404545
path = f'{path}?{urlencode(query_params)}'
45414546
# TODO: remove the hack that pops config.
45424547
request_dict.pop('config', None)
@@ -6376,7 +6381,12 @@ async def _list(
63766381
else:
63776382
path = '{models_url}'
63786383
query_params = request_dict.get('_query')
6379-
if query_params:
6384+
if query_params and query_params.get('filter'):
6385+
query_param_filter = query_params.pop('filter')
6386+
path = f'{path}?filter={query_param_filter}'
6387+
if query_params:
6388+
path += f'&{urlencode(query_params)}'
6389+
elif query_params:
63806390
path = f'{path}?{urlencode(query_params)}'
63816391
# TODO: remove the hack that pops config.
63826392
request_dict.pop('config', None)

google/genai/tests/models/test_list.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@
3030
test_table: list[pytest_helper.TestTableItem] = [
3131
pytest_helper.TestTableItem(
3232
name='test_tuned_models',
33-
parameters=types._ListModelsParameters(config={'query_base': False}),
33+
parameters=types._ListModelsParameters(
34+
config={'query_base': False, 'page_size': 3}
35+
),
36+
),
37+
pytest_helper.TestTableItem(
38+
name='test_tuned_models_with_filter',
39+
parameters=types._ListModelsParameters(
40+
config={'query_base': False,
41+
'page_size': 3,
42+
'filter': 'displayName="gemini-2.5-flash-1b0689e3-9773-43b4-97eb-c8140d5f183b"'}
43+
),
3444
),
3545
pytest_helper.TestTableItem(
3646
name='test_base_models',
@@ -193,13 +203,13 @@ def test_empty_api_response_empty_dict_headers(mock_api_client, client):
193203

194204
@pytest.mark.asyncio
195205
async def test_tuned_models_async_pager(client):
196-
pager = await client.aio.models.list(config={'page_size': 10, 'query_base': False})
206+
pager = await client.aio.models.list(config={'page_size': 3, 'query_base': False})
197207

198208
assert 'Content-Type' in pager.sdk_http_response.headers
199209
assert 'Content-Encoding' in pager.sdk_http_response.headers
200210
assert pager.name == 'models'
201-
assert pager.page_size == 10
202-
assert len(pager) <= 10
211+
assert pager.page_size == 3
212+
assert len(pager) <= 3
203213

204214
# Iterate through all the pages. Then next_page() should raise an exception.
205215
async for _ in pager:

0 commit comments

Comments
 (0)