Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langgraph Typing Module

From Leeroopedia
Attribute Value
Source `libs/langgraph/langgraph/typing.py` (48 lines)
Domain Types, Core
Principle Type_System
Library langgraph
Import `from langgraph.typing import StateT, ContextT, InputT, OutputT`

Overview

The `typing.py` module defines the core type variables used throughout the LangGraph framework to parameterize graphs, protocols, and runtime objects. It provides six primary type variables and three covariant/contravariant variants, all bound to `StateLike` (or `StateLike | None` for context).

Description

This module centralizes the generic type variable definitions that form the backbone of LangGraph's type system. All type variables are bound to `StateLike` (imported from `langgraph._internal._typing`), which constrains them to types that can serve as graph state (typically `TypedDict` subclasses, dataclasses, or `BaseModel` subclasses).

Primary Type Variables

`StateT` -- Represents the state in a graph. Bound to `StateLike`. Used to parameterize `StateGraph`, `PregelProtocol`, and related classes.

`ContextT` -- Represents graph run-scoped context (e.g., user_id, database connections). Bound to `StateLike | None` with a default of `None`. This means graphs that do not use context do not need to specify this parameter.

`InputT` -- Represents the input to a `StateGraph`. Bound to `StateLike` with a default of `StateT`, meaning the input type defaults to the state type when not explicitly specified.

`OutputT` -- Represents the output of a `StateGraph`. Bound to `StateLike` with a default of `StateT`, meaning the output type defaults to the state type when not explicitly specified.

`NodeInputT` -- Represents the input to an individual node. Bound to `StateLike`.

Variant Type Variables

`StateT_co` -- Covariant variant of `StateT`, for use in positions where covariance is needed (e.g., return types).

`StateT_contra` -- Contravariant variant of `StateT`, for use in positions where contravariance is needed (e.g., parameter types).

`ContextT_contra` -- Contravariant variant of `ContextT`, with the same `StateLike | None` bound and `None` default.

`NodeInputT_contra` -- Contravariant variant of `NodeInputT`.

Usage

from langgraph.typing import StateT, ContextT, InputT, OutputT

# These type variables are used internally to parameterize protocols
# and classes. End users typically interact with them indirectly
# through StateGraph and compiled graph type annotations.

Code Reference

Type Variables

Name Bound Default Variance Description
`StateT` `StateLike` -- invariant Graph state type.
`StateT_co` `StateLike` -- covariant Covariant graph state type.
`StateT_contra` `StateLike` -- contravariant Contravariant graph state type.
`ContextT` None` `None` invariant Run-scoped context type.
`ContextT_contra` None` `None` contravariant Contravariant context type.
`InputT` `StateLike` `StateT` invariant Graph input type.
`OutputT` `StateLike` `StateT` invariant Graph output type.
`NodeInputT` `StateLike` -- invariant Node input type.
`NodeInputT_contra` `StateLike` -- contravariant Contravariant node input type.

I/O Contract

Aspect Detail
Input N/A -- this module only exports type variable definitions.
Output `TypeVar` instances used for generic parameterization throughout the framework.
Side Effects None. This module has no runtime behavior.
Constraints All type variables are bound to `StateLike`, constraining them to types usable as graph state.

Usage Examples

Using Type Variables in Custom Protocols

from typing import Generic
from langgraph.typing import StateT, ContextT

class MyGraphWrapper(Generic[StateT, ContextT]):
    def run(self, state: StateT, context: ContextT) -> StateT:
        ...

Understanding Default Types

from typing import TypedDict
from langgraph.graph import StateGraph

class MyState(TypedDict):
    messages: list

# InputT defaults to StateT (MyState)
# OutputT defaults to StateT (MyState)
# ContextT defaults to None
builder = StateGraph(MyState)

Separate Input and Output Types

from typing import TypedDict
from langgraph.graph import StateGraph

class InputState(TypedDict):
    query: str

class OutputState(TypedDict):
    result: str

class FullState(InputState, OutputState):
    intermediate: list

# InputT = InputState, OutputT = OutputState
builder = StateGraph(FullState, input=InputState, output=OutputState)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment