-
|
I need multiple sub-agents to read/write the same session state. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) diagnostic-check-ok — bot environment verified [test] |
Beta Was this translation helpful? Give feedback.
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: ADK does not provide a built-in thread-safety guarantee for session state when modified by multiple sub-agents. You should use a locking mechanism to protect shared state or ensure sub-agents write to distinct state keys. Hello! That's an excellent question about concurrency in ADK. When using a The documentation recommends that if you need to share a context between agents (as in a There are two main approaches to handle this: 1. Use Distinct State Keys (Recommended) 2. Implement Locking Here is a conceptual example: import threading
# You would create a lock and make it available to your sub-agents,
# possibly by initializing it in a shared object or passing it during setup.
state_lock = threading.Lock()
def my_sub_agent_logic(session_state):
# Acquire the lock before accessing the shared state
with state_lock:
# Safely read from and write to the shared state
current_value = session_state.get('shared_key', 0)
session_state['shared_key'] = current_value + 1By using a lock, you ensure that only one sub-agent can modify the shared key at a time. I hope this helps you build your solution! [1] https://google.github.io/adk-docs/agents/workflow-agents/parallel-agents/ |
Beta Was this translation helpful? Give feedback.
Response from ADK Answering Agent (experimental, answer may be inaccurate)
TLDR: ADK does not provide a built-in thread-safety guarantee for session state when modified by multiple sub-agents. You should use a locking mechanism to protect shared state or ensure sub-agents write to distinct state keys.
Hello! That's an excellent question about concurrency in ADK.
When using a
ParallelAgent, all sub-agents share the samesession.state. [2] If multiple sub-agents need to read and write to the same key in the session state, there is no automatic thread-safety guarantee, and you could encounter race conditions.The documentation recommends that if you need to share a context between agents (a…