Replies: 6 comments 6 replies
-
What benefit does it have on Agent given that (AFAICT) it's specific to AgentState? I'm curious where else we expect this to be used. Everywhere else we do serialization, it's the SDK driving it and thus we don't need control over the serialization. |
Beta Was this translation helpful? Give feedback.
-
This seems contrary to our tenet:
For that reason, I would lean towards one or the other but not both |
Beta Was this translation helpful? Give feedback.
-
|
I think there's a larger item here as to what However, this item:
repurposes Side note: I think Strands is lacking here and we need an |
Beta Was this translation helpful? Give feedback.
-
Do people want/need these other "non-serializable" objects to persist? Is that part of the problem? Given the problem definition, it can just be solved using |
Beta Was this translation helpful? Give feedback.
-
is this safe? client objects will likely have tokens/creds inside, we might want to put a warning in docs for it |
Beta Was this translation helpful? Give feedback.
-
This to me like the most straightforward approach since we already have the pattern. I feel like we can do this in two steps
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
AgentState Expansion
Executive Summary
persistparameter for runtime-only state (Option 1). Default JSON serializer maintains backward compatibility; optional PickleSerializer enables full Python object support.persistparameter) for immediate implementation.Current State of the world:
Strands offers an
AgentStateprimitive that provides key-value storage for stateful information that exists outside of the conversation context. It can be accessed and modified by tools and application logic and is saved to session when using a session manager.Here’s an example of how developers use
AgentState:Problem:
Currently, Strands agents require
agent.stateto be JSON-serializable, limiting it to basic Python types (str,int,dict,list, etc.). This prevents users from storing:datetime,Decimal,UUIDExamples:
**In addition **to the above issue of requiring JSON-serializable objects, there is another problem in that users want to be able to store Runtime-Only Resources that cannot and are not meant to be serialized. Such as:
sqlite3.Connection,psycopg2.connection)boto3.client,httpx.Client)These represent runtime resources, not data. They cannot be meaningfully serialized because they contain system handles that are only valid in the current process.
Here’s an example of how a developer would like to access a database connection in their tool which does not currently work as they cannot store the db connection in their
AgentStateUsers want to store a broader set of objects in the
AgentStateas we can see requested here: #1245 Therefore, in this document we outline an approach on how to support a larger diverse set of objects to be stored inAgentStateProposed Solution:
To solve the above two problems, our goal is to offer pluggable serializers. Such as a simple (default) JSON serializer that can only serialize JSON serializable objects or a Pickle serializer that can serialize (almost) all python objects.
However, in addition to the Pluggable serializers proposed we will also need to support runtime-only objects. So
AgentStatewould always need to support both of the following types of state:Serializer Configuration Options:
Option 1: Serializer on AgentState and Agent level (recommended)
Pros: Maximum flexibility, configurable at both levels.
Cons: Two ways to do same thing (though one is just delegation), another parameter in the Agent class (this can be offset as it can be potentially added as part of the
agent_configthe team is thinking about in the future.)Note: The Agent constructor will raise an error when there is a conflict conflict:
You can skip reading the below additional options if you agree with the above recommended option
Option 2: Serializer on SessionManager level
Rationale: SessionManager owns persistence → owns serialization format
Pros: Clean separation, SessionManager already handles storage
Cons:
Option 3: Serializer on Agent level
Pros:
Cons:
SessionManager calls
agent.state_serializer.serialize()during persistence.Option 4: Serializer on AgentState level
Pros:
JSONSerializableDict) - this is extending itCons:
agent.state.serializervs passing in to theAgenton initialization)Option 5: Both, Serializer on AgentState and Agent level
Pros: Maximum flexibility, discoverable at both levels
Cons: Two ways to do same thing
Options skipping is done here. Continue reading below
Runtime State Configuration Options:
Option 1: Transient Flag with
persistParameter (recommended)Usage Examples
Pros
persist=Truemaintains current behaviorget()method for all valuesCons
_transient_keyssetOption 2: Separate Runtime Methods
Implementation
Usage Examples
Pros
Cons
getmethod to useAppendix:
DX:
Basic Usage with Default JSON Serializer
Using PickleSerializer for Rich Python Types
Transient State (Runtime-Only Values)
Custom Serializers
Beta Was this translation helpful? Give feedback.
All reactions