Agent state
Agent state schema for PyAgenity agent graphs.
This module provides the AgentState class, which tracks message context, context summaries, and internal execution metadata for agent workflows. Supports subclassing for custom application fields.
Classes:
Name | Description |
---|---|
AgentState |
Common state schema that includes messages, context and internal execution metadata. |
Attributes:
Name | Type | Description |
---|---|---|
logger |
|
Attributes¶
Classes¶
AgentState
¶
Bases: BaseModel
Common state schema that includes messages, context and internal execution metadata.
This class can be subclassed to add application-specific fields while maintaining compatibility with the PyAgenity framework. All internal execution metadata is preserved through subclassing.
Notes:
- execution_meta
contains internal-only execution progress and interrupt info.
- Users may subclass AgentState
to add application fields; internal exec meta remains
available to the runtime and will be persisted with the state.
- When subclassing, add your fields but keep the core fields intact.
Example
class MyCustomState(AgentState): user_data: dict = Field(default_factory=dict) custom_field: str = "default"
Methods:
Name | Description |
---|---|
advance_step |
Advance the execution step in the metadata. |
clear_interrupt |
Clear any interrupt in the execution metadata. |
complete |
Mark the agent state as completed. |
error |
Mark the agent state as errored. |
is_interrupted |
Check if the agent state is currently interrupted. |
is_running |
Check if the agent state is currently running. |
is_stopped_requested |
Check if a stop has been requested for the agent state. |
set_current_node |
Set the current node in the execution metadata. |
set_interrupt |
Set an interrupt in the execution metadata. |
Attributes:
Name | Type | Description |
---|---|---|
context |
Annotated[list[Message], add_messages]
|
|
context_summary |
str | None
|
|
execution_meta |
ExecutionState
|
|
Source code in pyagenity/state/agent_state.py
23 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 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 |
|
Attributes¶
execution_meta
class-attribute
instance-attribute
¶
execution_meta = Field(default_factory=lambda: ExecutionState(current_node=START))
Functions¶
advance_step
¶
advance_step()
Advance the execution step in the metadata.
Source code in pyagenity/state/agent_state.py
90 91 92 93 94 95 96 |
|
clear_interrupt
¶
clear_interrupt()
Clear any interrupt in the execution metadata.
Source code in pyagenity/state/agent_state.py
61 62 63 64 65 66 |
|
complete
¶
complete()
Mark the agent state as completed.
Source code in pyagenity/state/agent_state.py
109 110 111 112 113 114 |
|
error
¶
error(error_msg)
Mark the agent state as errored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
Error message to record. |
required |
Source code in pyagenity/state/agent_state.py
116 117 118 119 120 121 122 123 124 |
|
is_interrupted
¶
is_interrupted()
Check if the agent state is currently interrupted.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if interrupted, False otherwise. |
Source code in pyagenity/state/agent_state.py
79 80 81 82 83 84 85 86 87 88 |
|
is_running
¶
is_running()
Check if the agent state is currently running.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if running, False otherwise. |
Source code in pyagenity/state/agent_state.py
68 69 70 71 72 73 74 75 76 77 |
|
is_stopped_requested
¶
is_stopped_requested()
Check if a stop has been requested for the agent state.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if stop requested, False otherwise. |
Source code in pyagenity/state/agent_state.py
126 127 128 129 130 131 132 133 134 135 |
|
set_current_node
¶
set_current_node(node)
Set the current node in the execution metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
Node to set as current. |
required |
Source code in pyagenity/state/agent_state.py
98 99 100 101 102 103 104 105 106 107 |
|
set_interrupt
¶
set_interrupt(node, reason, status, data=None)
Set an interrupt in the execution metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
Node where the interrupt occurred. |
required |
|
str
|
Reason for the interrupt. |
required |
|
Execution status to set. |
required | |
|
dict | None
|
Optional additional interrupt data. |
None
|
Source code in pyagenity/state/agent_state.py
48 49 50 51 52 53 54 55 56 57 58 59 |
|