Skip to content

Commit a2af300

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: add support for session TTL and expiration in Vertex AI session service
PiperOrigin-RevId: 936180164
1 parent 97ee94a commit a2af300

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

agentplatform/agent_engines/templates/adk.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,10 @@ async def async_create_session(
16161616
will be be generated for the session.
16171617
state (dict[str, Any]):
16181618
Optional. The initial state of the session.
1619+
ttl (str):
1620+
Optional. The time-to-live for the session.
1621+
expire_time (str):
1622+
Optional. The expiration time for the session.
16191623
**kwargs (dict[str, Any]):
16201624
Optional. Additional keyword arguments to pass to the
16211625
session service.

tests/unit/agentplatform/frameworks/test_frameworks_adk.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from google.genai import types
4343
import pytest
44+
from google.adk.sessions.base_session_service import BaseSessionService
4445

4546

4647
try:
@@ -701,6 +702,50 @@ async def test_async_create_session(self, get_project_id_mock: mock.Mock):
701702
assert session2["user_id"] == _TEST_USER_ID
702703
assert session2["id"] == "test_session_id"
703704

705+
@pytest.mark.asyncio
706+
async def test_async_create_session_with_ttl_kwargs(
707+
self, get_project_id_mock: mock.Mock
708+
):
709+
mock_session_service = mock.Mock(spec=BaseSessionService)
710+
mock_session_service.create_session = mock.AsyncMock()
711+
app = adk_template.AdkApp(
712+
agent=_TEST_AGENT,
713+
session_service_builder=lambda: mock_session_service,
714+
)
715+
await app.async_create_session(
716+
user_id=_TEST_USER_ID,
717+
ttl="7200s",
718+
)
719+
mock_session_service.create_session.assert_called_once_with(
720+
app_name=app._app_name(),
721+
user_id=_TEST_USER_ID,
722+
session_id=None,
723+
state=None,
724+
ttl="7200s",
725+
)
726+
727+
@pytest.mark.asyncio
728+
async def test_async_create_session_with_expire_time_kwargs(
729+
self, get_project_id_mock: mock.Mock
730+
):
731+
mock_session_service = mock.Mock(spec=BaseSessionService)
732+
mock_session_service.create_session = mock.AsyncMock()
733+
app = adk_template.AdkApp(
734+
agent=_TEST_AGENT,
735+
session_service_builder=lambda: mock_session_service,
736+
)
737+
await app.async_create_session(
738+
user_id=_TEST_USER_ID,
739+
expire_time="2026-03-01T00:00:00Z",
740+
)
741+
mock_session_service.create_session.assert_called_once_with(
742+
app_name=app._app_name(),
743+
user_id=_TEST_USER_ID,
744+
session_id=None,
745+
state=None,
746+
expire_time="2026-03-01T00:00:00Z",
747+
)
748+
704749
@pytest.mark.asyncio
705750
async def test_async_get_session(self, get_project_id_mock: mock.Mock):
706751
app = adk_template.AdkApp(agent=_TEST_AGENT)

tests/unit/vertex_adk/test_agent_engine_templates_adk.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from vertexai.agent_engines.templates import adk as adk_template
4141
from google.genai import types
4242
import pytest
43+
from google.adk.sessions.base_session_service import BaseSessionService
4344

4445

4546
try:
@@ -620,6 +621,51 @@ async def test_async_create_session(self, get_project_id_mock: mock.Mock):
620621
assert session2["user_id"] == _TEST_USER_ID
621622
assert session2["id"] == "test_session_id"
622623

624+
@pytest.mark.asyncio
625+
async def test_async_create_session_with_ttl_kwargs(
626+
self, get_project_id_mock: mock.Mock
627+
):
628+
mock_session_service = mock.Mock(spec=BaseSessionService)
629+
mock_session_service.create_session = mock.AsyncMock()
630+
app = agent_engines.AdkApp(
631+
agent=_TEST_AGENT,
632+
session_service_builder=lambda: mock_session_service,
633+
)
634+
await app.async_create_session(
635+
user_id=_TEST_USER_ID,
636+
ttl="7200s",
637+
)
638+
mock_session_service.create_session.assert_called_once_with(
639+
app_name=app._app_name(),
640+
user_id=_TEST_USER_ID,
641+
session_id=None,
642+
state=None,
643+
ttl="7200s",
644+
)
645+
646+
@pytest.mark.asyncio
647+
async def test_async_create_session_with_expire_time_kwargs(
648+
self, get_project_id_mock: mock.Mock
649+
):
650+
mock_session_service = mock.Mock(spec=BaseSessionService)
651+
mock_session_service.create_session = mock.AsyncMock()
652+
app = agent_engines.AdkApp(
653+
agent=_TEST_AGENT,
654+
session_service_builder=lambda: mock_session_service,
655+
)
656+
await app.async_create_session(
657+
user_id=_TEST_USER_ID,
658+
expire_time="2026-03-01T00:00:00Z",
659+
)
660+
mock_session_service.create_session.assert_called_once_with(
661+
app_name=app._app_name(),
662+
user_id=_TEST_USER_ID,
663+
session_id=None,
664+
state=None,
665+
expire_time="2026-03-01T00:00:00Z",
666+
)
667+
668+
623669
@pytest.mark.asyncio
624670
async def test_async_get_session(self, get_project_id_mock: mock.Mock):
625671
app = agent_engines.AdkApp(agent=_TEST_AGENT)

vertexai/agent_engines/templates/adk.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,10 @@ async def async_create_session(
15761576
will be be generated for the session.
15771577
state (dict[str, Any]):
15781578
Optional. The initial state of the session.
1579+
ttl (str):
1580+
Optional. The time-to-live for the session.
1581+
expire_time (str):
1582+
Optional. The expiration time for the session.
15791583
**kwargs (dict[str, Any]):
15801584
Optional. Additional keyword arguments to pass to the
15811585
session service.

0 commit comments

Comments
 (0)