Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agentplatform/agent_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ def set_up(self):
artifact_service=self._tmpl_attrs.get("artifact_service"),
memory_service=self._tmpl_attrs.get("memory_service"),
credential_service=self._tmpl_attrs.get("credential_service"),
auto_create_session=True,
)
self._tmpl_attrs["in_memory_session_service"] = InMemorySessionService()
self._tmpl_attrs["in_memory_artifact_service"] = InMemoryArtifactService()
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/agentplatform/frameworks/test_frameworks_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,67 @@ async def test_async_stream_query(
events.append(event)
assert len(events) == 1

def test_set_up_runner_auto_create_session_enabled(
self,
default_instrumentor_builder_mock: mock.Mock,
get_project_id_mock: mock.Mock,
):
"""The main runner opts into auto-creating missing sessions (b/537477760)."""
app = adk_template.AdkApp(agent=_TEST_AGENT)
app.set_up()
assert app._tmpl_attrs.get("runner").auto_create_session is True

@pytest.mark.asyncio
async def test_runner_auto_creates_missing_session(
self,
default_instrumentor_builder_mock: mock.Mock,
get_project_id_mock: mock.Mock,
):
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import (
InMemorySessionService,
)

app = adk_template.AdkApp(agent=_TEST_AGENT)
app.set_up()
assert app._tmpl_attrs.get("runner").auto_create_session is True

app_name = app._tmpl_attrs.get("app_name")
session_service = InMemorySessionService()
runner = Runner(
agent=_TEST_AGENT,
app_name=app_name,
session_service=session_service,
auto_create_session=app._tmpl_attrs.get("runner").auto_create_session,
)
missing_session_id = "0000000000000000000"

# Sanity: the session does not exist yet.
assert (
await session_service.get_session(
app_name=app_name,
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)
is None
)

session = await runner._get_or_create_session(
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)

assert session is not None
assert session.id == missing_session_id
assert (
await session_service.get_session(
app_name=app_name,
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)
is not None
)

@pytest.mark.asyncio
async def test_async_stream_query_with_empty_session_events(
self,
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/vertex_adk/test_agent_engine_templates_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,67 @@ async def test_async_stream_query(
events.append(event)
assert len(events) == 1

def test_set_up_runner_auto_create_session_enabled(
self,
default_instrumentor_builder_mock: mock.Mock,
get_project_id_mock: mock.Mock,
):
"""The main runner opts into auto-creating missing sessions (b/537477760)."""
app = agent_engines.AdkApp(agent=_TEST_AGENT)
app.set_up()
assert app._tmpl_attrs.get("runner").auto_create_session is True

@pytest.mark.asyncio
async def test_runner_auto_creates_missing_session(
self,
default_instrumentor_builder_mock: mock.Mock,
get_project_id_mock: mock.Mock,
):
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import (
InMemorySessionService,
)

app = agent_engines.AdkApp(agent=_TEST_AGENT)
app.set_up()
assert app._tmpl_attrs.get("runner").auto_create_session is True

app_name = app._tmpl_attrs.get("app_name")
session_service = InMemorySessionService()
runner = Runner(
agent=_TEST_AGENT,
app_name=app_name,
session_service=session_service,
auto_create_session=app._tmpl_attrs.get("runner").auto_create_session,
)
missing_session_id = "0000000000000000000"

# Sanity: the session does not exist yet.
assert (
await session_service.get_session(
app_name=app_name,
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)
is None
)

session = await runner._get_or_create_session(
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)

assert session is not None
assert session.id == missing_session_id
assert (
await session_service.get_session(
app_name=app_name,
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)
is not None
)

@pytest.mark.asyncio
@mock.patch.dict(
os.environ,
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/vertex_adk/test_reasoning_engine_templates_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,63 @@ async def test_async_stream_query(self):
events.append(event)
assert len(events) == 1

def test_set_up_runner_auto_create_session_enabled(self):
"""The main runner opts into auto-creating missing sessions (b/537477760)."""
app = reasoning_engines.AdkApp(
agent=Agent(name=_TEST_AGENT_NAME, model=_TEST_MODEL)
)
app.set_up()
assert app._tmpl_attrs.get("runner").auto_create_session is True

@pytest.mark.asyncio
async def test_runner_auto_creates_missing_session(self):
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import (
InMemorySessionService,
)

app = reasoning_engines.AdkApp(
agent=Agent(name=_TEST_AGENT_NAME, model=_TEST_MODEL)
)
app.set_up()
assert app._tmpl_attrs.get("runner").auto_create_session is True

app_name = app._tmpl_attrs.get("app_name")
session_service = InMemorySessionService()
runner = Runner(
agent=Agent(name=_TEST_AGENT_NAME, model=_TEST_MODEL),
app_name=app_name,
session_service=session_service,
auto_create_session=app._tmpl_attrs.get("runner").auto_create_session,
)
missing_session_id = "0000000000000000000"

# Sanity: the session does not exist yet.
assert (
await session_service.get_session(
app_name=app_name,
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)
is None
)

session = await runner._get_or_create_session(
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)

assert session is not None
assert session.id == missing_session_id
assert (
await session_service.get_session(
app_name=app_name,
user_id=_TEST_USER_ID,
session_id=missing_session_id,
)
is not None
)

@pytest.mark.asyncio
async def test_async_stream_query_with_empty_session_events(self):
app = reasoning_engines.AdkApp(
Expand Down
1 change: 1 addition & 0 deletions vertexai/agent_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ def set_up(self):
session_service=self._tmpl_attrs.get("session_service"),
artifact_service=self._tmpl_attrs.get("artifact_service"),
memory_service=self._tmpl_attrs.get("memory_service"),
auto_create_session=True,
)
self._tmpl_attrs["in_memory_session_service"] = InMemorySessionService()
self._tmpl_attrs["in_memory_artifact_service"] = InMemoryArtifactService()
Expand Down
1 change: 1 addition & 0 deletions vertexai/preview/reasoning_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ def set_up(self):
artifact_service=self._tmpl_attrs.get("artifact_service"),
memory_service=self._tmpl_attrs.get("memory_service"),
app_name=self._tmpl_attrs.get("app_name"),
auto_create_session=True,
)
self._tmpl_attrs["in_memory_session_service"] = InMemorySessionService()
self._tmpl_attrs["in_memory_artifact_service"] = InMemoryArtifactService()
Expand Down
Loading