pydantic_graph.persistence
SnapshotStatus
module-attribute
SnapshotStatus = Literal[
"created", "pending", "running", "success", "error"
]
The status of a snapshot.
'created'
: The snapshot has been created but not yet run.'pending'
: The snapshot has been retrieved withload_next
but not yet run.'running'
: The snapshot is currently running.'success'
: The snapshot has been run successfully.'error'
: The snapshot has been run but an error occurred.
NodeSnapshot
dataclass
Bases: Generic[StateT, RunEndT]
History step describing the execution of a node in a graph.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
state
instance-attribute
state: StateT
The state of the graph before the node is run.
node
instance-attribute
The node to run next.
start_ts
class-attribute
instance-attribute
start_ts: datetime | None = None
The timestamp when the node started running, None
until the run starts.
duration
class-attribute
instance-attribute
duration: float | None = None
The duration of the node run in seconds, if the node has been run.
status
class-attribute
instance-attribute
status: SnapshotStatus = 'created'
The status of the snapshot.
kind
class-attribute
instance-attribute
kind: Literal['node'] = 'node'
The kind of history step, can be used as a discriminator when deserializing history.
EndSnapshot
dataclass
Bases: Generic[StateT, RunEndT]
History step describing the end of a graph run.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
69 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 |
|
state
instance-attribute
state: StateT
The state of the graph at the end of the run.
ts
class-attribute
instance-attribute
The timestamp when the graph run ended.
kind
class-attribute
instance-attribute
kind: Literal['end'] = 'end'
The kind of history step, can be used as a discriminator when deserializing history.
Snapshot
module-attribute
Snapshot = Union[
NodeSnapshot[StateT, RunEndT],
EndSnapshot[StateT, RunEndT],
]
A step in the history of a graph run.
Graph.run
returns a list of these steps describing the execution of the graph,
together with the run return value.
BaseStatePersistence
Bases: ABC
, Generic[StateT, RunEndT]
Abstract base class for storing the state of a graph run.
Each instance of a BaseStatePersistence
subclass should be used for a single graph run.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
snapshot_node
abstractmethod
async
Snapshot the state of a graph, when the next step is to run a node.
This method should add a NodeSnapshot
to persistence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
StateT
|
The state of the graph. |
required |
next_node
|
BaseNode[StateT, Any, RunEndT]
|
The next node to run. |
required |
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
112 113 114 115 116 117 118 119 120 121 122 |
|
snapshot_node_if_new
abstractmethod
async
snapshot_node_if_new(
snapshot_id: str,
state: StateT,
next_node: BaseNode[StateT, Any, RunEndT],
) -> None
Snapshot the state of a graph if the snapshot ID doesn't already exist in persistence.
This method will generally call snapshot_node
but should do so in an atomic way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
snapshot_id
|
str
|
The ID of the snapshot to check. |
required |
state
|
StateT
|
The state of the graph. |
required |
next_node
|
BaseNode[StateT, Any, RunEndT]
|
The next node to run. |
required |
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
snapshot_end
abstractmethod
async
snapshot_end(state: StateT, end: End[RunEndT]) -> None
Snapshot the state of a graph when the graph has ended.
This method should add an EndSnapshot
to persistence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
StateT
|
The state of the graph. |
required |
end
|
End[RunEndT]
|
data from the end of the run. |
required |
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
140 141 142 143 144 145 146 147 148 149 150 |
|
record_run
abstractmethod
record_run(
snapshot_id: str,
) -> AbstractAsyncContextManager[None]
Record the run of the node, or error if the node is already running.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
snapshot_id
|
str
|
The ID of the snapshot to record. |
required |
Raises:
Type | Description |
---|---|
GraphNodeRunningError
|
if the node status it not |
LookupError
|
if the snapshot ID is not found in persistence. |
Returns:
Type | Description |
---|---|
AbstractAsyncContextManager[None]
|
An async context manager that records the run of the node. |
In particular this should set:
NodeSnapshot.status
to'running'
andNodeSnapshot.start_ts
when the run starts.NodeSnapshot.status
to'success'
or'error'
andNodeSnapshot.duration
when the run finishes.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
load_next
abstractmethod
async
load_next() -> NodeSnapshot[StateT, RunEndT] | None
Retrieve a node snapshot with status 'created
' and set its status to 'pending'
.
This is used by Graph.iter_from_persistence
to get the next node to run.
Returns: The snapshot, or None
if no snapshot with status 'created
' exists.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
175 176 177 178 179 180 181 182 183 184 |
|
load_all
abstractmethod
async
Load the entire history of snapshots.
load_all
is not used by pydantic-graph itself, instead it's provided to make it convenient to
get all snapshots from persistence.
Returns: The list of snapshots.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
186 187 188 189 190 191 192 193 194 195 |
|
set_graph_types
Set the types of the state and run end from a graph.
You generally won't need to customise this method, instead implement
set_types
and
should_set_types
.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
197 198 199 200 201 202 203 204 205 206 |
|
should_set_types
should_set_types() -> bool
Whether types need to be set.
Implementations should override this method to return True
when types have not been set if they are needed.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
208 209 210 211 212 213 |
|
set_types
Set the types of the state and run end.
This can be used to create type adapters for serializing and deserializing snapshots,
e.g. with build_snapshot_list_type_adapter
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_type
|
type[StateT]
|
The state type. |
required |
run_end_type
|
type[RunEndT]
|
The run end type. |
required |
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
215 216 217 218 219 220 221 222 223 224 225 |
|
build_snapshot_list_type_adapter
build_snapshot_list_type_adapter(
state_t: type[StateT], run_end_t: type[RunEndT]
) -> TypeAdapter[list[Snapshot[StateT, RunEndT]]]
Build a type adapter for a list of snapshots.
This method should be called from within
set_types
where context variables will be set such that Pydantic can create a schema for
NodeSnapshot.node
.
Source code in pydantic_graph/pydantic_graph/persistence/__init__.py
228 229 230 231 232 233 234 235 236 237 238 |
|
In memory state persistence.
This module provides simple in memory state persistence for graphs.
SimpleStatePersistence
dataclass
Bases: BaseStatePersistence[StateT, RunEndT]
Simple in memory state persistence that just hold the latest snapshot.
If no state persistence implementation is provided when running a graph, this is used by default.
Source code in pydantic_graph/pydantic_graph/persistence/in_mem.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
last_snapshot
class-attribute
instance-attribute
last_snapshot: Snapshot[StateT, RunEndT] | None = None
The last snapshot.
FullStatePersistence
dataclass
Bases: BaseStatePersistence[StateT, RunEndT]
In memory state persistence that hold a list of snapshots.
Source code in pydantic_graph/pydantic_graph/persistence/in_mem.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 |
|
deep_copy
class-attribute
instance-attribute
deep_copy: bool = True
Whether to deep copy the state and nodes when storing them.
Defaults to True
so even if nodes or state are modified after the snapshot is taken,
the persistence history will record the value at the time of the snapshot.
history
class-attribute
instance-attribute
List of snapshots taken during the graph run.
dump_json
Dump the history to JSON bytes.
Source code in pydantic_graph/pydantic_graph/persistence/in_mem.py
157 158 159 160 |
|
load_json
Load the history from JSON.
Source code in pydantic_graph/pydantic_graph/persistence/in_mem.py
162 163 164 165 |
|
FileStatePersistence
dataclass
Bases: BaseStatePersistence[StateT, RunEndT]
File based state persistence that hold graph run state in a JSON file.
Source code in pydantic_graph/pydantic_graph/persistence/file.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 98 99 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
json_file
instance-attribute
json_file: Path
Path to the JSON file where the snapshots are stored.
You should use a different file for each graph run, but a single file should be reused for multiple steps of the same run.
For example if you have a run ID of the form run_123abc
, you might create a FileStatePersistence
thus:
from pathlib import Path
from pydantic_graph import FullStatePersistence
run_id = 'run_123abc'
persistence = FullStatePersistence(Path('runs') / f'{run_id}.json')
should_set_types
should_set_types() -> bool
Whether types need to be set.
Source code in pydantic_graph/pydantic_graph/persistence/file.py
104 105 106 |
|