Skip to content

Commit 0fe2f8e

Browse files
Docs: clarify purpose of State class (closes #9738) (#9746)
Co-authored-by: anakin87 <[email protected]>
1 parent 4644785 commit 0fe2f8e

File tree

1 file changed

+24
-4
lines changed
  • haystack/components/agents/state

1 file changed

+24
-4
lines changed

haystack/components/agents/state/state.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,35 @@ def _validate_schema(schema: dict[str, Any]) -> None:
8080

8181
class State:
8282
"""
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.
8484
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:
8688
```json
8789
"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
9092
}
9193
```
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+
```
92112
"""
93113

94114
def __init__(self, schema: dict[str, Any], data: Optional[dict[str, Any]] = None):

0 commit comments

Comments
 (0)