Skip to content

Commit 31f0d81

Browse files
committed
fix: handle missing agent name in readonly context
1 parent abcaa08 commit 31f0d81

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

src/google/adk/agents/readonly_context.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def invocation_id(self) -> str:
4949
@property
5050
def agent_name(self) -> str:
5151
"""The name of the agent that is currently running."""
52+
if self._invocation_context.agent is None:
53+
return "unknown"
5254
return self._invocation_context.agent.name
5355

5456
@property

tests/unittests/agents/test_readonly_context.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def test_agent_name(mock_invocation_context):
3939
assert readonly_context.agent_name == "test-agent-name"
4040

4141

42+
def test_agent_name_without_agent(mock_invocation_context):
43+
mock_invocation_context.agent = None
44+
readonly_context = ReadonlyContext(mock_invocation_context)
45+
assert readonly_context.agent_name == "unknown"
46+
47+
4248
def test_state_content(mock_invocation_context):
4349
readonly_context = ReadonlyContext(mock_invocation_context)
4450
state = readonly_context.state

0 commit comments

Comments
 (0)