Agentflow icon indicating copy to clipboard operation
Agentflow copied to clipboard

Support generic / user-defined state types in GraphLifecycleHook (e.g. `on_graph_start[TState]`)

Open Mothilal-M opened this issue 6 months ago โ€ข 0 comments

๐Ÿงพ 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 state as AgentState โ†’ โŒ lose access to custom fields
  • Annotate state as FashionState โ†’ โŒ 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_start
  • on_graph_end
  • on_graph_error
  • on_interrupt
  • on_resume
  • on_checkpoint
  • on_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

  • state is fully typed as FashionState
  • No need for # type: ignore
  • Full IDE autocomplete support
  • Framework still treats it as AgentState at 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: ignore for 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)

Mothilal-M avatar May 06 '26 07:05 Mothilal-M