From 97c4b0e03e61498e49d86deeaa490aa9ea0079d2 Mon Sep 17 00:00:00 2001 From: Mohit Sharma Date: Mon, 2 Nov 2026 14:01:40 +0530 Subject: [PATCH] fix(runner): raise ValueError in run_async when new_message and invocation_id are None (#7934) --- src/google/adk/runners.py | 7 +++++ tests/unittests/test_runners.py | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/google/adk/runners.py b/src/google/adk/runners.py index d935e7718fc..a79f0b85f2d 100644 --- a/src/google/adk/runners.py +++ b/src/google/adk/runners.py @@ -1179,6 +1179,13 @@ async def run_async( if new_message and new_message.role != 'user': new_message = types.Content(role='user', parts=new_message.parts) + if not invocation_id and new_message is None: + raise ValueError( + 'Running an agent requires either a new_message or an ' + 'invocation_id to resume a previous invocation. ' + f'Session: {session_id}, User: {user_id}' + ) + from .agents.llm_agent import LlmAgent from .workflow._base_node import BaseNode diff --git a/tests/unittests/test_runners.py b/tests/unittests/test_runners.py index d4be5005cf9..a2f4787c26f 100644 --- a/tests/unittests/test_runners.py +++ b/tests/unittests/test_runners.py @@ -7414,5 +7414,53 @@ async def _consume(): assert not [e for e in session.events if e.actions.compaction] +async def test_run_async_llm_agent_without_message_or_invocation_id_raises_value_error(): + """Runner.run_async must raise ValueError when both new_message and invocation_id are None (#7934).""" + session_service = InMemorySessionService() + runner = Runner( + app_name="test_app", + agent=MockLlmAgent("test_agent"), + session_service=session_service, + auto_create_session=True, + ) + with pytest.raises( + ValueError, + match=r"Running an agent requires either a new_message or an invocation_id", + ): + agen = runner.run_async( + user_id="u", + session_id="s", + new_message=None, + invocation_id=None, + ) + async for _ in agen: + pass + + +async def test_run_async_base_node_without_message_or_invocation_id_raises_value_error(): + """Runner.run_async with BaseNode root must raise ValueError when both new_message and invocation_id are None (#7934).""" + from google.adk.workflow._base_node import BaseNode + + session_service = InMemorySessionService() + node = BaseNode(name="test_node") + runner = Runner( + app=App(name="test_app", root_agent=node), + session_service=session_service, + auto_create_session=True, + ) + with pytest.raises( + ValueError, + match=r"Running an agent requires either a new_message or an invocation_id", + ): + agen = runner.run_async( + user_id="u", + session_id="s", + new_message=None, + invocation_id=None, + ) + async for _ in agen: + pass + + if __name__ == "__main__": pytest.main([__file__])