@@ -80,15 +80,35 @@ def _validate_schema(schema: dict[str, Any]) -> None:
80
80
81
81
class State :
82
82
"""
83
- A class that wraps a StateSchema and maintains an internal _data dictionary .
83
+ State is a container for storing shared information during the execution of an Agent and its tools .
84
84
85
- Each schema entry has:
85
+ For instance, State can be used to store documents, context, and intermediate results.
86
+
87
+ Internally it wraps a `_data` dictionary defined by a `schema`. Each schema entry has:
86
88
```json
87
89
"parameter_name": {
88
- "type": SomeType,
89
- "handler": Optional[Callable[[Any, Any], Any]]
90
+ "type": SomeType, # expected type
91
+ "handler": Optional[Callable[[Any, Any], Any]] # merge/update function
90
92
}
91
93
```
94
+
95
+ Handlers control how values are merged when using the `set()` method:
96
+ - For list types: defaults to `merge_lists` (concatenates lists)
97
+ - For other types: defaults to `replace_values` (overwrites existing value)
98
+
99
+ A `messages` field with type `list[ChatMessage]` is automatically added to the schema.
100
+
101
+ This makes it possible for the Agent to read from and write to the same context.
102
+
103
+ ### Usage example
104
+ ```python
105
+ from haystack.components.agents.state import State
106
+
107
+ my_state = State(
108
+ schema={"gh_repo_name": {"type": str}, "user_name": {"type": str}},
109
+ data={"gh_repo_name": "my_repo", "user_name": "my_user_name"}
110
+ )
111
+ ```
92
112
"""
93
113
94
114
def __init__ (self , schema : dict [str , Any ], data : Optional [dict [str , Any ]] = None ):
0 commit comments