Skip to content

Commit 77ed1f5

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Add setdefault method to the ADK State object
This makes the ADK state more pythonic, with the State API being more like a python dict. PiperOrigin-RevId: 796767559
1 parent 4afc9b2 commit 77ed1f5

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/google/adk/sessions/state.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ def __contains__(self, key: str) -> bool:
4848
"""Whether the state dict contains the given key."""
4949
return key in self._value or key in self._delta
5050

51+
def setdefault(self, key: str, default: Any = None) -> Any:
52+
"""Gets the value of a key, or sets it to a default if the key doesn't exist."""
53+
if key in self:
54+
return self[key]
55+
else:
56+
self[key] = default
57+
return default
58+
5159
def has_delta(self) -> bool:
5260
"""Whether the state has pending delta."""
5361
return bool(self._delta)

0 commit comments

Comments
 (0)