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..6ecebb9c3 100644 --- a/libs/langgraph/langgraph/func/__init__.py +++ b/libs/langgraph/langgraph/func/__init__.py @@ -262,6 +262,11 @@ class entrypoint(Generic[ContextT]): 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. + !!! warning "`config_schema` Deprecated" + The `config_schema` parameter is deprecated in v0.6.0 and support will be removed in v2.0.0. + Please use `context_schema` instead to specify the schema for run-scoped context. + + Example: Using entrypoint and tasks ```python import time diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index 9e6d8552e..e7854b900 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -129,6 +129,10 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): input_schema: The schema class that defines the input to the graph. output_schema: The schema class that defines the output from the graph. + !!! warning "`config_schema` Deprecated" + The `config_schema` parameter is deprecated in v0.6.0 and support will be removed in v2.0.0. + Please use `context_schema` instead to specify the schema for run-scoped context. + Example: ```python from langchain_core.runnables import RunnableConfig diff --git a/libs/langgraph/langgraph/pregel/main.py b/libs/langgraph/langgraph/pregel/main.py index 1aec1da33..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 @@ -2438,6 +2439,8 @@ class Pregel( Args: input: The input to the graph. config: The configuration to use for the run. + context: The static context to use for the run. + !!! version-added "Added in version 0.6.0." stream_mode: The mode to stream output, defaults to `self.stream_mode`. Options are: @@ -2694,6 +2697,8 @@ class Pregel( Args: input: The input to the graph. config: The configuration to use for the run. + context: The static context to use for the run. + !!! version-added "Added in version 0.6.0." stream_mode: The mode to stream output, defaults to `self.stream_mode`. Options are: @@ -2981,6 +2986,8 @@ class Pregel( Args: input: The input data for the graph. It can be a dictionary or any other type. config: Optional. The configuration for the graph run. + context: The static context to use for the run. + !!! version-added "Added in version 0.6.0." stream_mode: Optional[str]. The stream mode for the graph run. Default is "values". print_mode: Accepts the same values as `stream_mode`, but only prints the output to the console, for debugging purposes. Does not affect the output of the graph in any way. output_keys: Optional. The output keys to retrieve from the graph run. @@ -3058,6 +3065,8 @@ class Pregel( Args: input: The input data for the computation. It can be a dictionary or any other type. config: Optional. The configuration for the computation. + context: The static context to use for the run. + !!! version-added "Added in version 0.6.0." stream_mode: Optional. The stream mode for the computation. Default is "values". print_mode: Accepts the same values as `stream_mode`, but only prints the output to the console, for debugging purposes. Does not affect the output of the graph in any way. output_keys: Optional. The output keys to include in the result. Default is None. diff --git a/libs/langgraph/langgraph/runtime.py b/libs/langgraph/langgraph/runtime.py index c819f1d4e..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 1.0.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..f2c472fcf 100644 --- a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py @@ -364,6 +364,11 @@ def create_react_agent( 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. + !!! warning "`config_schema` Deprecated" + The `config_schema` parameter is deprecated in v0.6.0 and support will be removed in v2.0.0. + Please use `context_schema` instead to specify the schema for run-scoped context. + + Returns: A compiled LangChain runnable that can be used for chat interactions.