Skip to content

Commit 47f43dd

Browse files
committed
fix(runners): widen Runner.__init__ agent param to accept BaseNode
The `Runner.agent` class field is typed `Optional[BaseAgent | 'BaseNode']` and the runtime explicitly branches on `isinstance(self.agent, BaseNode)` in both `run_async` and `run_live`, so a `BaseNode` root (e.g. a `Workflow`) works at runtime. However, the `__init__` `agent` parameter was still typed `Optional[BaseAgent]`, so type checkers reject `Runner(agent=my_workflow)`. Widen the `__init__` `agent` annotation to `Optional[BaseAgent | 'BaseNode']` to match the class field, mirroring the existing forward-reference style used for the field and the `run_live` `node` parameter. This is a type-only change with no runtime behavior change. Fixes #6270
1 parent 2e878ed commit 47f43dd

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/google/adk/runners.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def __init__(
239239
*,
240240
app: Optional[App] = None,
241241
app_name: Optional[str] = None,
242-
agent: Optional[BaseAgent] = None,
242+
agent: Optional[BaseAgent | BaseNode] = None,
243243
node: BaseNode | None = None,
244244
plugins: Optional[List[BasePlugin]] = None,
245245
artifact_service: Optional[BaseArtifactService] = None,
@@ -320,7 +320,7 @@ def _require_root_agent(self) -> BaseAgent:
320320
def _resolve_app(
321321
app: Optional[App],
322322
app_name: Optional[str],
323-
agent: Optional[BaseAgent],
323+
agent: Optional[BaseAgent | BaseNode],
324324
node: BaseNode | None,
325325
plugins: Optional[List[BasePlugin]],
326326
) -> App:

tests/unittests/test_runners.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,22 @@ def test_resolve_app_with_agent_wraps_in_app(self):
17771777
assert runner.app_name == "test_app"
17781778
assert runner.agent is self.root_agent
17791779

1780+
def test_resolve_app_with_base_node_via_agent_param(self):
1781+
"""Test that a BaseNode root passed via `agent` is accepted."""
1782+
from google.adk.workflow._base_node import BaseNode
1783+
1784+
node = BaseNode(name="test_node")
1785+
runner = Runner(
1786+
app_name="test_app",
1787+
agent=node,
1788+
session_service=self.session_service,
1789+
artifact_service=self.artifact_service,
1790+
)
1791+
assert runner.app is not None
1792+
assert runner.app.root_agent is node
1793+
assert runner.app_name == "test_app"
1794+
assert runner.agent is node
1795+
17801796
def test_resolve_app_with_node_wraps_in_app(self):
17811797
"""Test that a bare node is wrapped into an App."""
17821798
from google.adk.workflow._base_node import BaseNode

0 commit comments

Comments
 (0)