Skip to content

Commit 3ab365e

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add support for session TTL and expiration in Vertex AI session service
PiperOrigin-RevId: 917111472
1 parent 1284493 commit 3ab365e

7 files changed

Lines changed: 59 additions & 7 deletions

File tree

src/google/adk/cli/cli_deploy.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
171171
'user_id': {'type': 'string'},
172172
'session_id': {'type': 'string', 'nullable': True},
173173
'state': {'type': 'object', 'nullable': True},
174+
'ttl': {'type': 'string', 'nullable': True},
175+
'expire_time': {'type': 'string', 'nullable': True},
174176
},
175177
'required': ['user_id'],
176178
'type': 'object',
@@ -239,19 +241,24 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
239241
'Creates a new session.\n\n Args:\n user_id'
240242
' (str):\n Required. The ID of the user.\n '
241243
' session_id (str):\n Optional. The ID of the'
242-
' session. If not provided, an ID\n will be be'
244+
' session. If not provided, an ID\n will be'
243245
' generated for the session.\n state (dict[str, Any]):\n'
244246
' Optional. The initial state of the session.\n '
245-
' **kwargs (dict[str, Any]):\n Optional.'
246-
' Additional keyword arguments to pass to the\n '
247-
' session service.\n\n Returns:\n Session: The'
248-
' newly created session instance.\n '
247+
' ttl (str):\n Optional. The time-to-live for'
248+
' the session.\n expire_time (str):\n '
249+
' Optional. The expiration time for the session.\n '
250+
' **kwargs (dict[str, Any]):\n Optional. Additional'
251+
' keyword arguments to pass to the\n session'
252+
' service.\n\n Returns:\n Session: The newly'
253+
' created session instance.\n '
249254
),
250255
'parameters': {
251256
'properties': {
252257
'user_id': {'type': 'string'},
253258
'session_id': {'type': 'string', 'nullable': True},
254259
'state': {'type': 'object', 'nullable': True},
260+
'ttl': {'type': 'string', 'nullable': True},
261+
'expire_time': {'type': 'string', 'nullable': True},
255262
},
256263
'required': ['user_id'],
257264
'type': 'object',

src/google/adk/sessions/base_session_service.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ async def create_session(
6565
user_id: str,
6666
state: Optional[dict[str, Any]] = None,
6767
session_id: Optional[str] = None,
68+
**kwargs: Any,
6869
) -> Session:
6970
"""Creates a new session.
7071
@@ -74,6 +75,8 @@ async def create_session(
7475
state: the initial state of the session.
7576
session_id: the client-provided id of the session. If not provided, a
7677
generated ID will be used.
78+
**kwargs: Additional keyword arguments to pass to the session creation,
79+
such as 'ttl' or 'expire_time'. Supported by some implementations.
7780
7881
Returns:
7982
session: The newly created session instance.

src/google/adk/sessions/database_session_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ async def create_session(
416416
user_id: str,
417417
state: Optional[dict[str, Any]] = None,
418418
session_id: Optional[str] = None,
419+
**kwargs: Any,
419420
) -> Session:
420421
# 1. Populate states.
421422
# 2. Build storage session object

src/google/adk/sessions/in_memory_session_service.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ async def create_session(
8282
user_id: str,
8383
state: Optional[dict[str, Any]] = None,
8484
session_id: Optional[str] = None,
85+
**kwargs: Any,
8586
) -> Session:
8687
return self._create_session_impl(
8788
app_name=app_name,
8889
user_id=user_id,
8990
state=state,
9091
session_id=session_id,
92+
**kwargs,
9193
)
9294

9395
def create_session_sync(
@@ -97,13 +99,15 @@ def create_session_sync(
9799
user_id: str,
98100
state: Optional[dict[str, Any]] = None,
99101
session_id: Optional[str] = None,
102+
**kwargs: Any,
100103
) -> Session:
101104
logger.warning('Deprecated. Please migrate to the async method.')
102105
return self._create_session_impl(
103106
app_name=app_name,
104107
user_id=user_id,
105108
state=state,
106109
session_id=session_id,
110+
**kwargs,
107111
)
108112

109113
def _create_session_impl(
@@ -113,6 +117,7 @@ def _create_session_impl(
113117
user_id: str,
114118
state: Optional[dict[str, Any]] = None,
115119
session_id: Optional[str] = None,
120+
**kwargs: Any,
116121
) -> Session:
117122
if session_id and self._get_session_impl(
118123
app_name=app_name, user_id=user_id, session_id=session_id

src/google/adk/sessions/sqlite_session_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ async def create_session(
161161
user_id: str,
162162
state: Optional[dict[str, Any]] = None,
163163
session_id: Optional[str] = None,
164+
**kwargs: Any,
164165
) -> Session:
165166
if session_id:
166167
session_id = session_id.strip()

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,19 @@ async def create_session(
117117
state: The initial state of the session.
118118
session_id: The ID of the session.
119119
**kwargs: Additional arguments to pass to the session creation. E.g. set
120+
ttl='7200s' to set the session time-to-live or
120121
expire_time='2025-10-01T00:00:00Z' to set the session expiration time.
121-
See https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1beta1/projects.locations.reasoningEngines.sessions
122-
for more details.
122+
See
123+
https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1beta1/projects.locations.reasoningEngines.sessions
124+
for more details.
125+
123126
Returns:
124127
The created session.
125128
"""
129+
if 'ttl' in kwargs and 'expire_time' in kwargs:
130+
raise ValueError(
131+
"Cannot specify both 'ttl' and 'expire_time' simultaneously."
132+
)
126133
reasoning_engine_id = self._get_reasoning_engine_id(app_name)
127134

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

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,34 @@ async def test_create_session_with_custom_config(mock_api_client_instance):
967967
)
968968

969969

970+
@pytest.mark.asyncio
971+
@pytest.mark.usefixtures('mock_get_api_client')
972+
async def test_create_session_with_ttl(mock_api_client_instance):
973+
session_service = mock_vertex_ai_session_service()
974+
975+
ttl = '7200s'
976+
await session_service.create_session(app_name='123', user_id='user', ttl=ttl)
977+
assert mock_api_client_instance.last_create_session_config['ttl'] == ttl
978+
979+
980+
@pytest.mark.asyncio
981+
@pytest.mark.usefixtures('mock_get_api_client')
982+
async def test_create_session_with_ttl_and_expire_time_raises_value_error(
983+
mock_api_client_instance,
984+
):
985+
session_service = mock_vertex_ai_session_service()
986+
with pytest.raises(
987+
ValueError,
988+
match="Cannot specify both 'ttl' and 'expire_time' simultaneously.",
989+
):
990+
await session_service.create_session(
991+
app_name='123',
992+
user_id='user',
993+
ttl='7200s',
994+
expire_time='2025-12-12T12:12:12.123456Z',
995+
)
996+
997+
970998
@pytest.mark.asyncio
971999
@pytest.mark.usefixtures('mock_get_api_client')
9721000
async def test_append_event():

0 commit comments

Comments
 (0)