teff.state.state¶
teff.state.state
¶
Typed state with per-key reducers for graph workflows.
Classes:
| Name | Description |
|---|---|
State |
Typed workflow state that applies per-key reducers on merge. |
Functions:
| Name | Description |
|---|---|
apply_reducers |
Merge new_values into state using the provided per-key reducers. |
reducer_appends |
True when reducer accumulates list contributions (append semantics). |
reducers_from_typeddict |
Extract per-key reducers from a TypedDict's |
reducers_from_yaml_schema |
Convert a YAML state schema dict into a reducer map. |
reducers_to_yaml_schema |
Convert a reducer map back into a YAML state schema dict. |
state_schema_to_jsonschema |
Convert a YAML |
validate_state |
Validate state against a YAML |
Attributes:
| Name | Type | Description |
|---|---|---|
Reducer |
Merge strategy: |
Reducer
module-attribute
¶
Reducer = Callable[[Any, Any], Any] | str
Merge strategy: "override", "append", "keep", or a callable (old, new) -> value.
State
¶
Bases: dict
Typed workflow state that applies per-key reducers on merge.
Wraps a dict with reducers extracted from a TypedDict schema::
class MyState(TypedDict):
messages: Annotated[list, "append"]
status: str
state = State(MyState, {"status": "ok"})
state.merge({"messages": ["hello"]})
state.merge({"messages": ["world"]})
assert state["messages"] == ["hello", "world"]
Methods:
| Name | Description |
|---|---|
merge |
Merge new_values using per-key reducers. |
Attributes:
| Name | Type | Description |
|---|---|---|
reducers |
dict[str, Reducer]
|
Return this state's per-key reducers. |
Source code in teff/state/state.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
reducers
property
¶
reducers
Return this state's per-key reducers.
Exposed so nested components (e.g. parallel branches) apply the
same merge strategies as the top-level graph.run() merge.
merge
¶
merge(new_values)
Merge new_values using per-key reducers.
Source code in teff/state/state.py
236 237 238 | |
apply_reducers
¶
apply_reducers(state, new_values, reducers)
Merge new_values into state using the provided per-key reducers.
Keys without a reducer are overridden (backward-compatible default).
A callable reducer for a key that is missing from state receives no
old value — the new value is stored as-is instead of calling it
with None (so add_messages(old, new) = old + new works from a
fresh state).
Source code in teff/state/state.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
reducer_appends
¶
reducer_appends(reducer)
True when reducer accumulates list contributions (append semantics).
"append" and any callable treat a node's returned value as new items
to merge into the existing value. None / "override" / "keep"
treat it as a full replacement (or a keep-if-absent), so a node that
writes a whole value back must return that whole value under those
strategies and only its delta under append semantics.
Source code in teff/state/state.py
10 11 12 13 14 15 16 17 18 19 20 21 | |
reducers_from_typeddict
¶
reducers_from_typeddict(cls)
Extract per-key reducers from a TypedDict's Annotated metadata.
Usage::
def add_messages(old: list, new: list) -> list:
return old + new
class ChatState(TypedDict):
messages: Annotated[list[str], add_messages]
status: str
reducers = reducers_from_typeddict(ChatState) # {"messages": add_messages}
Source code in teff/state/state.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
reducers_from_yaml_schema
¶
reducers_from_yaml_schema(schema)
Convert a YAML state schema dict into a reducer map.
YAML format::
state:
schema:
messages:
reducer: append
type: list
status:
reducer: keep
Returns a dict like {"messages": "append", "status": "keep"}.
Keys without a reducer field default to "override".
Source code in teff/state/state.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
reducers_to_yaml_schema
¶
reducers_to_yaml_schema(reducers)
Convert a reducer map back into a YAML state schema dict.
Only string reducers ("override"/"append"/"keep") can be
serialised; callable reducers are skipped. Field types are unknown
after the reverse mapping, so entries carry just the reducer key::
reducers_to_yaml_schema({"messages": "append"})
# -> {"messages": {"reducer": "append"}}
Source code in teff/state/state.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
state_schema_to_jsonschema
¶
state_schema_to_jsonschema(schema)
Convert a YAML state.schema block into a JSON Schema dict.
The YAML format associates a type with each state key::
state:
schema:
status: string
count: {type: integer, minimum: 0}
tags: {type: list}
Each entry may be a plain type name (string, integer,
number, boolean, list, object, null/any) or a
dict whose type key holds the type plus any JSON Schema keywords
(minimum, items, enum, ...). Keys listed as required: true
are marked required. Unknown types are left unconstrained.
Returns:
| Type | Description |
|---|---|
dict
|
A JSON Schema with |
Source code in teff/state/state.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
validate_state
¶
validate_state(state, schema)
Validate state against a YAML state.schema dict.
Returns a list of human-readable errors (empty when state conforms).
schema is converted with :func:state_schema_to_jsonschema and
validated with :func:teff.schema.validate_json.
Source code in teff/state/state.py
205 206 207 208 209 210 211 212 213 214 | |