Skip to content

Commit 49d4441

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add support for session TTL and expiration in Vertex AI session service
PiperOrigin-RevId: 936180164
1 parent d00ad67 commit 49d4441

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

src/google/adk/cli/cli_deploy.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
147147
'user_id': {'type': 'string'},
148148
'session_id': {'type': 'string', 'nullable': True},
149149
'state': {'type': 'object', 'nullable': True},
150+
'ttl': {'type': 'string', 'nullable': True},
151+
'expire_time': {'type': 'string', 'nullable': True},
150152
},
151153
'required': ['user_id'],
152154
'type': 'object',
@@ -215,19 +217,24 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
215217
'Creates a new session.\n\n Args:\n user_id'
216218
' (str):\n Required. The ID of the user.\n '
217219
' session_id (str):\n Optional. The ID of the'
218-
' session. If not provided, an ID\n will be be'
220+
' session. If not provided, an ID\n will be'
219221
' generated for the session.\n state (dict[str, Any]):\n'
220222
' Optional. The initial state of the session.\n '
221-
' **kwargs (dict[str, Any]):\n Optional.'
222-
' Additional keyword arguments to pass to the\n '
223-
' session service.\n\n Returns:\n Session: The'
224-
' newly created session instance.\n '
223+
' ttl (str):\n Optional. The time-to-live for'
224+
' the session.\n expire_time (str):\n '
225+
' Optional. The expiration time for the session.\n '
226+
' **kwargs (dict[str, Any]):\n Optional. Additional'
227+
' keyword arguments to pass to the\n session'
228+
' service.\n\n Returns:\n Session: The newly'
229+
' created session instance.\n '
225230
),
226231
'parameters': {
227232
'properties': {
228233
'user_id': {'type': 'string'},
229234
'session_id': {'type': 'string', 'nullable': True},
230235
'state': {'type': 'object', 'nullable': True},
236+
'ttl': {'type': 'string', 'nullable': True},
237+
'expire_time': {'type': 'string', 'nullable': True},
231238
},
232239
'required': ['user_id'],
233240
'type': 'object',

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,18 @@ async def create_session(
159159
state: The initial state of the session.
160160
session_id: The ID of the session.
161161
**kwargs: Additional arguments to pass to the session creation. E.g. set
162+
ttl='7200s' to set the session time-to-live or
162163
expire_time='2025-10-01T00:00:00Z' to set the session expiration time.
163164
See https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1beta1/projects.locations.reasoningEngines.sessions
164165
for more details.
166+
165167
Returns:
166168
The created session.
167169
"""
170+
if kwargs.get('ttl') is not None and kwargs.get('expire_time') is not None:
171+
raise ValueError(
172+
"Cannot specify both 'ttl' and 'expire_time' simultaneously."
173+
)
168174
reasoning_engine_id = self._get_reasoning_engine_id(app_name)
169175

170176
config = {'session_state': state} if state else {}

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,46 @@ async def test_create_session_with_custom_config(mock_api_client_instance):
10081008
)
10091009

10101010

1011+
@pytest.mark.asyncio
1012+
@pytest.mark.usefixtures('mock_get_api_client')
1013+
async def test_create_session_with_ttl(mock_api_client_instance):
1014+
session_service = mock_vertex_ai_session_service()
1015+
1016+
ttl = '7200s'
1017+
await session_service.create_session(app_name='123', user_id='user', ttl=ttl)
1018+
assert mock_api_client_instance.last_create_session_config['ttl'] == ttl
1019+
1020+
1021+
@pytest.mark.asyncio
1022+
@pytest.mark.usefixtures('mock_get_api_client')
1023+
async def test_create_session_with_ttl_and_expire_time_raises_value_error(
1024+
mock_api_client_instance,
1025+
):
1026+
session_service = mock_vertex_ai_session_service()
1027+
with pytest.raises(
1028+
ValueError,
1029+
match="Cannot specify both 'ttl' and 'expire_time' simultaneously.",
1030+
):
1031+
await session_service.create_session(
1032+
app_name='123',
1033+
user_id='user',
1034+
ttl='7200s',
1035+
expire_time='2025-12-12T12:12:12.123456Z',
1036+
)
1037+
1038+
1039+
@pytest.mark.asyncio
1040+
@pytest.mark.usefixtures('mock_get_api_client')
1041+
async def test_create_session_with_ttl_none_and_expire_time_none_does_not_raise(
1042+
mock_api_client_instance,
1043+
):
1044+
session_service = mock_vertex_ai_session_service()
1045+
# None means "not set"; passing both as None must not raise.
1046+
await session_service.create_session(
1047+
app_name='123', user_id='user', ttl=None, expire_time=None
1048+
)
1049+
1050+
10111051
@pytest.mark.asyncio
10121052
@pytest.mark.usefixtures('mock_get_api_client')
10131053
async def test_append_event():

0 commit comments

Comments
 (0)