Implementation:Langchain ai Langgraph ManagedValue Base
| Attribute | Value |
|---|---|
| Source | `libs/langgraph/langgraph/managed/base.py` (31 lines) |
| Domain | Managed_Values, Core |
| Principle | Managed_Value_System |
| Library | langgraph |
| Import | `from langgraph.managed.base import ManagedValue, ManagedValueSpec` |
Overview
The `base.py` module defines the abstract base class for LangGraph's managed value system. `ManagedValue` is a generic ABC that provides a static `get` method for computing values from the internal Pregel scratchpad. This module also provides type aliases and a type guard function for working with managed value specifications.
Description
Managed values are a mechanism that allows LangGraph to inject computed, runtime-scoped values into graph node signatures. Rather than storing these values in the graph state, managed values are computed on-the-fly from the execution scratchpad each time a node is invoked.
`ManagedValue(ABC, Generic[V])` -- The abstract base class for all managed values. It declares a single abstract static method:
- `get(scratchpad: PregelScratchpad) -> V` -- Computes and returns the managed value from the current execution scratchpad.
`ManagedValueSpec` -- A type alias equal to `type[ManagedValue]`. Used to annotate specifications that identify a managed value class.
`is_managed_value(value: Any) -> TypeGuard[ManagedValueSpec]` -- A type guard function that returns `True` if the given value is a class that is a subclass of `ManagedValue`. This is used internally to detect managed value annotations in graph state schemas.
`ManagedValueMapping` -- A type alias equal to `dict[str, ManagedValueSpec]`. Represents a mapping from state key names to their managed value classes.
The `PregelScratchpad` provides execution metadata such as the current step number and the maximum step limit, which managed value implementations (like `IsLastStepManager` and `RemainingStepsManager`) use to compute their values.
Usage
from langgraph.managed.base import ManagedValue, ManagedValueSpec, is_managed_value
# Check if a class is a managed value
class MyManager(ManagedValue[str]):
@staticmethod
def get(scratchpad):
return "computed_value"
assert is_managed_value(MyManager) # True
assert not is_managed_value(str) # False
Code Reference
ManagedValue Class
| Member | Signature | Description |
|---|---|---|
| `get` | `@staticmethod @abstractmethod get(scratchpad: PregelScratchpad) -> V` | Computes and returns the managed value from the current execution scratchpad. |
Type Aliases
| Name | Type | Description |
|---|---|---|
| `ManagedValueSpec` | `type[ManagedValue]` | A managed value class reference. |
| `ManagedValueMapping` | `dict[str, ManagedValueSpec]` | Maps state key names to their managed value classes. |
Type Guard
| Function | Signature | Description |
|---|---|---|
| `is_managed_value` | `(value: Any) -> TypeGuard[ManagedValueSpec]` | Returns `True` if `value` is a class that is a subclass of `ManagedValue`. |
I/O Contract
| Aspect | Detail |
|---|---|
| Input | `ManagedValue.get` receives a `PregelScratchpad` containing execution metadata (step number, stop limit, etc.). |
| Output | Returns a value of generic type `V` computed from the scratchpad. |
| Side Effects | None. Managed values are pure computations from the scratchpad state. |
Usage Examples
Creating a Custom Managed Value
from typing import Annotated
from langgraph.managed.base import ManagedValue
from langgraph._internal._scratchpad import PregelScratchpad
class CurrentStepManager(ManagedValue[int]):
@staticmethod
def get(scratchpad: PregelScratchpad) -> int:
return scratchpad.step
# Use with Annotated to inject into node signatures
CurrentStep = Annotated[int, CurrentStepManager]
Using is_managed_value for Introspection
from langgraph.managed.base import is_managed_value, ManagedValue
class MyManager(ManagedValue[str]):
@staticmethod
def get(scratchpad):
return "hello"
print(is_managed_value(MyManager)) # True
print(is_managed_value(int)) # False
Related Pages
- Langchain_ai_Langgraph_IsLastStep -- Concrete managed value implementations built on this base class.
- Langchain_ai_Langgraph_Typing_Module -- Type variables used across LangGraph.
- Langchain_ai_Langgraph_PregelProtocol -- The Pregel execution protocol that provides the scratchpad.