diff --git a/docs/docs/concepts/human_in_the_loop.md b/docs/docs/concepts/human_in_the_loop.md index eabe5fe85..442a98b35 100644 --- a/docs/docs/concepts/human_in_the_loop.md +++ b/docs/docs/concepts/human_in_the_loop.md @@ -23,7 +23,7 @@ To review, edit, and approve tool calls in an agent or workflow, [use LangGraph' ## Key capabilities -* **Persistent execution state**: Interrupts use LangGraph's [persistence](../../concepts/persistence.md) layer, which saves the graph state, to indefinitely pause graph execution until you resume. This is possible because LangGraph checkpoints the graph state after each step, which allows the system to persist execution context and later resume the workflow, continuing from where it left off. This supports asynchronous human review or input without time constraints. +* **Persistent execution state**: Interrupts use LangGraph's [persistence](./persistence.md) layer, which saves the graph state, to indefinitely pause graph execution until you resume. This is possible because LangGraph checkpoints the graph state after each step, which allows the system to persist execution context and later resume the workflow, continuing from where it left off. This supports asynchronous human review or input without time constraints. There are two ways to pause a graph: diff --git a/docs/docs/reference/runtime.md b/docs/docs/reference/runtime.md new file mode 100644 index 000000000..f326e78b8 --- /dev/null +++ b/docs/docs/reference/runtime.md @@ -0,0 +1,18 @@ +# Runtime + +::: langgraph.runtime.Runtime + options: + show_root_heading: true + show_root_full_path: false + members: + - context + - store + - stream_writer + - previous + +::: langgraph.runtime + options: + members: + - get_runtime + + diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index e76332942..506572963 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -250,6 +250,7 @@ nav: - Storage: reference/store.md - Caching: reference/cache.md - Types: reference/types.md + - Runtime: reference/runtime.md - Config: reference/config.md - Errors: reference/errors.md - Constants: reference/constants.md diff --git a/libs/langgraph/langgraph/func/__init__.py b/libs/langgraph/langgraph/func/__init__.py index 24ec545f7..ec453fec2 100644 --- a/libs/langgraph/langgraph/func/__init__.py +++ b/libs/langgraph/langgraph/func/__init__.py @@ -261,6 +261,10 @@ class entrypoint(Generic[ContextT]): passed to the workflow. cache_policy: A cache policy to use for caching the results of the workflow. retry_policy: A retry policy (or list of policies) to use for the workflow in case of a failure. + config_schema: Specifies the schema for the `configurable` key in the `RunnableConfig` object. + !!! warning "Deprecated" + This parameter is deprecated and support will be removed in v2.0.0. + Please use `context_schema` instead. Example: Using entrypoint and tasks ```python diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index 9e6d8552e..0bee5c0dc 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -128,6 +128,10 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): Use this to expose immutable context data to your nodes, like user_id, db_conn, etc. input_schema: The schema class that defines the input to the graph. output_schema: The schema class that defines the output from the graph. + config_schema: Specifies the schema for the `configurable` key in the `RunnableConfig` object. + !!! warning "Deprecated" + This parameter is deprecated and support will be removed in v2.0.0. + Please use `context_schema` instead. Example: ```python diff --git a/libs/langgraph/langgraph/pregel/main.py b/libs/langgraph/langgraph/pregel/main.py index a402d7cf4..45d6399ff 100644 --- a/libs/langgraph/langgraph/pregel/main.py +++ b/libs/langgraph/langgraph/pregel/main.py @@ -602,6 +602,7 @@ class Pregel( Defaults to None.""" context_schema: type[ContextT] | None = None + """Specifies the schema for the context object that will be passed to the workflow.""" config: RunnableConfig | None = None diff --git a/libs/langgraph/langgraph/runtime.py b/libs/langgraph/langgraph/runtime.py index b5d7cdfdd..5c1053a78 100644 --- a/libs/langgraph/langgraph/runtime.py +++ b/libs/langgraph/langgraph/runtime.py @@ -11,6 +11,8 @@ from langgraph.store.base import BaseStore from langgraph.types import _DC_KWARGS, StreamWriter from langgraph.typing import ContextT +__all__ = ("Runtime", "get_runtime") + def _no_op_stream_writer(_: Any) -> None: ... @@ -24,9 +26,61 @@ class _RuntimeOverrides(TypedDict, Generic[ContextT], total=False): @dataclass(**_DC_KWARGS) class Runtime(Generic[ContextT]): - """Convenience class that bundles run-scoped context and graph configuration. + """Convenience class that bundles run-scoped context and other runtime utilities. - !!! version-added "Added in version 0.6.0." + !!! version-added "Added in version v0.6.0" + + Example: + + ```python + from typing import TypedDict + from langgraph.graph import StateGraph + from dataclasses import dataclass + from langgraph.runtime import Runtime + from langgraph.store.memory import InMemoryStore + + + @dataclass + class Context: # (1)! + user_id: str + + + class State(TypedDict, total=False): + response: str + + + store = InMemoryStore() # (2)! + store.put(("users",), "user_123", {"name": "Alice"}) + + + def personalized_greeting(state: State, runtime: Runtime[Context]) -> State: + '''Generate personalized greeting using runtime context and store.''' + user_id = runtime.context.user_id # (3)! + name = "unknown_user" + if runtime.store: + if memory := runtime.store.get(("users",), user_id): + name = memory.value["name"] + + response = f"Hello {name}! Nice to see you again." + return {"response": response} + + + graph = ( + StateGraph(state_schema=State, context_schema=Context) + .add_node("personalized_greeting", personalized_greeting) + .set_entry_point("personalized_greeting") + .set_finish_point("personalized_greeting") + .compile(store=store) + ) + + result = graph.invoke({}, context=Context(user_id="user_123")) + print(result) + # > {'response': 'Hello Alice! Nice to see you again.'} + ``` + + 1. Define a schema for the runtime context. + 2. Create a store to persist memories and other information. + 3. Use the runtime context to access the user_id. """ context: ContextT = field(default=None) # type: ignore[assignment] @@ -76,7 +130,14 @@ DEFAULT_RUNTIME = Runtime( def get_runtime(context_schema: type[ContextT] | None = None) -> Runtime[ContextT]: - """Get the runtime for the current graph run.""" + """Get the runtime for the current graph run. + + Args: + context_schema: Optional schema used for type hinting the return type of the runtime. + + Returns: + The runtime for the current graph run. + """ # TODO: in an ideal world, we would have a context manager for # the runtime that's independent of the config. this will follow diff --git a/libs/langgraph/langgraph/types.py b/libs/langgraph/langgraph/types.py index 867991b35..3dcc8f8dc 100644 --- a/libs/langgraph/langgraph/types.py +++ b/libs/langgraph/langgraph/types.py @@ -149,10 +149,25 @@ class Interrupt: """Information about an interrupt that occurred in a node. !!! version-added "Added in version 0.2.24." + + !!! version-changed "Changed in version v0.4.0" + * `interrupt_id` was introduced as a property + + !!! version-changed "Changed in version v0.6.0" + + The following attributes have been removed: + + * `ns` + * `when` + * `resumable` + * `interrupt_id`, deprecated in favor of `id` """ value: Any + """The value associated with the interrupt.""" + id: str + """The ID of the interrupt. Can be used to resume the interrupt directly.""" def __init__( self, diff --git a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py index 7b63c528c..34c1a930b 100644 --- a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py @@ -363,6 +363,10 @@ def create_react_agent( name: An optional name for the CompiledStateGraph. This name will be automatically used when adding ReAct agent graph to another graph as a subgraph node - particularly useful for building multi-agent systems. + config_schema: Specifies the schema for the `configurable` key in the `RunnableConfig` object. + !!! warning "Deprecated" + This parameter is deprecated and support will be removed in v2.0.0. + Please use `context_schema` instead. Returns: A compiled LangChain runnable that can be used for chat interactions.