Agentflow
Agentflow copied to clipboard
Support generic / user-defined state types in GraphLifecycleHook (e.g. `on_graph_start[TState]`)
๐งพ Summary
GraphLifecycleHook methods are currently typed against the base AgentState, which makes it awkward to use a custom subclass (e.g. FashionState) without relying on # type: ignore or manual casting.
This proposal introduces generics so that hooks can operate on user-defined state types in a type-safe way. Custom fields should be first-class on state inside every lifecycle callback.
โ ๏ธ Current Behavior
In a downstream project, a custom state class is defined:
from agentflow.core.state.agent_state import AgentState
class FashionState(AgentState):
BRAND_IDENTITY: str | None = None
LAST_UPDATED_AT: int = 0
When subclassing GraphLifecycleHook, there are two problematic options:
- Annotate
stateasAgentStateโ โ lose access to custom fields - Annotate
stateasFashionStateโ โ type mismatch with base signature
Example:
class AgentLifecycleHook(GraphLifecycleHook):
async def on_graph_start(
self,
context: "GraphLifecycleContext",
state: FashionState, # mismatch with base signature
) -> "AgentState | None":
if state.BRAND_IDENTITY and state.LAST_UPDATED_AT < 5 * 60:
return None
state.BRAND_IDENTITY = "..." # type: ignore โ required today
return state
This issue affects all lifecycle methods:
on_graph_starton_graph_endon_graph_erroron_interrupton_resumeon_checkpointon_state_update
โ Desired Behavior
Allow hooks to be parameterized with a custom state type:
class AgentLifecycleHook(GraphLifecycleHook[FashionState]):
async def on_graph_start(
self,
context: "GraphLifecycleContext[FashionState]",
state: FashionState,
) -> "FashionState | None":
...
Expected Outcome
stateis fully typed asFashionState- No need for
# type: ignore - Full IDE autocomplete support
- Framework still treats it as
AgentStateat runtime
๐ก Proposed Change
Introduce a generic type parameter:
from typing import Generic, TypeVar
TState = TypeVar("TState", bound=AgentState)
Update GraphLifecycleHook
class GraphLifecycleHook(Generic[TState]):
async def on_graph_start(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
) -> "TState | None":
...
async def on_graph_end(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
) -> "TState | None":
...
async def on_graph_error(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
error: Exception,
) -> "TState | None":
...
async def on_interrupt(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
) -> "TState | None":
...
async def on_resume(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
) -> "TState | None":
...
async def on_checkpoint(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
) -> "TState | None":
...
async def on_state_update(
self,
context: "GraphLifecycleContext[TState]",
state: TState,
) -> "TState | None":
...
Update GraphLifecycleContext (if applicable)
class GraphLifecycleContext(Generic[TState]):
state: TState
๐ Backward Compatibility
- Default generic type remains
AgentState - Existing implementations continue to work without changes
- No runtime behavior changes (typing-only enhancement)
๐ Repro Reference
File:
virtual-try-on-ai/graph/validators/lifecycle.py
Issue:
AgentLifecycleHook.on_graph_start
- Needs
state: FashionState - Currently requires
# type: ignorefor assigning custom fields
๐ฏ Benefits
- Eliminates unsafe casts and
# type: ignore - Enables clean extensibility for custom state models
- Improves developer experience (DX) with better typing + autocomplete
- Aligns with modern Python typing patterns (Generics + TypeVar)