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
This commit is contained in:
Google Team Member
2025-08-19 01:45:33 -07:00
committed by Copybara-Service
parent 4afc9b2f33
commit 77ed1f5f15
+8
View File
@@ -48,6 +48,14 @@ class State:
"""Whether the state dict contains the given key.""" """Whether the state dict contains the given key."""
return key in self._value or key in self._delta return key in self._value or key in self._delta
def setdefault(self, key: str, default: Any = None) -> Any:
"""Gets the value of a key, or sets it to a default if the key doesn't exist."""
if key in self:
return self[key]
else:
self[key] = default
return default
def has_delta(self) -> bool: def has_delta(self) -> bool:
"""Whether the state has pending delta.""" """Whether the state has pending delta."""
return bool(self._delta) return bool(self._delta)