adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Support for Immutable Session State Keys

Open triangle-z opened this issue 1 year ago • 4 comments

I have a case where some state keys shouldn't be modified by the agent. Similar keys are in the documentation such as user:login_count which should ideally only be modified by the code itself to ensure that it value is correct. Currently, the session state is fully mutable, which creates risk (low but still not 0) for accidental overwrites, inconsistencies in workflows, and unintended side effects.


I know that this is against the initial purpose of the state, which is to be the agent's scratchpad, but it might be helpful to add support for immutability in session state keys.

Potential implementation options:

  1. StateValue object
    A StateValue class with an optional mutable flag. If a raw value is assigned, it defaults to mutable=True.

     state_changes = {
         "task_status": "active",   # Equivalent to StateValue(session.state.get("user:login_count", 0) + 1), default mutable=True
         "user:login_count": StateValue(session.state.get("user:login_count", 0) + 1, mutable=False), # Update user state
     }
    
  2. Prefix convention
    Use a reserved prefix like immutable: for the read-only keys.

    session.state["immutable:booking_id"] = "12345"
    
  3. Immutable key list
    Support a config-level list (or method) of keys that should not be modified after initial assignment.

    IMMUTABLE_KEYS = ["user:login_count", "booking_id"]
    

There are of course some alternatives if this is not a good idea :

  • Enforcing immutability in the agent logic, but this is error-prone and scattered across the codebase.
  • Using before callbacks to manually prevent modification of certain keys, but this leads to spaghetti code and lacks a centralized mechanism.
  • Fetching data directly from another source in the tools that need it (e.g., database, in memory ...) instead of storing it in state, but this also leads to spaghetti code and decouples business logic from conversational context.

If this aligns with the project, I'm happy to contribute a pull request and also update the documentation.

triangle-z avatar May 02 '25 14:05 triangle-z

I've implemented a proposed solution based on the StateValue wrapper and committed it here: triangle-z/adk-python@6a4bc27727834c336746047dec585008c60d220e. If you believe this approach is viable, I’d appreciate your input on a few questions:

  1. Silent failure vs. exception: When the LLM (or a user) tries to modify an immutable key, I currently prevent the update and just print a warning, but I don’t raise an exception. I did this because currently, the LLM is not aware of which keys are immutable, so raising would cause unnecessary exceptions. → Is this the preferred behavior, or should an error be raised explicitly and handled somewhere else ?

  2. Should the LLM be aware of immutability? I’ve kept immutability enforcement internal and did not expose it to the agent logic to avoid changing the agent's behavior based on this constraint. But there might be cases where knowing a key is immutable could help guide the LLM's logic. → Should immutability metadata be exposed, or should it be kept internal ?

  3. Mutability resolution when both _value and _delta exist: There is a rare case where a key is first set as immutable and then redefined as mutable (or vice versa). Currently, I let the value in _delta take precedence. → Is that the correct and intended precedence rule?

  4. Safety checks vs. redundancy: I’ve added checks like v.value if isinstance(v, StateValue) else v throughout the code to be safe. Technically, __setitem__ ensures all values are wrapped, but these checks offer an extra layer of robustness. → Do you prefer keeping these safer patterns, or would you rather skip the isinstance checks for clarity?

Please let me know if I’ve missed any other consideration or if you would recommend adjustments to the current direction. I'm happy to refine the solution or expand on it further. I didn't open a PR yet since I don't know if this is the wanted direction.

triangle-z avatar May 03 '25 14:05 triangle-z

By immutable, do you mean that once the state is set, no one can mutate it? Or do you mean only a specific code piece can modify it? If so, how do you identify which piece of code has the right to mutate? Thanks!

boyangsvl avatar May 08 '25 06:05 boyangsvl

Hi @boyangsvl , thanks for the reply. By immutable I mean that it's a state that the agents can access but not modify. The app could modify it explicitely via tools or callbacks.

triangle-z avatar May 08 '25 08:05 triangle-z

Hi @triangle-z and thanks to everyone who participated in this thread.

We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.

If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.

Thanks for your contribution and understanding!

surajksharma07 avatar Oct 07 '25 07:10 surajksharma07