The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
feat(langchain_v1): simplify to use ONE agent (#33302)
Sydney Runkle62ccf7e8a46a
diff --git a/.claude/settings.local.json b/.claude/settings.local.jsonindex 630c4d8fd1..c39d2e22d2 100644--- a/.claude/settings.local.json+++ b/.claude/settings.local.json@@ -7,7 +7,10 @@"WebFetch(domain:ai.pydantic.dev)","WebFetch(domain:openai.github.io)","Bash(uv run:*)",- "Bash(python3:*)"+ "Bash(python3:*)",+ "WebFetch(domain:github.com)",+ "Bash(gh pr view:*)",+ "Bash(gh pr diff:*)"],"deny": [],"ask": []diff --git a/libs/langchain_v1/langchain/agents/__init__.py b/libs/langchain_v1/langchain/agents/__init__.pyindex 9f859fd1f0..72abb7ed62 100644--- a/libs/langchain_v1/langchain/agents/__init__.py+++ b/libs/langchain_v1/langchain/agents/__init__.py@@ -1,6 +1,7 @@"""langgraph.prebuilt exposes a higher-level API for creating and executing agents and tools."""-from langchain.agents.react_agent import AgentState, create_agent+from langchain.agents.factory import create_agent+from langchain.agents.middleware.types import AgentState__all__ = ["AgentState",diff --git a/libs/langchain_v1/langchain/agents/middleware_agent.py b/libs/langchain_v1/langchain/agents/factory.pysimilarity index 83%rename from libs/langchain_v1/langchain/agents/middleware_agent.pyrename to libs/langchain_v1/langchain/agents/factory.pyindex 6ff5d306b2..f3d6e9fe6d 100644--- a/libs/langchain_v1/langchain/agents/middleware_agent.py+++ b/libs/langchain_v1/langchain/agents/factory.py@@ -1,19 +1,27 @@-"""Middleware agent implementation."""+"""Agent factory for creating agents with middleware support."""++from __future__ import annotationsimport itertools-from collections.abc import Callable, Sequence-from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints+from typing import (+ TYPE_CHECKING,+ Annotated,+ Any,+ cast,+ get_args,+ get_origin,+ get_type_hints,+)from langchain_core.language_models.chat_models import BaseChatModelfrom langchain_core.messages import AIMessage, AnyMessage, SystemMessage, ToolMessage-from langchain_core.runnables import Runnablefrom langchain_core.tools import BaseToolfrom langgraph._internal._runnable import RunnableCallablefrom langgraph.constants import END, STARTfrom langgraph.graph.state import StateGraph-from langgraph.runtime import Runtime+from langgraph.runtime import Runtime # noqa: TC002from langgraph.types import Send-from langgraph.typing import ContextT+from langgraph.typing import ContextT # noqa: TC002from typing_extensions import NotRequired, Required, TypedDict, TypeVarfrom langchain.agents.middleware.types import (@@ -37,6 +45,15 @@ from langchain.agents.structured_output import (from langchain.chat_models import init_chat_modelfrom langchain.tools import ToolNode+if TYPE_CHECKING:+ from collections.abc import Callable, Sequence++ from langchain_core.runnables import Runnable+ from langgraph.cache.base import BaseCache+ from langgraph.graph.state import CompiledStateGraph+ from langgraph.store.base import BaseStore+ from langgraph.types import Checkpointer+STRUCTURED_OUTPUT_ERROR_TEMPLATE = "Error: {error}\n Please fix your mistakes."ResponseT = TypeVar("ResponseT")@@ -176,17 +193,92 @@ def _handle_structured_output_error(def create_agent( # noqa: PLR0915- *,model: str | BaseChatModel,tools: Sequence[BaseTool | Callable | dict[str, Any]] | ToolNode | None = None,+ *,system_prompt: str | None = None,middleware: Sequence[AgentMiddleware[AgentState[ResponseT], ContextT]] = (),response_format: ResponseFormat[ResponseT] | type[ResponseT] | None = None,context_schema: type[ContextT] | None = None,-) -> StateGraph[+ checkpointer: Checkpointer | None = None,+ store: BaseStore | None = None,+ interrupt_before: list[str] | None = None,+ interrupt_after: list[str] | None = None,+ debug: bool = False,+ name: str | None = None,+ cache: BaseCache | None = None,+) -> CompiledStateGraph[AgentState[ResponseT], ContextT, PublicAgentState[ResponseT], PublicAgentState[ResponseT]]:- """Create a middleware agent graph."""+ """Creates an agent graph that calls tools in a loop until a stopping condition is met.++ For more details on using ``create_agent``,+ visit [Agents](https://docs.langchain.com/oss/python/langchain/agents) documentation.++ Args:+ model: The language model for the agent. Can be a string identifier+ (e.g., ``"openai:gpt-4"``), a chat model instance (e.g., ``ChatOpenAI()``).+ tools: A list of tools or a ToolNode instance. If ``None`` or an empty list,+ the agent will consist of a single LLM node without tool calling.+ system_prompt: An optional system prompt for the LLM. If provided as a string,+ it will be converted to a SystemMessage and added to the beginning+ of the message list.+ middleware: A sequence of middleware instances to apply to the agent.+ Middleware can intercept and modify agent behavior at various stages.+ response_format: An optional configuration for structured responses.+ Can be a ToolStrategy, ProviderStrategy, or a Pydantic model class.+ If provided, the agent will handle structured output during the+ conversation flow. Raw schemas will be wrapped in an appropriate strategy+ based on model capabilities.+ context_schema: An optional schema for runtime context.+ checkpointer: An optional checkpoint saver object. This is used for persisting+ the state of the graph (e.g., as chat memory) for a single thread+ (e.g., a single conversation).+ store: An optional store object. This is used for persisting data+ across multiple threads (e.g., multiple conversations / users).+ interrupt_before: An optional list of node names to interrupt before.+ This is useful if you want to add a user confirmation or other interrupt+ before taking an action.+ interrupt_after: An optional list of node names to interrupt after.+ This is useful if you want to return directly or run additional processing+ on an output.+ debug: A flag indicating whether to enable debug mode.+ name: An optional name for the CompiledStateGraph.+ This name will be automatically used when adding the agent graph to+ another graph as a subgraph node - particularly useful for building+ multi-agent systems.+ cache: An optional BaseCache instance to enable caching of graph execution.++ Returns:+ A compiled StateGraph that can be used for chat interactions.++ The agent node calls the language model with the messages list (after applying+ the system prompt). If the resulting AIMessage contains ``tool_calls``, the graph will+ then call the tools. The tools node executes the tools and adds the responses+ to the messages list as ``ToolMessage`` objects. The agent node then calls the+ language model again. The process repeats until no more ``tool_calls`` are+ present in the response. The agent then returns the full list of messages.++ Example:+ ```python+ from langchain.agents import create_agent+++ def check_weather(location: str) -> str:+ '''Return the weather forecast for the specified location.'''+ return f"It's always sunny in {location}"+++ graph = create_agent(+ model="anthropic:claude-3-7-sonnet-latest",+ tools=[check_weather],+ system_prompt="You are a helpful assistant",+ )+ inputs = {"messages": [{"role": "user", "content": "what is the weather in sf"}]}+ for chunk in graph.stream(inputs, stream_mode="updates"):+ print(chunk)+ ```+ """# init chat modelif isinstance(model, str):model = init_chat_model(model)@@ -230,7 +322,7 @@ def create_agent( # noqa: PLR0915# Setup toolstool_node: ToolNode | None = Noneif isinstance(tools, list):- # Extract built-in provider tools (dict format) and regular tools (BaseTool)+ # Extract built-in provider tools (dict format) and regular tools (BaseTool/callables)built_in_tools = [t for t in tools if isinstance(t, dict)]regular_tools = [t for t in tools if not isinstance(t, dict)]@@ -241,9 +333,13 @@ def create_agent( # noqa: PLR0915tool_node = ToolNode(tools=available_tools) if available_tools else None# Default tools for ModelRequest initialization- # Include built-ins and regular tools (can be changed dynamically by middleware)+ # Use converted BaseTool instances from ToolNode (not raw callables)+ # Include built-ins and converted tools (can be changed dynamically by middleware)# Structured tools are NOT included - they're added dynamically based on response_format- default_tools = regular_tools + middleware_tools + built_in_tools+ if tool_node:+ default_tools = list(tool_node.tools_by_name.values()) + built_in_tools+ else:+ default_tools = list(built_in_tools)elif isinstance(tools, ToolNode):tool_node = toolsif tool_node:@@ -252,10 +348,15 @@ def create_agent( # noqa: PLR0915tool_node = ToolNode(available_tools)# default_tools includes all client-side tools (no built-ins or structured tools)- default_tools = available_tools+ default_tools = list(tool_node.tools_by_name.values())+ else:+ default_tools = []+ # No tools provided, only middleware_tools available+ elif middleware_tools:+ tool_node = ToolNode(middleware_tools)+ default_tools = list(tool_node.tools_by_name.values())else:- # No tools provided, only middleware_tools available- default_tools = middleware_tools+ default_tools = []# validate middlewareassert len({m.name for m in middleware}) == len(middleware), ( # noqa: S101@@ -425,8 +526,11 @@ def create_agent( # noqa: PLR0915is the actual strategy used (may differ from initial if auto-detected)."""# Validate ONLY client-side tools that need to exist in tool_node- # Build map of available client-side tools (regular_tools + middleware_tools)- available_tools_by_name = {t.name: t for t in default_tools if isinstance(t, BaseTool)}+ # Build map of available client-side tools from the ToolNode+ # (which has already converted callables)+ available_tools_by_name = {}+ if tool_node:+ available_tools_by_name = tool_node.tools_by_name.copy()# Check if any requested tools are unknown CLIENT-SIDE toolsunknown_tool_names = []@@ -434,7 +538,7 @@ def create_agent( # noqa: PLR0915# Only validate BaseTool instances (skip built-in dict tools)if isinstance(t, dict):continue- if t.name not in available_tools_by_name:+ if isinstance(t, BaseTool) and t.name not in available_tools_by_name:unknown_tool_names.append(t.name)if unknown_tool_names:@@ -468,7 +572,8 @@ def create_agent( # noqa: PLR0915effective_response_format = request.response_format# Build final tools list including structured output tools- # request.tools already contains both BaseTool and dict (built-in) tools+ # request.tools now only contains BaseTool instances (converted from callables)+ # and dicts (built-ins)final_tools = list(request.tools)if isinstance(effective_response_format, ToolStrategy):# Add structured output tools to final tools list@@ -767,6 +872,12 @@ def create_agent( # noqa: PLR0915),[loop_entry_node, "tools", exit_node],)+ elif len(structured_output_tools) > 0:+ graph.add_conditional_edges(+ loop_exit_node,+ _make_model_to_model_edge(loop_entry_node, exit_node),+ [loop_entry_node, exit_node],+ )elif loop_exit_node == "model_request":# If no tools and no after_model, go directly to exit_nodegraph.add_edge(loop_exit_node, exit_node)@@ -857,7 +968,15 @@ def create_agent( # noqa: PLR0915can_jump_to=_get_can_jump_to(middleware_w_after_agent[0], "after_agent"),)- return graph+ return graph.compile(+ checkpointer=checkpointer,+ store=store,+ interrupt_before=interrupt_before,+ interrupt_after=interrupt_after,+ debug=debug,+ name=name,+ cache=cache,+ )def _resolve_jump(jump_to: JumpTo | None, first_node: str) -> str | None:@@ -891,8 +1010,10 @@ def _make_model_to_tools_edge(structured_output_tools: dict[str, OutputToolBinding],tool_node: ToolNode,exit_node: str,-) -> Callable[[dict[str, Any]], str | list[Send] | None]:- def model_to_tools(state: dict[str, Any]) -> str | list[Send] | None:+) -> Callable[[dict[str, Any], Runtime[ContextT]], str | list[Send] | None]:+ def model_to_tools(+ state: dict[str, Any], runtime: Runtime[ContextT]+ ) -> str | list[Send] | None:# 1. if there's an explicit jump_to in the state, use itif jump_to := state.get("jump_to"):return _resolve_jump(jump_to, first_node)@@ -914,26 +1035,55 @@ def _make_model_to_tools_edge(# 3. if there are pending tool calls, jump to the tool nodeif pending_tool_calls:pending_tool_calls = [- tool_node.inject_tool_args(call, state, None) for call in pending_tool_calls+ tool_node.inject_tool_args(call, state, runtime.store)+ for call in pending_tool_calls]return [Send("tools", [tool_call]) for tool_call in pending_tool_calls]- # 4. AIMessage has tool calls, but there are no pending tool calls+ # 4. if there is a structured response, exit the loop+ if "structured_response" in state:+ return exit_node++ # 5. AIMessage has tool calls, but there are no pending tool calls# which suggests the injection of artificial tool messages. jump to the first nodereturn first_nodereturn model_to_tools+def _make_model_to_model_edge(+ first_node: str,+ exit_node: str,+) -> Callable[[dict[str, Any], Runtime[ContextT]], str | list[Send] | None]:+ def model_to_model(+ state: dict[str, Any],+ runtime: Runtime[ContextT], # noqa: ARG001+ ) -> str | list[Send] | None:+ # 1. Priority: Check for explicit jump_to directive from middleware+ if jump_to := state.get("jump_to"):+ return _resolve_jump(jump_to, first_node)++ # 2. Exit condition: A structured response was generated+ if "structured_response" in state:+ return exit_node++ # 3. Default: Continue the loop, there may have been an issue+ # with structured output generation, so we need to retry+ return first_node++ return model_to_model++def _make_tools_to_model_edge(tool_node: ToolNode,next_node: str,structured_output_tools: dict[str, OutputToolBinding],exit_node: str,-) -> Callable[[dict[str, Any]], str | None]:- def tools_to_model(state: dict[str, Any]) -> str | None:+) -> Callable[[dict[str, Any], Runtime[ContextT]], str | None]:+ def tools_to_model(state: dict[str, Any], runtime: Runtime[ContextT]) -> str | None: # noqa: ARG001last_ai_message, tool_messages = _fetch_last_ai_and_tool_messages(state["messages"])+ # 1. Exit condition: All executed tools have return_direct=Trueif all(tool_node.tools_by_name[c["name"]].return_directfor c in last_ai_message.tool_calls@@ -941,9 +1091,13 @@ def _make_tools_to_model_edge():return exit_node+ # 2. Exit condition: A structured output tool was executedif any(t.name in structured_output_tools for t in tool_messages):return exit_node+ # 3. Default: Continue the loop+ # Tool execution completed successfully, route back to the model+ # so it can process the tool results and decide the next action.return next_nodereturn tools_to_model@@ -960,7 +1114,6 @@ def _add_middleware_edge(Args:graph: The graph to add the edge to.- method: The method to call for the middleware node.name: The name of the middleware node.default_destination: The default destination for the edge.model_destination: The destination for the edge to the model.@@ -984,3 +1137,8 @@ def _add_middleware_edge(else:graph.add_edge(name, default_destination)+++__all__ = [+ "create_agent",+]diff --git a/libs/langchain_v1/langchain/agents/middleware/__init__.py b/libs/langchain_v1/langchain/agents/middleware/__init__.pyindex 832b86fcc2..1afbba1c09 100644--- a/libs/langchain_v1/langchain/agents/middleware/__init__.py+++ b/libs/langchain_v1/langchain/agents/middleware/__init__.py@@ -1,11 +1,11 @@"""Middleware plugins for agents."""-from .call_tracking import ModelCallLimitMiddlewarefrom .context_editing import (ClearToolUsesEdit,ContextEditingMiddleware,)from .human_in_the_loop import HumanInTheLoopMiddleware+from .model_call_limit import ModelCallLimitMiddlewarefrom .model_fallback import ModelFallbackMiddlewarefrom .pii import PIIDetectionError, PIIMiddlewarefrom .planning import PlanningMiddlewarediff --git a/libs/langchain_v1/langchain/agents/middleware/call_tracking.py b/libs/langchain_v1/langchain/agents/middleware/model_call_limit.pysimilarity index 100%rename from libs/langchain_v1/langchain/agents/middleware/call_tracking.pyrename to libs/langchain_v1/langchain/agents/middleware/model_call_limit.pydiff --git a/libs/langchain_v1/langchain/agents/react_agent.py b/libs/langchain_v1/langchain/agents/react_agent.pydeleted file mode 100644index cd1cc469c7..0000000000--- a/libs/langchain_v1/langchain/agents/react_agent.py+++ /dev/null@@ -1,1229 +0,0 @@-"""React agent implementation."""--from __future__ import annotations--import inspect-from collections.abc import Awaitable, Callable, Sequence-from dataclasses import asdict, is_dataclass-from typing import (- TYPE_CHECKING,- Annotated,- Any,- Generic,- Literal,- cast,- get_type_hints,-)-from warnings import warn--from langchain_core.language_models import (- BaseChatModel,- LanguageModelInput,- LanguageModelLike,-)-from langchain_core.messages import (- AIMessage,- AnyMessage,- BaseMessage,- SystemMessage,- ToolCall,- ToolMessage,-)-from langchain_core.runnables import (- Runnable,- RunnableConfig,-)-from langgraph._internal._runnable import RunnableCallable, RunnableLike-from langgraph._internal._typing import MISSING-from langgraph.errors import ErrorCode, create_error_message-from langgraph.graph import END, StateGraph-from langgraph.graph.message import add_messages-from langgraph.managed import RemainingSteps # noqa: TC002-from langgraph.types import Checkpointer, Command, Send-from langgraph.typing import ContextT, StateT-from pydantic import BaseModel-from typing_extensions import NotRequired, TypedDict, TypeVar--from langchain.agents.middleware_agent import create_agent as create_middleware_agent-from langchain.agents.structured_output import (- MultipleStructuredOutputsError,- OutputToolBinding,- ProviderStrategy,- ProviderStrategyBinding,- ResponseFormat,- StructuredOutputValidationError,- ToolStrategy,-)-from langchain.chat_models import init_chat_model-from langchain.tools import ToolNode--if TYPE_CHECKING:- from langchain_core.tools import BaseTool- from langgraph.graph.state import CompiledStateGraph- from langgraph.runtime import Runtime- from langgraph.store.base import BaseStore-- from langchain.agents._internal._typing import (- SyncOrAsync,- )- from langchain.agents.types import AgentMiddleware--StructuredResponseT = TypeVar("StructuredResponseT", default=None)--STRUCTURED_OUTPUT_ERROR_TEMPLATE = "Error: {error}\n Please fix your mistakes."---class AgentState(TypedDict):- """The state of the agent."""-- messages: Annotated[Sequence[BaseMessage], add_messages]-- remaining_steps: NotRequired[RemainingSteps]---class AgentStatePydantic(BaseModel):- """The state of the agent."""-- messages: Annotated[Sequence[BaseMessage], add_messages]-- remaining_steps: RemainingSteps = 25---class AgentStateWithStructuredResponse(AgentState, Generic[StructuredResponseT]):- """The state of the agent with a structured response."""-- structured_response: StructuredResponseT---class AgentStateWithStructuredResponsePydantic(AgentStatePydantic, Generic[StructuredResponseT]):- """The state of the agent with a structured response."""-- structured_response: StructuredResponseT---PROMPT_RUNNABLE_NAME = "Prompt"--Prompt = (- SystemMessage- | str- | Callable[[StateT], LanguageModelInput]- | Runnable[StateT, LanguageModelInput]-)---def _get_state_value(state: StateT, key: str, default: Any = None) -> Any:- return state.get(key, default) if isinstance(state, dict) else getattr(state, key, default)---def _get_prompt_runnable(prompt: Prompt | None) -> Runnable:- prompt_runnable: Runnable- if prompt is None:- prompt_runnable = RunnableCallable(- lambda state: _get_state_value(state, "messages"), name=PROMPT_RUNNABLE_NAME- )- elif isinstance(prompt, str):- _system_message: BaseMessage = SystemMessage(content=prompt)- prompt_runnable = RunnableCallable(- lambda state: [_system_message, *_get_state_value(state, "messages")],- name=PROMPT_RUNNABLE_NAME,- )- elif isinstance(prompt, SystemMessage):- prompt_runnable = RunnableCallable(- lambda state: [prompt, *_get_state_value(state, "messages")],- name=PROMPT_RUNNABLE_NAME,- )- elif inspect.iscoroutinefunction(prompt):- prompt_runnable = RunnableCallable(- None,- prompt,- name=PROMPT_RUNNABLE_NAME,- )- elif callable(prompt):- prompt_runnable = RunnableCallable(- prompt,- name=PROMPT_RUNNABLE_NAME,- )- elif isinstance(prompt, Runnable):- prompt_runnable = prompt- else:- msg = f"Got unexpected type for `prompt`: {type(prompt)}"- raise ValueError(msg)-- return prompt_runnable---def _validate_chat_history(- messages: Sequence[BaseMessage],-) -> None:- """Validate that all tool calls in AIMessages have a corresponding ToolMessage."""- all_tool_calls = [- tool_call- for message in messages- if isinstance(message, AIMessage)- for tool_call in message.tool_calls- ]- tool_call_ids_with_results = {- message.tool_call_id for message in messages if isinstance(message, ToolMessage)- }- tool_calls_without_results = [- tool_call- for tool_call in all_tool_calls- if tool_call["id"] not in tool_call_ids_with_results- ]- if not tool_calls_without_results:- return-- error_message = create_error_message(- message="Found AIMessages with tool_calls that do not have a corresponding ToolMessage. "- f"Here are the first few of those tool calls: {tool_calls_without_results[:3]}.\n\n"- "Every tool call (LLM requesting to call a tool) in the message history "- "MUST have a corresponding ToolMessage (result of a tool invocation to return to the LLM) -"- " this is required by most LLM providers.",- error_code=ErrorCode.INVALID_CHAT_HISTORY,- )- raise ValueError(error_message)---class _AgentBuilder(Generic[StateT, ContextT, StructuredResponseT]):- """Internal builder class for constructing and agent."""-- def __init__(- self,- model: str | BaseChatModel | SyncOrAsync[[StateT, Runtime[ContextT]], BaseChatModel],- tools: Sequence[BaseTool | Callable | dict[str, Any]] | ToolNode,- *,- prompt: Prompt | None = None,- response_format: ResponseFormat[StructuredResponseT] | None = None,- pre_model_hook: RunnableLike | None = None,- post_model_hook: RunnableLike | None = None,- state_schema: type[StateT] | None = None,- context_schema: type[ContextT] | None = None,- version: Literal["v1", "v2"] = "v2",- name: str | None = None,- store: BaseStore | None = None,- ) -> None:- self.model = model- self.tools = tools- self.prompt = prompt- self.response_format = response_format- self.pre_model_hook = pre_model_hook- self.post_model_hook = post_model_hook- self.state_schema = state_schema- self.context_schema = context_schema- self.version = version- self.name = name- self.store = store-- if isinstance(model, Runnable) and not isinstance(model, BaseChatModel):- msg = (- "Expected `model` to be a BaseChatModel or a string, got {type(model)}."- "The `model` parameter should not have pre-bound tools, "- "simply pass the model and tools separately."- )- raise ValueError(msg)-- self._setup_tools()- self._setup_state_schema()- self._setup_structured_output()- self._setup_model()-- def _setup_tools(self) -> None:- """Setup tool-related attributes."""- if isinstance(self.tools, ToolNode):- self._tool_classes = list(self.tools.tools_by_name.values())- self._tool_node = self.tools- self._llm_builtin_tools = []- else:- self._llm_builtin_tools = [t for t in self.tools if isinstance(t, dict)]- self._tool_node = ToolNode([t for t in self.tools if not isinstance(t, dict)])- self._tool_classes = list(self._tool_node.tools_by_name.values())-- self._should_return_direct = {t.name for t in self._tool_classes if t.return_direct}- self._tool_calling_enabled = len(self._tool_classes) > 0-- def _setup_structured_output(self) -> None:- """Set up structured output tracking for "tools" and "native" strategies.-- "tools" strategy for structured output:- 1. Converting response format schemas to LangChain tools- 2. Creating metadata for proper response reconstruction- 3. Handling both Pydantic models and dict schemas-- "native" strategy for structured output:- 1. Capturing the schema reference for later parsing- 2. Binding provider-native response_format kwargs at model bind time- 3. Parsing provider-enforced structured output directly into the schema- """- self.structured_output_tools: dict[str, OutputToolBinding[StructuredResponseT]] = {}- self.native_output_binding: ProviderStrategyBinding[StructuredResponseT] | None = None-- if self.response_format is not None:- response_format = self.response_format-- if isinstance(response_format, ToolStrategy):- # check if response_format.schema is a union- for response_schema in response_format.schema_specs:- structured_tool_info = OutputToolBinding.from_schema_spec(response_schema)- self.structured_output_tools[structured_tool_info.tool.name] = (- structured_tool_info- )- elif isinstance(response_format, ProviderStrategy):- # Use native strategy - create ProviderStrategyBinding for parsing- self.native_output_binding = ProviderStrategyBinding.from_schema_spec(- response_format.schema_spec- )- else:- # This shouldn't happen with the new ResponseFormat type, but keeping for safety- msg = (- f"Unsupported response_format type: {type(response_format)}. "- f"Expected ToolStrategy."- )- raise ValueError(msg)-- def _setup_state_schema(self) -> None:- """Setup state schema with validation."""- if self.state_schema is not None:- required_keys = {"messages", "remaining_steps"}- if self.response_format is not None:- required_keys.add("structured_response")-- schema_keys = set(get_type_hints(self.state_schema))- if missing_keys := required_keys - schema_keys:- msg = f"Missing required key(s) {missing_keys} in state_schema"- raise ValueError(msg)-- self._final_state_schema = self.state_schema- else:- self._final_state_schema = (- AgentStateWithStructuredResponse # type: ignore[assignment]- if self.response_format is not None- else AgentState- )-- def _handle_structured_response_tool_calls(self, response: AIMessage) -> Command | None:- """Handle tool calls that match structured output tools using the tools strategy.-- Args:- response: The AI message containing potential tool calls-- Returns:- Command with structured response update if found, None otherwise-- Raises:- MultipleStructuredOutputsError: If multiple structured responses are returned- and error handling is disabled- StructuredOutputParsingError: If parsing fails and error handling is disabled- """- if not isinstance(self.response_format, ToolStrategy) or not response.tool_calls:- return None-- structured_tool_calls = [- tool_call- for tool_call in response.tool_calls- if tool_call["name"] in self.structured_output_tools- ]-- if not structured_tool_calls:- return None-- if len(structured_tool_calls) > 1:- return self._handle_multiple_structured_outputs(response, structured_tool_calls)-- return self._handle_single_structured_output(response, structured_tool_calls[0])-- def _handle_multiple_structured_outputs(- self,- response: AIMessage,- structured_tool_calls: list[ToolCall],- ) -> Command:- """Handle multiple structured output tool calls."""- tool_names = [tool_call["name"] for tool_call in structured_tool_calls]- exception = MultipleStructuredOutputsError(tool_names)-- should_retry, error_message = self._handle_structured_output_error(exception)-- if not should_retry:- raise exception-- tool_messages = [- ToolMessage(- content=error_message,- tool_call_id=tool_call["id"],- name=tool_call["name"],- )- for tool_call in structured_tool_calls- ]-- return Command(- update={"messages": [response, *tool_messages]},- goto="agent",- )-- def _handle_single_structured_output(- self,- response: AIMessage,- tool_call: Any,- ) -> Command:- """Handle a single structured output tool call."""- structured_tool_binding = self.structured_output_tools[tool_call["name"]]-- try:- structured_response = structured_tool_binding.parse(tool_call["args"])-- if isinstance(structured_response, BaseModel):- structured_response_dict = structured_response.model_dump()- elif is_dataclass(structured_response):- structured_response_dict = asdict(structured_response) # type: ignore[arg-type]- else:- structured_response_dict = cast("dict", structured_response)-- tool_message_content = (- self.response_format.tool_message_content- if isinstance(self.response_format, ToolStrategy)- and self.response_format.tool_message_content- else f"Returning structured response: {structured_response_dict}"- )-- return Command(- update={- "messages": [- response,- ToolMessage(- content=tool_message_content,- tool_call_id=tool_call["id"],- name=tool_call["name"],- ),- ],- "structured_response": structured_response,- }- )- except Exception as exc: # noqa: BLE001- exception = StructuredOutputValidationError(tool_call["name"], exc)-- should_retry, error_message = self._handle_structured_output_error(exception)-- if not should_retry:- raise exception-- return Command(- update={- "messages": [- response,- ToolMessage(- content=error_message,- tool_call_id=tool_call["id"],- name=tool_call["name"],- ),- ],- },- goto="agent",- )-- def _handle_structured_output_error(- self,- exception: Exception,- ) -> tuple[bool, str]:- """Handle structured output error.-- Returns (should_retry, retry_tool_message).- """- handle_errors = cast("ToolStrategy", self.response_format).handle_errors-- if handle_errors is False:- return False, ""- if handle_errors is True:- return True, STRUCTURED_OUTPUT_ERROR_TEMPLATE.format(error=str(exception))- if isinstance(handle_errors, str):- return True, handle_errors- if isinstance(handle_errors, type) and issubclass(handle_errors, Exception):- if isinstance(exception, handle_errors):- return True, STRUCTURED_OUTPUT_ERROR_TEMPLATE.format(error=str(exception))- return False, ""- if isinstance(handle_errors, tuple):- if any(isinstance(exception, exc_type) for exc_type in handle_errors):- return True, STRUCTURED_OUTPUT_ERROR_TEMPLATE.format(error=str(exception))- return False, ""- if callable(handle_errors):- return True, handle_errors(exception) # type: ignore[call-arg, return-value]- return False, ""-- def _apply_native_output_binding(self, model: LanguageModelLike) -> LanguageModelLike:- """If native output is configured, bind provider-native kwargs onto the model."""- if not isinstance(self.response_format, ProviderStrategy):- return model- kwargs = self.response_format.to_model_kwargs()- return model.bind(**kwargs)-- def _handle_structured_response_native(self, response: AIMessage) -> Command | None:- """Handle structured output using the native output.-- If native output is configured and there are no tool calls,- parse using ProviderStrategyBinding.- """- if self.native_output_binding is None:- return None- if response.tool_calls:- # if the model chooses to call tools, we let the normal flow handle it- return None-- structured_response = self.native_output_binding.parse(response)-- return Command(update={"messages": [response], "structured_response": structured_response})-- def _setup_model(self) -> None:- """Setup model-related attributes."""- self._is_dynamic_model = not isinstance(self.model, (str, Runnable)) and callable(- self.model- )- self._is_async_dynamic_model = self._is_dynamic_model and inspect.iscoroutinefunction(- self.model- )-- if not self._is_dynamic_model:- model = self.model- if isinstance(model, str):- model = init_chat_model(model)-- # Collect all tools: regular tools + structured output tools- structured_output_tools = list(self.structured_output_tools.values())- all_tools = (- self._tool_classes- + self._llm_builtin_tools- + [info.tool for info in structured_output_tools]- )-- if len(all_tools) > 0:- # Check if we need to force tool use for structured output- tool_choice = None- if self.response_format is not None and isinstance(- self.response_format, ToolStrategy- ):- tool_choice = "any"-- if tool_choice:- model = cast("BaseChatModel", model).bind_tools( # type: ignore[assignment]- all_tools, tool_choice=tool_choice- )- # If native output is configured, bind tools with strict=True. Required for OpenAI.- elif isinstance(self.response_format, ProviderStrategy):- model = cast("BaseChatModel", model).bind_tools( # type: ignore[assignment]- all_tools, strict=True- )- else:- model = cast("BaseChatModel", model).bind_tools(all_tools) # type: ignore[assignment]-- # bind native structured-output kwargs- model = self._apply_native_output_binding(model) # type: ignore[assignment, arg-type]-- # Extract just the model part for direct invocation- self._static_model: Runnable | None = model # type: ignore[assignment]- else:- self._static_model = None-- def _resolve_model(self, state: StateT, runtime: Runtime[ContextT]) -> LanguageModelLike:- """Resolve the model to use, handling both static and dynamic models."""- if self._is_dynamic_model:- dynamic_model = self.model(state, runtime) # type: ignore[operator]- return self._apply_native_output_binding(dynamic_model) # type: ignore[arg-type]- return self._static_model # type: ignore[return-value]-- async def _aresolve_model(self, state: StateT, runtime: Runtime[ContextT]) -> LanguageModelLike:- """Async resolve the model to use, handling both static and dynamic models."""- if self._is_async_dynamic_model:- dynamic_model = cast(- "Callable[[StateT, Runtime[ContextT]], Awaitable[BaseChatModel]]",- self.model,- )- return await dynamic_model(state, runtime)- if self._is_dynamic_model:- dynamic_model = self.model(state, runtime) # type: ignore[assignment, operator]- return self._apply_native_output_binding(dynamic_model) # type: ignore[arg-type]- return self._static_model # type: ignore[return-value]-- def create_model_node(self) -> RunnableCallable:- """Create the 'agent' node that calls the LLM."""-- def _get_model_input_state(state: StateT) -> StateT:- if self.pre_model_hook is not None:- messages = _get_state_value(state, "llm_input_messages") or _get_state_value(- state, "messages"- )- error_msg = (- f"Expected input to call_model to have 'llm_input_messages' "- f"or 'messages' key, but got {state}"- )- else:- messages = _get_state_value(state, "messages")- error_msg = f"Expected input to call_model to have 'messages' key, but got {state}"-- if messages is None:- raise ValueError(error_msg)-- _validate_chat_history(messages)-- if isinstance(self._final_state_schema, type) and issubclass(- self._final_state_schema, BaseModel- ):- # we're passing messages under `messages` key, as this- # is expected by the prompt- state.messages = messages # type: ignore[union-attr]- else:- state["messages"] = messages # type: ignore[index]- return state-- def _are_more_steps_needed(state: StateT, response: BaseMessage) -> bool:- has_tool_calls = isinstance(response, AIMessage) and response.tool_calls- all_tools_return_direct = (- all(call["name"] in self._should_return_direct for call in response.tool_calls)- if isinstance(response, AIMessage)- else False- )- remaining_steps = _get_state_value(state, "remaining_steps", None)- return (- remaining_steps is not None # type: ignore[return-value]- and (- (remaining_steps < 1 and all_tools_return_direct)- or (remaining_steps < 2 and has_tool_calls)- )- )-- def call_model(- state: StateT, runtime: Runtime[ContextT], config: RunnableConfig- ) -> dict[str, Any] | Command:- """Call the model with the current state and return the response."""- if self._is_async_dynamic_model:- msg = (- "Async model callable provided but agent invoked synchronously. "- "Use agent.ainvoke() or agent.astream(), or provide a sync model callable."- )- raise RuntimeError(msg)-- model_input = _get_model_input_state(state)- model = self._resolve_model(state, runtime)-- # Get prompt runnable and invoke it first to prepare messages- prompt_runnable = _get_prompt_runnable(self.prompt)- prepared_messages = prompt_runnable.invoke(model_input, config)-- # Then invoke the model with the prepared messages- response = cast("AIMessage", model.invoke(prepared_messages, config))- response.name = self.name-- if _are_more_steps_needed(state, response):- return {- "messages": [- AIMessage(- id=response.id,- content="Sorry, need more steps to process this request.",- )- ]- }-- # Check if any tool calls match structured output tools- structured_command = self._handle_structured_response_tool_calls(response)- if structured_command:- return structured_command-- # Native structured output- native_command = self._handle_structured_response_native(response)- if native_command:- return native_command-- return {"messages": [response]}-- async def acall_model(- state: StateT, runtime: Runtime[ContextT], config: RunnableConfig- ) -> dict[str, Any] | Command:- """Call the model with the current state and return the response."""- model_input = _get_model_input_state(state)-- model = await self._aresolve_model(state, runtime)-- # Get prompt runnable and invoke it first to prepare messages- prompt_runnable = _get_prompt_runnable(self.prompt)- prepared_messages = await prompt_runnable.ainvoke(model_input, config)-- # Then invoke the model with the prepared messages- response = cast(- "AIMessage",- await model.ainvoke(prepared_messages, config),- )- response.name = self.name- if _are_more_steps_needed(state, response):- return {- "messages": [- AIMessage(- id=response.id,- content="Sorry, need more steps to process this request.",- )- ]- }-- # Check if any tool calls match structured output tools- structured_command = self._handle_structured_response_tool_calls(response)- if structured_command:- return structured_command-- # Native structured output- native_command = self._handle_structured_response_native(response)- if native_command:- return native_command-- return {"messages": [response]}-- return RunnableCallable(call_model, acall_model)-- def _get_input_schema(self) -> type[StateT]:- """Get input schema for model node."""- if self.pre_model_hook is not None:- if isinstance(self._final_state_schema, type) and issubclass(- self._final_state_schema, BaseModel- ):- from pydantic import create_model-- return create_model(- "CallModelInputSchema",- llm_input_messages=(list[AnyMessage], ...),- __base__=self._final_state_schema,- )-- class CallModelInputSchema(self._final_state_schema): # type: ignore[name-defined, misc]- llm_input_messages: list[AnyMessage]-- return CallModelInputSchema- return self._final_state_schema-- def create_model_router(self) -> Callable[[StateT], str | list[Send]]:- """Create routing function for model node conditional edges."""-- def should_continue(state: StateT) -> str | list[Send]:- messages = _get_state_value(state, "messages")- last_message = messages[-1]-- # Check if the last message is a ToolMessage from a structured tool.- # This condition exists to support structured output via tools.- # Once a tool has been called for structured output, we skip- # tool execution and go to END (if there is no post_model_hook).- if (- isinstance(last_message, ToolMessage)- and last_message.name in self.structured_output_tools- ):- return END-- if isinstance(last_message, ToolMessage):- return END-- if not isinstance(last_message, AIMessage) or not last_message.tool_calls:- if self.post_model_hook is not None:- return "post_model_hook"- return END- if self.version == "v1":- return "tools"- if self.version == "v2":- if self.post_model_hook is not None:- return "post_model_hook"- tool_calls = [- self._tool_node.inject_tool_args(call, state, self.store) # type: ignore[arg-type]- for call in last_message.tool_calls- ]- return [Send("tools", [tool_call]) for tool_call in tool_calls]- return None-- return should_continue-- def create_post_model_hook_router(- self,- ) -> Callable[[StateT], str | list[Send]]:- """Create a routing function for post_model_hook node conditional edges."""-- def post_model_hook_router(state: StateT) -> str | list[Send]:- messages = _get_state_value(state, "messages")-- # Check if the last message is a ToolMessage from a structured tool.- # This condition exists to support structured output via tools.- # Once a tool has been called for structured output, we skip- # tool execution and go to END (if there is no post_model_hook).- last_message = messages[-1]- if (- isinstance(last_message, ToolMessage)- and last_message.name in self.structured_output_tools- ):- return END-- tool_messages = [m.tool_call_id for m in messages if isinstance(m, ToolMessage)]- last_ai_message = next(m for m in reversed(messages) if isinstance(m, AIMessage))- pending_tool_calls = [- c for c in last_ai_message.tool_calls if c["id"] not in tool_messages- ]-- if pending_tool_calls:- pending_tool_calls = [- self._tool_node.inject_tool_args(call, state, self.store) # type: ignore[arg-type]- for call in pending_tool_calls- ]- return [Send("tools", [tool_call]) for tool_call in pending_tool_calls]- if isinstance(messages[-1], ToolMessage):- return self._get_entry_point()- return END-- return post_model_hook_router-- def create_tools_router(self) -> Callable[[StateT], str] | None:- """Create a routing function for tools node conditional edges."""- if not self._should_return_direct:- return None-- def route_tool_responses(state: StateT) -> str:- messages = _get_state_value(state, "messages")- for m in reversed(messages):- if not isinstance(m, ToolMessage):- break- if m.name in self._should_return_direct:- return END-- if (- isinstance(m, AIMessage)- and m.tool_calls- and any(call["name"] in self._should_return_direct for call in m.tool_calls)- ):- return END-- return self._get_entry_point()-- return route_tool_responses-- def _get_entry_point(self) -> str:- """Get the workflow entry point."""- return "pre_model_hook" if self.pre_model_hook else "agent"-- def _get_model_paths(self) -> list[str]:- """Get possible edge destinations from model node."""- paths = []- if self._tool_calling_enabled:- paths.append("tools")- if self.post_model_hook:- paths.append("post_model_hook")- else:- paths.append(END)-- return paths-- def _get_post_model_hook_paths(self) -> list[str]:- """Get possible edge destinations from post_model_hook node."""- paths = []- if self._tool_calling_enabled:- paths = [self._get_entry_point(), "tools"]- paths.append(END)- return paths-- def build(self) -> StateGraph[StateT, ContextT]:- """Build the agent workflow graph (uncompiled)."""- workflow = StateGraph(- state_schema=self._final_state_schema,- context_schema=self.context_schema,- )-- # Set entry point- workflow.set_entry_point(self._get_entry_point())-- # Add nodes- workflow.add_node("agent", self.create_model_node(), input_schema=self._get_input_schema())-- if self._tool_calling_enabled:- workflow.add_node("tools", self._tool_node)-- if self.pre_model_hook:- workflow.add_node("pre_model_hook", self.pre_model_hook) # type: ignore[arg-type]-- if self.post_model_hook:- workflow.add_node("post_model_hook", self.post_model_hook) # type: ignore[arg-type]-- # Add edges- if self.pre_model_hook:- workflow.add_edge("pre_model_hook", "agent")-- if self.post_model_hook:- workflow.add_edge("agent", "post_model_hook")- post_hook_paths = self._get_post_model_hook_paths()- if len(post_hook_paths) == 1:- # No need for a conditional edge if there's only one path- workflow.add_edge("post_model_hook", post_hook_paths[0])- else:- workflow.add_conditional_edges(- "post_model_hook",- self.create_post_model_hook_router(),- path_map=post_hook_paths,- )- else:- model_paths = self._get_model_paths()- if len(model_paths) == 1:- # No need for a conditional edge if there's only one path- workflow.add_edge("agent", model_paths[0])- else:- workflow.add_conditional_edges(- "agent",- self.create_model_router(),- path_map=model_paths,- )-- if self._tool_calling_enabled:- # In some cases, tools can return directly. In these cases- # we add a conditional edge from the tools node to the END node- # instead of going to the entry point.- tools_router = self.create_tools_router()- if tools_router:- workflow.add_conditional_edges(- "tools",- tools_router,- path_map=[self._get_entry_point(), END],- )- else:- workflow.add_edge("tools", self._get_entry_point())-- return workflow---def _supports_native_structured_output(- model: str | BaseChatModel | SyncOrAsync[[StateT, Runtime[ContextT]], BaseChatModel],-) -> bool:- """Check if a model supports native structured output.-- TODO: replace with more robust model profiles.- """- model_name: str | None = None- if isinstance(model, str):- model_name = model- elif isinstance(model, BaseChatModel):- model_name = getattr(model, "model_name", None)-- return (- "grok" in model_name.lower()- or any(part in model_name for part in ["gpt-5", "gpt-4.1", "gpt-oss", "o3-pro", "o3-mini"])- if model_name- else False- )---def create_agent( # noqa: D417- model: str | BaseChatModel | SyncOrAsync[[StateT, Runtime[ContextT]], BaseChatModel],- tools: Sequence[BaseTool | Callable | dict[str, Any]] | ToolNode,- *,- middleware: Sequence[AgentMiddleware] = (),- prompt: Prompt | None = None,- response_format: ToolStrategy[StructuredResponseT]- | ProviderStrategy[StructuredResponseT]- | type[StructuredResponseT]- | None = None,- pre_model_hook: RunnableLike | None = None,- post_model_hook: RunnableLike | None = None,- state_schema: type[StateT] | None = None,- context_schema: type[ContextT] | None = None,- checkpointer: Checkpointer | None = None,- store: BaseStore | None = None,- interrupt_before: list[str] | None = None,- interrupt_after: list[str] | None = None,- debug: bool = False,- version: Literal["v1", "v2"] = "v2",- name: str | None = None,- **deprecated_kwargs: Any,-) -> CompiledStateGraph[StateT, ContextT]:- """Creates an agent graph that calls tools in a loop until a stopping condition is met.-- For more details on using `create_agent`,- visit [Agents](https://langchain-ai.github.io/langgraph/agents/overview/) documentation.-- Args:- model: The language model for the agent. Supports static and dynamic- model selection.-- - **Static model**: A chat model instance (e.g., `ChatOpenAI()`) or- string identifier (e.g., `"openai:gpt-4"`)- - **Dynamic model**: A callable with signature- `(state, runtime) -> BaseChatModel` that returns different models- based on runtime context- If the model has tools bound via `.bind_tools()` or other configurations,- the return type should be a Runnable[LanguageModelInput, BaseMessage]- Coroutines are also supported, allowing for asynchronous model selection.-- Dynamic functions receive graph state and runtime, enabling- context-dependent model selection. Must return a `BaseChatModel`- instance. For tool calling, bind tools using `.bind_tools()`.- Bound tools must be a subset of the `tools` parameter.-- Dynamic model example:- ```python- from dataclasses import dataclass--- @dataclass- class ModelContext:- model_name: str = "gpt-3.5-turbo"--- # Instantiate models globally- gpt4_model = ChatOpenAI(model="gpt-4")- gpt35_model = ChatOpenAI(model="gpt-3.5-turbo")--- def select_model(state: AgentState, runtime: Runtime[ModelContext]) -> ChatOpenAI:- model_name = runtime.context.model_name- model = gpt4_model if model_name == "gpt-4" else gpt35_model- return model.bind_tools(tools)- ```-- !!! note- Ensure returned models have appropriate tools bound via- `.bind_tools()` and support required functionality. Bound tools- must be a subset of those specified in the `tools` parameter.-- tools: A list of tools or a ToolNode instance.- If an empty list is provided, the agent will consist of a single LLM node- without tool calling.- prompt: An optional prompt for the LLM. Can take a few different forms:-- - str: This is converted to a SystemMessage and added to the beginning- of the list of messages in state["messages"].- - SystemMessage: this is added to the beginning of the list of messages- in state["messages"].- - Callable: This function should take in full graph state and the output is- then passed to the language model.- - Runnable: This runnable should take in full graph state and the output is- then passed to the language model.-- response_format: An optional UsingToolStrategy configuration for structured responses.-- If provided, the agent will handle structured output via tool calls- during the normal conversation flow.- When the model calls a structured output tool, the response will be captured- and returned in the 'structured_response' state key.- If not provided, `structured_response` will not be present in the output state.-- The UsingToolStrategy should contain:-- - schemas: A sequence of ResponseSchema objects that define- the structured output format- - tool_choice: Either "required" or "auto" to control when structured- output is used-- Each ResponseSchema contains:-- - schema: A Pydantic model that defines the structure- - name: Optional custom name for the tool (defaults to model name)- - description: Optional custom description (defaults to model docstring)- - strict: Whether to enforce strict validation-- !!! important- `response_format` requires the model to support tool calling-- !!! note- Structured responses are handled directly in the model call node via- tool calls, eliminating the need for separate structured response nodes.-- pre_model_hook: An optional node to add before the `agent` node- (i.e., the node that calls the LLM).- Useful for managing long message histories- (e.g., message trimming, summarization, etc.).- Pre-model hook must be a callable or a runnable that takes in current- graph state and returns a state update in the form of- ```python- # At least one of `messages` or `llm_input_messages` MUST be provided- {- # If provided, will UPDATE the `messages` in the state- "messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES), ...],- # If provided, will be used as the input to the LLM,- # and will NOT UPDATE `messages` in the state- "llm_input_messages": [...],- # Any other state keys that need to be propagated- ...- }- ```-- !!! important- At least one of `messages` or `llm_input_messages` MUST be provided- and will be used as an input to the `agent` node.- The rest of the keys will be added to the graph state.-- !!! warning- If you are returning `messages` in the pre-model hook,- you should OVERWRITE the `messages` key by doing the following:-- ```python- {- "messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES), *new_messages]- ...- }- ```- post_model_hook: An optional node to add after the `agent` node- (i.e., the node that calls the LLM).- Useful for implementing human-in-the-loop, guardrails, validation,- or other post-processing.- Post-model hook must be a callable or a runnable that takes in- current graph state and returns a state update.-- !!! note- Only available with `version="v2"`.- state_schema: An optional state schema that defines graph state.- Must have `messages` and `remaining_steps` keys.- Defaults to `AgentState` that defines those two keys.- context_schema: An optional schema for runtime context.- checkpointer: An optional checkpoint saver object. This is used for persisting- the state of the graph (e.g., as chat memory) for a single thread- (e.g., a single conversation).- store: An optional store object. This is used for persisting data- across multiple threads (e.g., multiple conversations / users).- interrupt_before: An optional list of node names to interrupt before.- Should be one of the following: "agent", "tools".- This is useful if you want to add a user confirmation or other interrupt- before taking an action.- interrupt_after: An optional list of node names to interrupt after.- Should be one of the following: "agent", "tools".- This is useful if you want to return directly or run additional processing on an output.- debug: A flag indicating whether to enable debug mode.- version: Determines the version of the graph to create.- Can be one of:-- - `"v1"`: The tool node processes a single message. All tool- calls in the message are executed in parallel within the tool node.- - `"v2"`: The tool node processes a tool call.- Tool calls are distributed across multiple instances of the tool- node using the [Send](https://langchain-ai.github.io/langgraph/concepts/low_level/#send)- API.- 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.-- !!! warning- 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.-- The "agent" node calls the language model with the messages list (after applying the prompt).- If the resulting AIMessage contains `tool_calls`,- the graph will then call the ["tools"][langgraph.prebuilt.tool_node.ToolNode].- The "tools" node executes the tools (1 tool per `tool_call`)- and adds the responses to the messages list as `ToolMessage` objects.- The agent node then calls the language model again.- The process repeats until no more `tool_calls` are present in the response.- The agent then returns the full list of messages as a dictionary containing the key "messages".-- ``` mermaid- sequenceDiagram- participant U as User- participant A as LLM- participant T as Tools- U->>A: Initial input- Note over A: Prompt + LLM- loop while tool_calls present- A->>T: Execute tools- T-->>A: ToolMessage for each tool_calls- end- A->>U: Return final state- ```-- Example:- ```python- from langchain.agents import create_agent-- def check_weather(location: str) -> str:- '''Return the weather forecast for the specified location.'''- return f"It's always sunny in {location}"-- graph = create_agent(- "anthropic:claude-3-7-sonnet-latest",- tools=[check_weather],- prompt="You are a helpful assistant",- )- inputs = {"messages": [{"role": "user", "content": "what is the weather in sf"}]}- for chunk in graph.stream(inputs, stream_mode="updates"):- print(chunk)- ```- """- if middleware:- assert isinstance(model, str | BaseChatModel) # noqa: S101- assert isinstance(prompt, str | None) # noqa: S101- assert not isinstance(response_format, tuple) # noqa: S101- assert pre_model_hook is None # noqa: S101- assert post_model_hook is None # noqa: S101- assert state_schema is None # noqa: S101- return create_middleware_agent( # type: ignore[return-value]- model=model,- tools=tools,- system_prompt=prompt,- middleware=middleware,- response_format=response_format,- context_schema=context_schema,- ).compile(- checkpointer=checkpointer,- store=store,- name=name,- interrupt_after=interrupt_after,- interrupt_before=interrupt_before,- debug=debug,- )-- # Handle deprecated config_schema parameter- if (config_schema := deprecated_kwargs.pop("config_schema", MISSING)) is not MISSING:- warn(- "`config_schema` is deprecated and will be removed. "- "Please use `context_schema` instead.",- category=DeprecationWarning,- stacklevel=2,- )- if context_schema is None:- context_schema = config_schema-- if len(deprecated_kwargs) > 0:- msg = f"create_agent() got unexpected keyword arguments: {deprecated_kwargs}"- raise TypeError(msg)-- if response_format and not isinstance(response_format, (ToolStrategy, ProviderStrategy)):- if _supports_native_structured_output(model):- response_format = ProviderStrategy(- schema=response_format,- )- else:- response_format = ToolStrategy(- schema=response_format,- )- elif isinstance(response_format, tuple) and len(response_format) == 2:- msg = "Passing a 2-tuple as response_format is no longer supported. "- raise ValueError(msg)-- # Create and configure the agent builder- builder = _AgentBuilder(- model=model,- tools=tools,- prompt=prompt,- response_format=cast("ResponseFormat[StructuredResponseT] | None", response_format),- pre_model_hook=pre_model_hook,- post_model_hook=post_model_hook,- state_schema=state_schema,- context_schema=context_schema,- version=version,- name=name,- store=store,- )-- # Build and compile the workflow- workflow = builder.build()- return workflow.compile( # type: ignore[return-value]- checkpointer=checkpointer,- store=store,- interrupt_before=interrupt_before,- interrupt_after=interrupt_after,- debug=debug,- name=name,- )---__all__ = [- "AgentState",- "AgentStatePydantic",- "AgentStateWithStructuredResponse",- "AgentStateWithStructuredResponsePydantic",- "create_agent",-]diff --git a/libs/langchain_v1/tests/unit_tests/agents/__snapshots__/test_react_agent_graph.ambr b/libs/langchain_v1/tests/unit_tests/agents/__snapshots__/test_react_agent_graph.ambrdeleted file mode 100644index 49e40649e5..0000000000--- a/libs/langchain_v1/tests/unit_tests/agents/__snapshots__/test_react_agent_graph.ambr+++ /dev/null@@ -1,83 +0,0 @@-# serializer version: 1-# name: test_react_agent_graph_structure[None-None-tools0]- '''- graph TD;- __start__ --> agent;- agent --> __end__;-- '''-# ----# name: test_react_agent_graph_structure[None-None-tools1]- '''- graph TD;- __start__ --> agent;- agent -.-> __end__;- agent -.-> tools;- tools --> agent;-- '''-# ----# name: test_react_agent_graph_structure[None-pre_model_hook-tools0]- '''- graph TD;- __start__ --> pre_model_hook;- pre_model_hook --> agent;- agent --> __end__;-- '''-# ----# name: test_react_agent_graph_structure[None-pre_model_hook-tools1]- '''- graph TD;- __start__ --> pre_model_hook;- agent -.-> __end__;- agent -.-> tools;- pre_model_hook --> agent;- tools --> pre_model_hook;-- '''-# ----# name: test_react_agent_graph_structure[post_model_hook-None-tools0]- '''- graph TD;- __start__ --> agent;- agent --> post_model_hook;- post_model_hook --> __end__;-- '''-# ----# name: test_react_agent_graph_structure[post_model_hook-None-tools1]- '''- graph TD;- __start__ --> agent;- agent --> post_model_hook;- post_model_hook -.-> __end__;- post_model_hook -.-> agent;- post_model_hook -.-> tools;- tools --> agent;-- '''-# ----# name: test_react_agent_graph_structure[post_model_hook-pre_model_hook-tools0]- '''- graph TD;- __start__ --> pre_model_hook;- agent --> post_model_hook;- pre_model_hook --> agent;- post_model_hook --> __end__;-- '''-# ----# name: test_react_agent_graph_structure[post_model_hook-pre_model_hook-tools1]- '''- graph TD;- __start__ --> pre_model_hook;- agent --> post_model_hook;- post_model_hook -.-> __end__;- post_model_hook -.-> pre_model_hook;- post_model_hook -.-> tools;- pre_model_hook --> agent;- tools --> pre_model_hook;-- '''-# ---diff --git a/libs/langchain_v1/tests/unit_tests/agents/model.py b/libs/langchain_v1/tests/unit_tests/agents/model.pyindex 70cbbbafe0..07ed23995e 100644--- a/libs/langchain_v1/tests/unit_tests/agents/model.py+++ b/libs/langchain_v1/tests/unit_tests/agents/model.py@@ -108,4 +108,4 @@ class FakeToolCallingModel(BaseChatModel, Generic[StructuredResponseT]):})- return self.bind(tools=tool_dicts)+ return self.bind(tools=tool_dicts, **kwargs)diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.pyindex ee34a47d7d..bee3b1277f 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py@@ -36,7 +36,7 @@ from langchain.agents.middleware.planning import (write_todos,WRITE_TODOS_TOOL_DESCRIPTION,)-from langchain.agents.middleware.call_tracking import (+from langchain.agents.middleware.model_call_limit import (ModelCallLimitMiddleware,ModelCallLimitExceededError,)@@ -52,7 +52,7 @@ from langchain.agents.middleware.types import (OmitFromOutput,PrivateStateAttr,)-from langchain.agents.middleware_agent import create_agent+from langchain.agents.factory import create_agentfrom langchain.agents.structured_output import ToolStrategyfrom langchain.tools import InjectedState@@ -134,7 +134,7 @@ def test_create_agent_diagram(system_prompt="You are a helpful assistant.",)- assert agent_zero.compile().get_graph().draw_mermaid() == snapshot+ assert agent_zero.get_graph().draw_mermaid() == snapshotagent_one = create_agent(model=FakeToolCallingModel(),@@ -143,7 +143,7 @@ def test_create_agent_diagram(middleware=[NoopOne()],)- assert agent_one.compile().get_graph().draw_mermaid() == snapshot+ assert agent_one.get_graph().draw_mermaid() == snapshotagent_two = create_agent(model=FakeToolCallingModel(),@@ -152,7 +152,7 @@ def test_create_agent_diagram(middleware=[NoopOne(), NoopTwo()],)- assert agent_two.compile().get_graph().draw_mermaid() == snapshot+ assert agent_two.get_graph().draw_mermaid() == snapshotagent_three = create_agent(model=FakeToolCallingModel(),@@ -161,7 +161,7 @@ def test_create_agent_diagram(middleware=[NoopOne(), NoopTwo(), NoopThree()],)- assert agent_three.compile().get_graph().draw_mermaid() == snapshot+ assert agent_three.get_graph().draw_mermaid() == snapshotagent_four = create_agent(model=FakeToolCallingModel(),@@ -170,7 +170,7 @@ def test_create_agent_diagram(middleware=[NoopFour()],)- assert agent_four.compile().get_graph().draw_mermaid() == snapshot+ assert agent_four.get_graph().draw_mermaid() == snapshotagent_five = create_agent(model=FakeToolCallingModel(),@@ -179,7 +179,7 @@ def test_create_agent_diagram(middleware=[NoopFour(), NoopFive()],)- assert agent_five.compile().get_graph().draw_mermaid() == snapshot+ assert agent_five.get_graph().draw_mermaid() == snapshotagent_six = create_agent(model=FakeToolCallingModel(),@@ -188,7 +188,7 @@ def test_create_agent_diagram(middleware=[NoopFour(), NoopFive(), NoopSix()],)- assert agent_six.compile().get_graph().draw_mermaid() == snapshot+ assert agent_six.get_graph().draw_mermaid() == snapshotagent_seven = create_agent(model=FakeToolCallingModel(),@@ -197,7 +197,7 @@ def test_create_agent_diagram(middleware=[NoopSeven()],)- assert agent_seven.compile().get_graph().draw_mermaid() == snapshot+ assert agent_seven.get_graph().draw_mermaid() == snapshotagent_eight = create_agent(model=FakeToolCallingModel(),@@ -206,7 +206,7 @@ def test_create_agent_diagram(middleware=[NoopSeven(), NoopEight()],)- assert agent_eight.compile().get_graph().draw_mermaid() == snapshot+ assert agent_eight.get_graph().draw_mermaid() == snapshotagent_nine = create_agent(model=FakeToolCallingModel(),@@ -215,7 +215,7 @@ def test_create_agent_diagram(middleware=[NoopSeven(), NoopEight(), NoopNine()],)- assert agent_nine.compile().get_graph().draw_mermaid() == snapshot+ assert agent_nine.get_graph().draw_mermaid() == snapshotagent_ten = create_agent(model=FakeToolCallingModel(),@@ -224,7 +224,7 @@ def test_create_agent_diagram(middleware=[NoopTen()],)- assert agent_ten.compile().get_graph().draw_mermaid() == snapshot+ assert agent_ten.get_graph().draw_mermaid() == snapshotagent_eleven = create_agent(model=FakeToolCallingModel(),@@ -233,7 +233,7 @@ def test_create_agent_diagram(middleware=[NoopTen(), NoopEleven()],)- assert agent_eleven.compile().get_graph().draw_mermaid() == snapshot+ assert agent_eleven.get_graph().draw_mermaid() == snapshotdef test_create_agent_invoke(@@ -282,7 +282,8 @@ def test_create_agent_invoke(tools=[my_tool],system_prompt="You are a helpful assistant.",middleware=[NoopSeven(), NoopEight()],- ).compile(checkpointer=sync_checkpointer)+ checkpointer=sync_checkpointer,+ )thread1 = {"configurable": {"thread_id": "1"}}assert agent_one.invoke({"messages": ["hello"]}, thread1) == {@@ -371,7 +372,8 @@ def test_create_agent_jump(tools=[my_tool],system_prompt="You are a helpful assistant.",middleware=[NoopSeven(), NoopEight()],- ).compile(checkpointer=sync_checkpointer)+ checkpointer=sync_checkpointer,+ )if isinstance(sync_checkpointer, InMemorySaver):assert agent_one.get_graph().draw_mermaid() == snapshot@@ -1260,14 +1262,13 @@ def test_modify_model_request() -> None:request.messages.append(HumanMessage("remember to be nice!"))return request- builder = create_agent(+ agent = create_agent(model=FakeToolCallingModel(),tools=[],system_prompt="You are a helpful assistant.",middleware=[ModifyMiddleware()],)- agent = builder.compile()result = agent.invoke({"messages": [HumanMessage("Hello")]})assert result["messages"][0].content == "Hello"assert result["messages"][1].content == "remember to be nice!"@@ -1314,9 +1315,8 @@ def test_tools_to_model_edge_with_structured_and_regular_tool_calls():response_format=ToolStrategy(schema=WeatherResponse),)- # Compile and invoke the agent- compiled_agent = agent.compile()- result = compiled_agent.invoke(+ # Invoke the agent (already compiled)+ result = agent.invoke({"messages": [HumanMessage("What's the weather and help me with a query?")]})@@ -1369,7 +1369,6 @@ def test_public_private_state_for_custom_middleware() -> None:return {"omit_input": "test", "omit_output": "test", "private_state": "test"}agent = create_agent(model=FakeToolCallingModel(), middleware=[CustomMiddleware()])- agent = agent.compile()result = agent.invoke({"messages": [HumanMessage("Hello")],@@ -1404,7 +1403,6 @@ def test_runtime_injected_into_middleware() -> None:middleware = CustomMiddleware()agent = create_agent(model=FakeToolCallingModel(), middleware=[CustomMiddleware()])- agent = agent.compile()agent.invoke({"messages": [HumanMessage("Hello")]})@@ -1435,7 +1433,7 @@ def test_injected_state_in_middleware_agent() -> None:tools=[test_state],system_prompt="You are a helpful assistant.",middleware=[TestMiddleware()],- ).compile()+ )result = agent.invoke({"test_state": "I love pizza", "messages": [HumanMessage("Call the test state tool")]}@@ -1465,7 +1463,6 @@ def test_jump_to_is_ephemeral() -> None:return {"jump_to": "model"}agent = create_agent(model=FakeToolCallingModel(), middleware=[MyMiddleware()])- agent = agent.compile()result = agent.invoke({"messages": [HumanMessage("Hello")]})assert "jump_to" not in result@@ -1596,7 +1593,6 @@ def test_planning_middleware_agent_creation_with_middleware() -> None:)middleware = PlanningMiddleware()agent = create_agent(model=model, middleware=[middleware])- agent = agent.compile()result = agent.invoke({"messages": [HumanMessage("Hello")]})assert result["todos"] == [{"content": "Task 1", "status": "completed"}]@@ -1707,7 +1703,6 @@ def test_planning_middleware_custom_system_prompt() -> None:)agent = create_agent(model=model, middleware=[middleware])- agent = agent.compile()result = agent.invoke({"messages": [HumanMessage("Hello")]})assert result["todos"] == [{"content": "Custom task", "status": "pending"}]@@ -1781,7 +1776,8 @@ def test_thread_limit_with_create_agent():model=model,tools=[simple_tool],middleware=[ModelCallLimitMiddleware(thread_limit=1)],- ).compile(checkpointer=InMemorySaver())+ checkpointer=InMemorySaver(),+ )# First invocation should work - 1 model call, within thread limitresult = agent.invoke(@@ -1824,7 +1820,8 @@ def test_run_limit_with_create_agent():model=model,tools=[simple_tool],middleware=[ModelCallLimitMiddleware(run_limit=1)],- ).compile(checkpointer=InMemorySaver())+ checkpointer=InMemorySaver(),+ )# This should hit the run limit after the first model callresult = agent.invoke(@@ -1911,8 +1908,7 @@ def test_run_limit_resets_between_invocations() -> None:tool_calls=[[], [], [], []]) # No tool calls, so only model call per run- agent = create_agent(model=model, middleware=[middleware])- agent = agent.compile(checkpointer=InMemorySaver())+ agent = create_agent(model=model, middleware=[middleware], checkpointer=InMemorySaver())thread_config = {"configurable": {"thread_id": "test_thread"}}agent.invoke({"messages": [HumanMessage("Hello")]}, thread_config)@@ -1959,7 +1955,7 @@ async def test_create_agent_async_invoke() -> None:tools=[my_tool],system_prompt="You are a helpful assistant.",middleware=[AsyncMiddleware()],- ).compile()+ )result = await agent.ainvoke({"messages": [HumanMessage("hello")]})@@ -2015,7 +2011,7 @@ async def test_create_agent_async_invoke_multiple_middleware() -> None:tools=[],system_prompt="You are a helpful assistant.",middleware=[AsyncMiddlewareOne(), AsyncMiddlewareTwo()],- ).compile()+ )result = await agent.ainvoke({"messages": [HumanMessage("hello")]})@@ -2056,7 +2052,7 @@ async def test_create_agent_async_jump() -> None:tools=[my_tool],system_prompt="You are a helpful assistant.",middleware=[AsyncMiddlewareOne(), AsyncMiddlewareTwo()],- ).compile()+ )result = await agent.ainvoke({"messages": []})@@ -2095,7 +2091,7 @@ async def test_create_agent_mixed_sync_async_middleware() -> None:tools=[],system_prompt="You are a helpful assistant.",middleware=[SyncMiddleware(), AsyncMiddleware()],- ).compile()+ )result = await agent.ainvoke({"messages": [HumanMessage("hello")]})@@ -2143,7 +2139,7 @@ def test_retry_model_request_hook() -> None:failing_model = FailingModel()retry_middleware = RetryMiddleware()- agent = create_agent(model=failing_model, middleware=[retry_middleware]).compile()+ agent = create_agent(model=failing_model, middleware=[retry_middleware])result = agent.invoke({"messages": [HumanMessage("Test")]})@@ -2181,7 +2177,7 @@ def test_retry_model_request_attempt_number() -> None:model = AlwaysFailingModel()tracker = AttemptTrackingMiddleware()- agent = create_agent(model=model, middleware=[tracker]).compile()+ agent = create_agent(model=model, middleware=[tracker])with pytest.raises(ValueError, match="Always fails"):agent.invoke({"messages": [HumanMessage("Test")]})@@ -2208,7 +2204,7 @@ def test_retry_model_request_no_retry() -> None:# Always return None to not retryreturn None- agent = create_agent(model=FailingModel(), middleware=[NoRetryMiddleware()]).compile()+ agent = create_agent(model=FailingModel(), middleware=[NoRetryMiddleware()])with pytest.raises(ValueError, match="Model error"):agent.invoke({"messages": [HumanMessage("Test")]})@@ -2245,7 +2241,7 @@ def test_model_fallback_middleware() -> None:# Only pass fallback models to middleware (not the primary)fallback_middleware = ModelFallbackMiddleware(fallback)- agent = create_agent(model=primary, middleware=[fallback_middleware]).compile()+ agent = create_agent(model=primary, middleware=[fallback_middleware])result = agent.invoke({"messages": [HumanMessage("Test")]})@@ -2278,7 +2274,7 @@ def test_model_fallback_middleware_exhausted() -> None:# Primary fails (attempt 1), then fallback1 (attempt 2), then fallback2 (attempt 3)fallback_middleware = ModelFallbackMiddleware(fallback1, fallback2)- agent = create_agent(model=primary, middleware=[fallback_middleware]).compile()+ agent = create_agent(model=primary, middleware=[fallback_middleware])# Should fail with the last fallback's errorwith pytest.raises(ValueError, match="fallback2 failed"):@@ -2328,7 +2324,7 @@ def test_retry_model_request_max_attempts() -> None:model = AlwaysFailingModel()middleware = InfiniteRetryMiddleware()- agent = create_agent(model=model, middleware=[middleware]).compile()+ agent = create_agent(model=model, middleware=[middleware])# Should fail with max attempts error, not infinite loopwith pytest.raises(RuntimeError, match="Maximum retry attempts \\(100\\) exceeded"):@@ -2372,7 +2368,7 @@ async def test_retry_model_request_async() -> None:failing_model = AsyncFailingModel()retry_middleware = AsyncRetryMiddleware()- agent = create_agent(model=failing_model, middleware=[retry_middleware]).compile()+ agent = create_agent(model=failing_model, middleware=[retry_middleware])result = await agent.ainvoke({"messages": [HumanMessage("Test")]})@@ -2394,7 +2390,7 @@ def test_create_agent_sync_invoke_with_only_async_middleware_raises_error() -> Ntools=[],system_prompt="You are a helpful assistant.",middleware=[AsyncOnlyMiddleware()],- ).compile()+ )with pytest.raises(TypeError,@@ -2427,7 +2423,7 @@ def test_create_agent_sync_invoke_with_mixed_middleware() -> None:tools=[],system_prompt="You are a helpful assistant.",middleware=[MixedMiddleware()],- ).compile()+ )result = agent.invoke({"messages": [HumanMessage("hello")]})diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.pyindex 536d5fdc6e..700f3939cf 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_decorators.py@@ -20,7 +20,7 @@ from langchain.agents.middleware.types import (modify_model_request,hook_config,)-from langchain.agents.middleware_agent import create_agent, _get_can_jump_to+from langchain.agents.factory import create_agent, _get_can_jump_tofrom .model import FakeToolCallingModel@@ -137,7 +137,7 @@ def test_all_decorators_integration() -> None:agent = create_agent(model=FakeToolCallingModel(), middleware=[track_before, track_modify, track_after])- agent = agent.compile()+ # Agent is already compiledagent.invoke({"messages": [HumanMessage("Hello")]})assert call_order == ["before", "modify", "after"]@@ -226,7 +226,7 @@ def test_can_jump_to_integration() -> None:return Noneagent = create_agent(model=FakeToolCallingModel(), middleware=[early_exit])- agent = agent.compile()+ # Agent is already compiled# Test with early exitresult = agent.invoke({"messages": [HumanMessage("exit")]})@@ -339,7 +339,7 @@ async def test_async_decorators_integration() -> None:model=FakeToolCallingModel(),middleware=[track_async_before, track_async_modify, track_async_after],)- agent = agent.compile()+ # Agent is already compiledawait agent.ainvoke({"messages": [HumanMessage("Hello")]})assert call_order == ["async_before", "async_modify", "async_after"]@@ -395,7 +395,7 @@ async def test_mixed_sync_async_decorators_integration() -> None:track_sync_after,],)- agent = agent.compile()+ # Agent is already compiledawait agent.ainvoke({"messages": [HumanMessage("Hello")]})assert call_order == [@@ -456,7 +456,7 @@ async def test_async_can_jump_to_integration() -> None:return Noneagent = create_agent(model=FakeToolCallingModel(), middleware=[async_early_exit])- agent = agent.compile()+ # Agent is already compiled# Test with early exitresult = await agent.ainvoke({"messages": [HumanMessage("exit")]})@@ -527,7 +527,7 @@ def test_async_middleware_with_can_jump_to_graph_snapshot(snapshot: SnapshotAssemodel=FakeToolCallingModel(), middleware=[async_before_with_jump])- assert agent_async_before.compile().get_graph().draw_mermaid() == snapshot+ assert agent_async_before.get_graph().draw_mermaid() == snapshot# Test 2: Async after_model with can_jump_to@after_model(can_jump_to=["model", "end"])@@ -540,7 +540,7 @@ def test_async_middleware_with_can_jump_to_graph_snapshot(snapshot: SnapshotAssemodel=FakeToolCallingModel(), middleware=[async_after_with_jump])- assert agent_async_after.compile().get_graph().draw_mermaid() == snapshot+ assert agent_async_after.get_graph().draw_mermaid() == snapshot# Test 3: Multiple async middleware with can_jump_to@before_model(can_jump_to=["end"])@@ -556,7 +556,7 @@ def test_async_middleware_with_can_jump_to_graph_snapshot(snapshot: SnapshotAssemiddleware=[async_before_early_exit, async_after_retry],)- assert agent_multiple_async.compile().get_graph().draw_mermaid() == snapshot+ assert agent_multiple_async.get_graph().draw_mermaid() == snapshot# Test 4: Mixed sync and async middleware with can_jump_to@before_model(can_jump_to=["end"])@@ -572,7 +572,7 @@ def test_async_middleware_with_can_jump_to_graph_snapshot(snapshot: SnapshotAssemiddleware=[sync_before_with_jump, async_after_with_jumps],)- assert agent_mixed.compile().get_graph().draw_mermaid() == snapshot+ assert agent_mixed.get_graph().draw_mermaid() == snapshotdef test_dynamic_prompt_decorator() -> None:@@ -637,7 +637,7 @@ def test_dynamic_prompt_integration() -> None:return f"you are a helpful assistant."agent = create_agent(model=FakeToolCallingModel(), middleware=[context_aware_prompt])- agent = agent.compile()+ # Agent is already compiledresult = agent.invoke({"messages": [HumanMessage("Hello")]})@@ -672,7 +672,7 @@ async def test_async_dynamic_prompt_integration() -> None:return f"Async assistant."agent = create_agent(model=FakeToolCallingModel(), middleware=[async_context_prompt])- agent = agent.compile()+ # Agent is already compiledresult = await agent.ainvoke({"messages": [HumanMessage("Hello")]})assert prompt_calls == 1@@ -691,7 +691,7 @@ def test_dynamic_prompt_overwrites_system_prompt() -> None:system_prompt="Original static prompt",middleware=[override_prompt],)- agent = agent.compile()+ # Agent is already compiledresult = agent.invoke({"messages": [HumanMessage("Hello")]})assert result["messages"][-1].content == "Overridden prompt.-Hello"@@ -711,7 +711,7 @@ def test_dynamic_prompt_multiple_in_sequence() -> None:# When used together, the last middleware in the list should win# since they're both modify_model_request hooks executed in sequenceagent = create_agent(model=FakeToolCallingModel(), middleware=[first_prompt, second_prompt])- agent = agent.compile()+ # Agent is already compiledresult = agent.invoke({"messages": [HumanMessage("Hello")]})assert result["messages"][-1].content == "Second prompt.-Hello"diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.pyindex 9f65134d30..f1c12de3d4 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py@@ -3,7 +3,7 @@import pytestfrom langchain.agents.middleware.types import AgentMiddleware, AgentState, ModelRequest-from langchain.agents.middleware_agent import create_agent+from langchain.agents.factory import create_agentfrom langchain_core.messages import HumanMessage, ToolMessagefrom langchain_core.tools import toolfrom .model import FakeToolCallingModel@@ -36,7 +36,7 @@ def test_model_request_tools_are_base_tools() -> None:tools=[search_tool, calculator],system_prompt="You are a helpful assistant.",middleware=[RequestCapturingMiddleware()],- ).compile()+ )agent.invoke({"messages": [HumanMessage("Hello")]})@@ -86,7 +86,7 @@ def test_middleware_can_modify_tools() -> None:tools=[tool_a, tool_b, tool_c],system_prompt="You are a helpful assistant.",middleware=[ToolFilteringMiddleware()],- ).compile()+ )result = agent.invoke({"messages": [HumanMessage("Use tool_a")]})@@ -124,7 +124,7 @@ def test_unknown_tool_raises_error() -> None:tools=[known_tool],system_prompt="You are a helpful assistant.",middleware=[BadMiddleware()],- ).compile()+ )with pytest.raises(ValueError, match="Middleware returned unknown tool names"):agent.invoke({"messages": [HumanMessage("Hello")]})@@ -164,7 +164,7 @@ def test_middleware_can_add_and_remove_tools() -> None:tools=[search, admin_tool],system_prompt="You are a helpful assistant.",middleware=[ConditionalToolMiddleware()],- ).compile()+ )# Test non-admin user - should not have access to admin_tool# We can't directly inspect the bound model, but we can verify the agent runs@@ -199,7 +199,7 @@ def test_empty_tools_list_is_valid() -> None:tools=[some_tool],system_prompt="You are a helpful assistant.",middleware=[NoToolsMiddleware()],- ).compile()+ )# Should run without error even with no toolsresult = agent.invoke({"messages": [HumanMessage("Hello")]})@@ -250,7 +250,7 @@ def test_tools_preserved_across_multiple_middleware() -> None:tools=[tool_a, tool_b, tool_c],system_prompt="You are a helpful assistant.",middleware=[FirstMiddleware(), SecondMiddleware()],- ).compile()+ )agent.invoke({"messages": [HumanMessage("Hello")]})@@ -291,7 +291,7 @@ def test_middleware_with_additional_tools() -> None:tools=[base_tool],system_prompt="You are a helpful assistant.",middleware=[ToolProvidingMiddleware()],- ).compile()+ )result = agent.invoke({"messages": [HumanMessage("Use middleware tool")]})diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_pii_middleware.py b/libs/langchain_v1/tests/unit_tests/agents/test_pii_middleware.pyindex 9c09971f92..5b76f79ef0 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_pii_middleware.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_pii_middleware.py@@ -12,7 +12,7 @@ from langchain.agents.middleware.pii import (detect_mac_address,detect_url,)-from langchain.agents.middleware_agent import create_agent+from langchain.agents.factory import create_agentfrom .model import FakeToolCallingModel@@ -499,9 +499,8 @@ class TestPIIMiddlewareIntegration:middleware=[PIIMiddleware("email", strategy="redact")],)- # Compile and invoke- graph = agent.compile()- result = graph.invoke({"messages": [HumanMessage("Email: [email protected]")]})+ # Invoke (agent is already compiled)+ result = agent.invoke({"messages": [HumanMessage("Email: [email protected]")]})# Check that email was redacted in the stored messages# The first message should have been processed@@ -602,10 +601,8 @@ class TestMultipleMiddleware:],)- graph = agent.compile()-# Test with email and IP (url would block, so we omit it)- result = graph.invoke(+ result = agent.invoke({"messages": [HumanMessage("Contact: [email protected], IP: 192.168.1.100")]})diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_react_agent.py b/libs/langchain_v1/tests/unit_tests/agents/test_react_agent.pyindex d88952e2e9..ad3d9ca9a0 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_react_agent.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_react_agent.py@@ -1,1655 +1,987 @@-import dataclasses-import inspect-from types import UnionType-from typing import (- Annotated,- Union,-)--import pytest-from langchain_core.language_models import BaseChatModel-from langchain_core.messages import (- AIMessage,- HumanMessage,- MessageLikeRepresentation,- RemoveMessage,- SystemMessage,- ToolCall,- ToolMessage,-)-from langchain_core.runnables import RunnableConfig, RunnableLambda-from langchain_core.tools import BaseTool, InjectedToolCallId, ToolException-from langchain_core.tools import tool as dec_tool-from langgraph.checkpoint.base import BaseCheckpointSaver-from langgraph.graph import START, MessagesState, StateGraph-from langgraph.graph.message import REMOVE_ALL_MESSAGES-from langgraph.runtime import Runtime-from langgraph.store.base import BaseStore-from langgraph.store.memory import InMemoryStore-from langgraph.types import Command, Interrupt, interrupt-from pydantic import BaseModel, Field-from typing_extensions import TypedDict--from langchain.agents import (- AgentState,- create_agent,-)-from langchain.agents.react_agent import _validate_chat_history-from langchain.tools import (- ToolNode,- InjectedState,- InjectedStore,-)-from langchain.tools.tool_node import (- _get_state_args,- _infer_handled_types,-)--from .any_str import AnyStr-from .messages import _AnyIdHumanMessage, _AnyIdToolMessage-from .model import FakeToolCallingModel--pytestmark = pytest.mark.anyio---def test_no_prompt(sync_checkpointer: BaseCheckpointSaver) -> None:- model = FakeToolCallingModel()-- agent = create_agent(- model,- [],- checkpointer=sync_checkpointer,- )- inputs = [HumanMessage("hi?")]- thread = {"configurable": {"thread_id": "123"}}- response = agent.invoke({"messages": inputs}, thread, debug=True)- expected_response = {"messages": [*inputs, AIMessage(content="hi?", id="0")]}- assert response == expected_response-- saved = sync_checkpointer.get_tuple(thread)- assert saved is not None- assert saved.checkpoint["channel_values"] == {- "messages": [- _AnyIdHumanMessage(content="hi?"),- AIMessage(content="hi?", id="0"),- ],- }- assert saved.metadata == {- "parents": {},- "source": "loop",- "step": 1,- }- assert saved.pending_writes == []---async def test_no_prompt_async(async_checkpointer: BaseCheckpointSaver) -> None:- model = FakeToolCallingModel()-- agent = create_agent(model, [], checkpointer=async_checkpointer)- inputs = [HumanMessage("hi?")]- thread = {"configurable": {"thread_id": "123"}}- response = await agent.ainvoke({"messages": inputs}, thread, debug=True)- expected_response = {"messages": [*inputs, AIMessage(content="hi?", id="0")]}- assert response == expected_response-- saved = await async_checkpointer.aget_tuple(thread)- assert saved is not None- assert saved.checkpoint["channel_values"] == {- "messages": [- _AnyIdHumanMessage(content="hi?"),- AIMessage(content="hi?", id="0"),- ],- }- assert saved.metadata == {- "parents": {},- "source": "loop",- "step": 1,- }- assert saved.pending_writes == []---def test_system_message_prompt() -> None:- prompt = SystemMessage(content="Foo")- agent = create_agent(FakeToolCallingModel(), [], prompt=prompt)- inputs = [HumanMessage("hi?")]- response = agent.invoke({"messages": inputs})- expected_response = {"messages": [*inputs, AIMessage(content="Foo-hi?", id="0", tool_calls=[])]}- assert response == expected_response---def test_string_prompt() -> None:- prompt = "Foo"- agent = create_agent(FakeToolCallingModel(), [], prompt=prompt)- inputs = [HumanMessage("hi?")]- response = agent.invoke({"messages": inputs})- expected_response = {"messages": [*inputs, AIMessage(content="Foo-hi?", id="0", tool_calls=[])]}- assert response == expected_response---def test_callable_prompt() -> None:- def prompt(state):- modified_message = f"Bar {state['messages'][-1].content}"- return [HumanMessage(content=modified_message)]-- agent = create_agent(FakeToolCallingModel(), [], prompt=prompt)- inputs = [HumanMessage("hi?")]- response = agent.invoke({"messages": inputs})- expected_response = {"messages": [*inputs, AIMessage(content="Bar hi?", id="0")]}- assert response == expected_response---async def test_callable_prompt_async() -> None:- async def prompt(state):- modified_message = f"Bar {state['messages'][-1].content}"- return [HumanMessage(content=modified_message)]-- agent = create_agent(FakeToolCallingModel(), [], prompt=prompt)- inputs = [HumanMessage("hi?")]- response = await agent.ainvoke({"messages": inputs})- expected_response = {"messages": [*inputs, AIMessage(content="Bar hi?", id="0")]}- assert response == expected_response---def test_runnable_prompt() -> None:- prompt = RunnableLambda(- lambda state: [HumanMessage(content=f"Baz {state['messages'][-1].content}")]- )-- agent = create_agent(FakeToolCallingModel(), [], prompt=prompt)- inputs = [HumanMessage("hi?")]- response = agent.invoke({"messages": inputs})- expected_response = {"messages": [*inputs, AIMessage(content="Baz hi?", id="0")]}- assert response == expected_response---def test_prompt_with_store() -> None:- def add(a: int, b: int):- """Adds a and b"""- return a + b-- in_memory_store = InMemoryStore()- in_memory_store.put(("memories", "1"), "user_name", {"data": "User name is Alice"})- in_memory_store.put(("memories", "2"), "user_name", {"data": "User name is Bob"})-- def prompt(state, config, *, store):- user_id = config["configurable"]["user_id"]- system_str = store.get(("memories", user_id), "user_name").value["data"]- return [SystemMessage(system_str)] + state["messages"]-- def prompt_no_store(state, config):- return SystemMessage("foo") + state["messages"]-- model = FakeToolCallingModel()-- # test state modifier that uses store works- agent = create_agent(- model,- [add],- prompt=prompt,- store=in_memory_store,- )- response = agent.invoke({"messages": [("user", "hi")]}, {"configurable": {"user_id": "1"}})- assert response["messages"][-1].content == "User name is Alice-hi"-- # test state modifier that doesn't use store works- agent = create_agent(- model,- [add],- prompt=prompt_no_store,- store=in_memory_store,- )- response = agent.invoke({"messages": [("user", "hi")]}, {"configurable": {"user_id": "2"}})- assert response["messages"][-1].content == "foo-hi"---async def test_prompt_with_store_async() -> None:- async def add(a: int, b: int):- """Adds a and b"""- return a + b-- in_memory_store = InMemoryStore()- await in_memory_store.aput(("memories", "1"), "user_name", {"data": "User name is Alice"})- await in_memory_store.aput(("memories", "2"), "user_name", {"data": "User name is Bob"})-- async def prompt(state, config, *, store):- user_id = config["configurable"]["user_id"]- system_str = (await store.aget(("memories", user_id), "user_name")).value["data"]- return [SystemMessage(system_str)] + state["messages"]-- async def prompt_no_store(state, config):- return SystemMessage("foo") + state["messages"]-- model = FakeToolCallingModel()-- # test state modifier that uses store works- agent = create_agent(model, [add], prompt=prompt, store=in_memory_store)- response = await agent.ainvoke(- {"messages": [("user", "hi")]}, {"configurable": {"user_id": "1"}}- )- assert response["messages"][-1].content == "User name is Alice-hi"-- # test state modifier that doesn't use store works- agent = create_agent(model, [add], prompt=prompt_no_store, store=in_memory_store)- response = await agent.ainvoke(- {"messages": [("user", "hi")]}, {"configurable": {"user_id": "2"}}- )- assert response["messages"][-1].content == "foo-hi"--[email protected]("tool_style", ["openai", "anthropic"])[email protected]("include_builtin", [True, False])-def test_model_with_tools(tool_style: str, include_builtin: bool) -> None:- model = FakeToolCallingModel(tool_style=tool_style)-- @dec_tool- def tool1(some_val: int) -> str:- """Tool 1 docstring."""- return f"Tool 1: {some_val}"-- @dec_tool- def tool2(some_val: int) -> str:- """Tool 2 docstring."""- return f"Tool 2: {some_val}"-- tools: list[BaseTool | dict] = [tool1, tool2]- if include_builtin:- tools.append(- {- "type": "mcp",- "server_label": "atest_sever",- "server_url": "https://some.mcp.somewhere.com/sse",- "headers": {"foo": "bar"},- "allowed_tools": [- "mcp_tool_1",- "set_active_account",- "get_url_markdown",- "get_url_screenshot",- ],- "require_approval": "never",- }- )- # check valid agent constructor- with pytest.raises(ValueError):- create_agent(- model.bind_tools(tools),- tools,- )---def test__validate_messages() -> None:- # empty input- _validate_chat_history([])-- # single human message- _validate_chat_history(- [- HumanMessage(content="What's the weather?"),- ]- )-- # human + AI- _validate_chat_history(- [- HumanMessage(content="What's the weather?"),- AIMessage(content="The weather is sunny and 75°F."),- ]- )-- # Answered tool calls- _validate_chat_history(- [- HumanMessage(content="What's the weather?"),- AIMessage(- content="Let me check that for you.",- tool_calls=[{"id": "call1", "name": "get_weather", "args": {}}],- ),- ToolMessage(content="Sunny, 75°F", tool_call_id="call1"),- AIMessage(content="The weather is sunny and 75°F."),- ]- )-- # Unanswered tool calls- with pytest.raises(ValueError):- _validate_chat_history(- [- AIMessage(- content="I'll check that for you.",- tool_calls=[- {"id": "call1", "name": "get_weather", "args": {}},- {"id": "call2", "name": "get_time", "args": {}},- ],- )- ]- )-- with pytest.raises(ValueError):- _validate_chat_history(- [- HumanMessage(content="What's the weather and time?"),- AIMessage(- content="I'll check that for you.",- tool_calls=[- {"id": "call1", "name": "get_weather", "args": {}},- {"id": "call2", "name": "get_time", "args": {}},- ],- ),- ToolMessage(content="Sunny, 75°F", tool_call_id="call1"),- AIMessage(content="The weather is sunny and 75°F. Let me check the time."),- ]- )---def test__infer_handled_types() -> None:- def handle(e) -> str: # type: ignore- return ""-- def handle2(e: Exception) -> str:- return ""-- def handle3(e: ValueError | ToolException) -> str:- return ""-- def handle4(e: Union[ValueError, ToolException]) -> str:- return ""-- class Handler:- def handle(self, e: ValueError) -> str:- return ""-- handle5 = Handler().handle-- def handle6(e: Union[Union[TypeError, ValueError], ToolException]) -> str:- return ""-- expected: tuple = (Exception,)- actual = _infer_handled_types(handle)- assert expected == actual-- expected = (Exception,)- actual = _infer_handled_types(handle2)- assert expected == actual-- expected = (ValueError, ToolException)- actual = _infer_handled_types(handle3)- assert expected == actual-- expected = (ValueError, ToolException)- actual = _infer_handled_types(handle4)- assert expected == actual-- expected = (ValueError,)- actual = _infer_handled_types(handle5)- assert expected == actual-- expected = (TypeError, ValueError, ToolException)- actual = _infer_handled_types(handle6)- assert expected == actual-- with pytest.raises(ValueError):-- def handler(e: str) -> str:- return ""-- _infer_handled_types(handler)-- with pytest.raises(ValueError):-- def handler(e: list[Exception]) -> str:- return ""-- _infer_handled_types(handler)-- with pytest.raises(ValueError):-- def handler(e: Union[str, int]) -> str:- return ""-- _infer_handled_types(handler)---def test_react_agent_with_structured_response() -> None:- class WeatherResponse(BaseModel):- temperature: float = Field(description="The temperature in fahrenheit")-- tool_calls = [- [{"args": {}, "id": "1", "name": "get_weather"}],- [{"name": "WeatherResponse", "id": "2", "args": {"temperature": 75}}],- ]-- def get_weather() -> str:- """Get the weather"""- return "The weather is sunny and 75°F."-- expected_structured_response = WeatherResponse(temperature=75)- model = FakeToolCallingModel[WeatherResponse](- tool_calls=tool_calls, structured_response=expected_structured_response- )- agent = create_agent(- model,- [get_weather],- response_format=WeatherResponse,- )- response = agent.invoke({"messages": [HumanMessage("What's the weather?")]})- assert response["structured_response"] == expected_structured_response- assert len(response["messages"]) == 5-- # Check message types in message history- msg_types = [m.type for m in response["messages"]]- assert msg_types == [- "human", # "What's the weather?"- "ai", # "What's the weather?"- "tool", # "The weather is sunny and 75°F."- "ai", # structured response- "tool", # artificial tool message- ]-- assert [m.content for m in response["messages"]] == [- "What's the weather?",- "What's the weather?",- "The weather is sunny and 75°F.",- "What's the weather?-What's the weather?-The weather is sunny and 75°F.",- "Returning structured response: {'temperature': 75.0}",- ]---class CustomState(AgentState):- user_name: str---def test_react_agent_update_state(- sync_checkpointer: BaseCheckpointSaver,-) -> None:- @dec_tool- def get_user_name(tool_call_id: Annotated[str, InjectedToolCallId]):- """Retrieve user name"""- user_name = interrupt("Please provider user name:")- return Command(- update={- "user_name": user_name,- "messages": [- ToolMessage("Successfully retrieved user name", tool_call_id=tool_call_id)- ],- }- )-- def prompt(state: CustomState):- user_name = state.get("user_name")- if user_name is None:- return state["messages"]-- system_msg = f"User name is {user_name}"- return [{"role": "system", "content": system_msg}] + state["messages"]-- tool_calls = [[{"args": {}, "id": "1", "name": "get_user_name"}]]- model = FakeToolCallingModel(tool_calls=tool_calls)- agent = create_agent(- model,- [get_user_name],- state_schema=CustomState,- prompt=prompt,- checkpointer=sync_checkpointer,- )- config = {"configurable": {"thread_id": "1"}}- # Run until interrupted- agent.invoke({"messages": [("user", "what's my name")]}, config)- # supply the value for the interrupt- response = agent.invoke(Command(resume="Archibald"), config)- # confirm that the state was updated- assert response["user_name"] == "Archibald"- assert len(response["messages"]) == 4- tool_message: ToolMessage = response["messages"][-2]- assert tool_message.content == "Successfully retrieved user name"- assert tool_message.tool_call_id == "1"- assert tool_message.name == "get_user_name"---def test_react_agent_parallel_tool_calls(- sync_checkpointer: BaseCheckpointSaver,-) -> None:- human_assistance_execution_count = 0-- @dec_tool- def human_assistance(query: str) -> str:- """Request assistance from a human."""- nonlocal human_assistance_execution_count- human_response = interrupt({"query": query})- human_assistance_execution_count += 1- return human_response["data"]-- get_weather_execution_count = 0-- @dec_tool- def get_weather(location: str) -> str:- """Use this tool to get the weather."""- nonlocal get_weather_execution_count- get_weather_execution_count += 1- return "It's sunny!"-- tool_calls = [- [- {"args": {"location": "sf"}, "id": "1", "name": "get_weather"},- {"args": {"query": "request help"}, "id": "2", "name": "human_assistance"},- ],- [],- ]- model = FakeToolCallingModel(tool_calls=tool_calls)- agent = create_agent(- model,- [human_assistance, get_weather],- checkpointer=sync_checkpointer,- )- config = {"configurable": {"thread_id": "1"}}- query = "Get user assistance and also check the weather"- message_types = []- for event in agent.stream({"messages": [("user", query)]}, config, stream_mode="values"):- if messages := event.get("messages"):- message_types.append([m.type for m in messages])-- assert message_types == [- ["human"],- ["human", "ai"],- ["human", "ai", "tool"],- ]-- # Resume- message_types = []- for event in agent.stream(Command(resume={"data": "Hello"}), config, stream_mode="values"):- if messages := event.get("messages"):- message_types.append([m.type for m in messages])-- assert message_types == [- ["human", "ai"],- ["human", "ai", "tool", "tool"],- ["human", "ai", "tool", "tool", "ai"],- ]-- assert human_assistance_execution_count == 1- assert get_weather_execution_count == 1---class AgentStateExtraKey(AgentState):- foo: int---def test_create_agent_inject_vars() -> None:- """Test that the agent can inject state and store into tool functions."""- store = InMemoryStore()- namespace = ("test",)- store.put(namespace, "test_key", {"bar": 3})-- def tool1(- some_val: int,- state: Annotated[dict, InjectedState],- store: Annotated[BaseStore, InjectedStore()],- ) -> str:- """Tool 1 docstring."""- store_val = store.get(namespace, "test_key").value["bar"]- return some_val + state["foo"] + store_val-- tool_call = {- "name": "tool1",- "args": {"some_val": 1},- "id": "some 0",- "type": "tool_call",- }- model = FakeToolCallingModel(tool_calls=[[tool_call], []])- agent = create_agent(- model,- ToolNode([tool1], handle_tool_errors=False),- state_schema=AgentStateExtraKey,- store=store,- )- result = agent.invoke({"messages": [{"role": "user", "content": "hi"}], "foo": 2})- assert result["messages"] == [- _AnyIdHumanMessage(content="hi"),- AIMessage(content="hi", tool_calls=[tool_call], id="0"),- _AnyIdToolMessage(content="6", name="tool1", tool_call_id="some 0"),- AIMessage("hi-hi-6", id="1"),- ]- assert result["foo"] == 2---async def test_return_direct() -> None:- @dec_tool(return_direct=True)- def tool_return_direct(input: str) -> str:- """A tool that returns directly."""- return f"Direct result: {input}"-- @dec_tool- def tool_normal(input: str) -> str:- """A normal tool."""- return f"Normal result: {input}"-- first_tool_call = [- ToolCall(- name="tool_return_direct",- args={"input": "Test direct"},- id="1",- ),- ]- expected_ai = AIMessage(- content="Test direct",- id="0",- tool_calls=first_tool_call,- )- model = FakeToolCallingModel(tool_calls=[first_tool_call, []])- agent = create_agent(- model,- [tool_return_direct, tool_normal],- )-- # Test direct return for tool_return_direct- result = agent.invoke({"messages": [HumanMessage(content="Test direct", id="hum0")]})- assert result["messages"] == [- HumanMessage(content="Test direct", id="hum0"),- expected_ai,- ToolMessage(- content="Direct result: Test direct",- name="tool_return_direct",- tool_call_id="1",- id=result["messages"][2].id,- ),- ]- second_tool_call = [- ToolCall(- name="tool_normal",- args={"input": "Test normal"},- id="2",- ),- ]- model = FakeToolCallingModel(tool_calls=[second_tool_call, []])- agent = create_agent(model, [tool_return_direct, tool_normal])- result = agent.invoke({"messages": [HumanMessage(content="Test normal", id="hum1")]})- assert result["messages"] == [- HumanMessage(content="Test normal", id="hum1"),- AIMessage(content="Test normal", id="0", tool_calls=second_tool_call),- ToolMessage(- content="Normal result: Test normal",- name="tool_normal",- tool_call_id="2",- id=result["messages"][2].id,- ),- AIMessage(content="Test normal-Test normal-Normal result: Test normal", id="1"),- ]-- both_tool_calls = [- ToolCall(- name="tool_return_direct",- args={"input": "Test both direct"},- id="3",- ),- ToolCall(- name="tool_normal",- args={"input": "Test both normal"},- id="4",- ),- ]- model = FakeToolCallingModel(tool_calls=[both_tool_calls, []])- agent = create_agent(model, [tool_return_direct, tool_normal])- result = agent.invoke({"messages": [HumanMessage(content="Test both", id="hum2")]})- assert result["messages"] == [- HumanMessage(content="Test both", id="hum2"),- AIMessage(content="Test both", id="0", tool_calls=both_tool_calls),- ToolMessage(- content="Direct result: Test both direct",- name="tool_return_direct",- tool_call_id="3",- id=result["messages"][2].id,- ),- ToolMessage(- content="Normal result: Test both normal",- name="tool_normal",- tool_call_id="4",- id=result["messages"][3].id,- ),- ]---def test__get_state_args() -> None:- class Schema1(BaseModel):- a: Annotated[str, InjectedState]-- class Schema2(Schema1):- b: Annotated[int, InjectedState("bar")]-- @dec_tool(args_schema=Schema2)- def foo(a: str, b: int) -> float:- """return"""- return 0.0-- assert _get_state_args(foo) == {"a": None, "b": "bar"}---def test_inspect_react() -> None:- model = FakeToolCallingModel(tool_calls=[])- agent = create_agent(model, [])- inspect.getclosurevars(agent.nodes["agent"].bound.func)---def test_react_with_subgraph_tools(- sync_checkpointer: BaseCheckpointSaver,-) -> None:- class State(TypedDict):- a: int- b: int-- class Output(TypedDict):- result: int-- # Define the subgraphs- def add(state):- return {"result": state["a"] + state["b"]}-- add_subgraph = (- StateGraph(State, output_schema=Output).add_node(add).add_edge(START, "add").compile()- )-- def multiply(state):- return {"result": state["a"] * state["b"]}-- multiply_subgraph = (- StateGraph(State, output_schema=Output)- .add_node(multiply)- .add_edge(START, "multiply")- .compile()- )-- multiply_subgraph.invoke({"a": 2, "b": 3})-- # Add subgraphs as tools-- def addition(a: int, b: int):- """Add two numbers"""- return add_subgraph.invoke({"a": a, "b": b})["result"]-- def multiplication(a: int, b: int):- """Multiply two numbers"""- return multiply_subgraph.invoke({"a": a, "b": b})["result"]-- model = FakeToolCallingModel(- tool_calls=[- [- {"args": {"a": 2, "b": 3}, "id": "1", "name": "addition"},- {"args": {"a": 2, "b": 3}, "id": "2", "name": "multiplication"},- ],- [],- ]- )- tool_node = ToolNode([addition, multiplication], handle_tool_errors=False)- agent = create_agent(- model,- tool_node,- checkpointer=sync_checkpointer,- )- result = agent.invoke(- {"messages": [HumanMessage(content="What's 2 + 3 and 2 * 3?")]},- config={"configurable": {"thread_id": "1"}},- )- assert result["messages"] == [- _AnyIdHumanMessage(content="What's 2 + 3 and 2 * 3?"),- AIMessage(- content="What's 2 + 3 and 2 * 3?",- id="0",- tool_calls=[- ToolCall(name="addition", args={"a": 2, "b": 3}, id="1"),- ToolCall(name="multiplication", args={"a": 2, "b": 3}, id="2"),- ],- ),- ToolMessage(content="5", name="addition", tool_call_id="1", id=result["messages"][2].id),- ToolMessage(- content="6",- name="multiplication",- tool_call_id="2",- id=result["messages"][3].id,- ),- AIMessage(content="What's 2 + 3 and 2 * 3?-What's 2 + 3 and 2 * 3?-5-6", id="1"),- ]---def test_react_agent_subgraph_streaming_sync() -> None:- """Test React agent streaming when used as a subgraph node sync version"""-- @dec_tool- def get_weather(city: str) -> str:- """Get the weather of a city."""- return f"The weather of {city} is sunny."-- # Create a React agent- model = FakeToolCallingModel(- tool_calls=[- [{"args": {"city": "Tokyo"}, "id": "1", "name": "get_weather"}],- [],- ]- )-- agent = create_agent(- model,- tools=[get_weather],- prompt="You are a helpful travel assistant.",- )-- # Create a subgraph that uses the React agent as a node- def react_agent_node(state: MessagesState, config: RunnableConfig) -> MessagesState:- """Node that runs the React agent and collects streaming output."""- collected_content = ""-- # Stream the agent output and collect content- for msg_chunk, _msg_metadata in agent.stream(- {"messages": [("user", state["messages"][-1].content)]},- config,- stream_mode="messages",- ):- if hasattr(msg_chunk, "content") and msg_chunk.content:- collected_content += msg_chunk.content-- return {"messages": [("assistant", collected_content)]}-- # Create the main workflow with the React agent as a subgraph node- workflow = StateGraph(MessagesState)- workflow.add_node("react_agent", react_agent_node)- workflow.add_edge(START, "react_agent")- workflow.add_edge("react_agent", "__end__")- compiled_workflow = workflow.compile()-- # Test the streaming functionality- result = compiled_workflow.invoke({"messages": [("user", "What is the weather in Tokyo?")]})-- # Verify the result contains expected structure- assert len(result["messages"]) == 2- assert result["messages"][0].content == "What is the weather in Tokyo?"- assert "assistant" in str(result["messages"][1])-- # Test streaming with subgraphs = True- result = compiled_workflow.invoke(- {"messages": [("user", "What is the weather in Tokyo?")]},- subgraphs=True,- )- assert len(result["messages"]) == 2-- events = []- for event in compiled_workflow.stream(- {"messages": [("user", "What is the weather in Tokyo?")]},- stream_mode="messages",- subgraphs=False,- ):- events.append(event)-- assert len(events) == 0-- events = []- for event in compiled_workflow.stream(- {"messages": [("user", "What is the weather in Tokyo?")]},- stream_mode="messages",- subgraphs=True,- ):- events.append(event)-- assert len(events) == 3- namespace, (msg, metadata) = events[0]- # FakeToolCallingModel returns a single AIMessage with tool calls- # The content of the AIMessage reflects the input message- assert msg.content.startswith("You are a helpful travel assistant")- namespace, (msg, metadata) = events[1] # ToolMessage- assert msg.content.startswith("The weather of Tokyo is sunny.")---async def test_react_agent_subgraph_streaming() -> None:- """Test React agent streaming when used as a subgraph node."""-- @dec_tool- def get_weather(city: str) -> str:- """Get the weather of a city."""- return f"The weather of {city} is sunny."-- # Create a React agent- model = FakeToolCallingModel(- tool_calls=[- [{"args": {"city": "Tokyo"}, "id": "1", "name": "get_weather"}],- [],- ]- )-- agent = create_agent(- model,- tools=[get_weather],- prompt="You are a helpful travel assistant.",- )-- # Create a subgraph that uses the React agent as a node- async def react_agent_node(state: MessagesState, config: RunnableConfig) -> MessagesState:- """Node that runs the React agent and collects streaming output."""- collected_content = ""-- # Stream the agent output and collect content- async for msg_chunk, _msg_metadata in agent.astream(- {"messages": [("user", state["messages"][-1].content)]},- config,- stream_mode="messages",- ):- if hasattr(msg_chunk, "content") and msg_chunk.content:- collected_content += msg_chunk.content-- return {"messages": [("assistant", collected_content)]}-- # Create the main workflow with the React agent as a subgraph node- workflow = StateGraph(MessagesState)- workflow.add_node("react_agent", react_agent_node)- workflow.add_edge(START, "react_agent")- workflow.add_edge("react_agent", "__end__")- compiled_workflow = workflow.compile()-- # Test the streaming functionality- result = await compiled_workflow.ainvoke(- {"messages": [("user", "What is the weather in Tokyo?")]}- )-- # Verify the result contains expected structure- assert len(result["messages"]) == 2- assert result["messages"][0].content == "What is the weather in Tokyo?"- assert "assistant" in str(result["messages"][1])-- # Test streaming with subgraphs = True- result = await compiled_workflow.ainvoke(- {"messages": [("user", "What is the weather in Tokyo?")]},- subgraphs=True,- )- assert len(result["messages"]) == 2-- events = []- async for event in compiled_workflow.astream(- {"messages": [("user", "What is the weather in Tokyo?")]},- stream_mode="messages",- subgraphs=False,- ):- events.append(event)-- assert len(events) == 0-- events = []- async for event in compiled_workflow.astream(- {"messages": [("user", "What is the weather in Tokyo?")]},- stream_mode="messages",- subgraphs=True,- ):- events.append(event)-- assert len(events) == 3- namespace, (msg, metadata) = events[0]- # FakeToolCallingModel returns a single AIMessage with tool calls- # The content of the AIMessage reflects the input message- assert msg.content.startswith("You are a helpful travel assistant")- namespace, (msg, metadata) = events[1] # ToolMessage- assert msg.content.startswith("The weather of Tokyo is sunny.")---def test_tool_node_node_interrupt(- sync_checkpointer: BaseCheckpointSaver,-) -> None:- def tool_normal(some_val: int) -> str:- """Tool docstring."""- return "normal"-- def tool_interrupt(some_val: int) -> str:- """Tool docstring."""- return interrupt("provide value for foo")-- # test inside react agent- model = FakeToolCallingModel(- tool_calls=[- [- ToolCall(name="tool_interrupt", args={"some_val": 0}, id="1"),- ToolCall(name="tool_normal", args={"some_val": 1}, id="2"),- ],- [],- ]- )- config = {"configurable": {"thread_id": "1"}}- agent = create_agent(- model,- [tool_interrupt, tool_normal],- checkpointer=sync_checkpointer,- )- result = agent.invoke({"messages": [HumanMessage("hi?")]}, config)- expected_messages = [- _AnyIdHumanMessage(content="hi?"),- AIMessage(- content="hi?",- id="0",- tool_calls=[- {- "name": "tool_interrupt",- "args": {"some_val": 0},- "id": "1",- "type": "tool_call",- },- {- "name": "tool_normal",- "args": {"some_val": 1},- "id": "2",- "type": "tool_call",- },- ],- ),- _AnyIdToolMessage(content="normal", name="tool_normal", tool_call_id="2"),- ]- assert result["messages"] == expected_messages-- state = agent.get_state(config)- assert state.next == ("tools",)- task = state.tasks[0]- assert task.name == "tools"- assert task.interrupts == (- Interrupt(- value="provide value for foo",- id=AnyStr(),- ),- )---def test_dynamic_model_basic() -> None:- """Test basic dynamic model functionality."""-- def dynamic_model(state, runtime: Runtime):- # Return different models based on state- if "urgent" in state["messages"][-1].content:- return FakeToolCallingModel(tool_calls=[])- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(dynamic_model, [])-- result = agent.invoke({"messages": [HumanMessage("hello")]})- assert len(result["messages"]) == 2- assert result["messages"][-1].content == "hello"-- result = agent.invoke({"messages": [HumanMessage("urgent help")]})- assert len(result["messages"]) == 2- assert result["messages"][-1].content == "urgent help"---def test_dynamic_model_with_tools() -> None:- """Test dynamic model with tool calling."""-- @dec_tool- def basic_tool(x: int) -> str:- """Basic tool."""- return f"basic: {x}"-- @dec_tool- def advanced_tool(x: int) -> str:- """Advanced tool."""- return f"advanced: {x}"-- def dynamic_model(state: dict, runtime: Runtime) -> BaseChatModel:- # Return model with different behaviors based on message content- if "advanced" in state["messages"][-1].content:- return FakeToolCallingModel(- tool_calls=[- [{"args": {"x": 1}, "id": "1", "name": "advanced_tool"}],- [],- ]- )- return FakeToolCallingModel(- tool_calls=[[{"args": {"x": 1}, "id": "1", "name": "basic_tool"}], []]- )-- agent = create_agent(dynamic_model, [basic_tool, advanced_tool])-- # Test basic tool usage- result = agent.invoke({"messages": [HumanMessage("basic request")]})- assert len(result["messages"]) == 3- tool_message = result["messages"][-1]- assert tool_message.content == "basic: 1"- assert tool_message.name == "basic_tool"-- # Test advanced tool usage- result = agent.invoke({"messages": [HumanMessage("advanced request")]})- assert len(result["messages"]) == 3- tool_message = result["messages"][-1]- assert tool_message.content == "advanced: 1"- assert tool_message.name == "advanced_tool"---class Context:- user_id: str---def test_dynamic_model_with_context() -> None:- """Test dynamic model using config parameters."""-- def dynamic_model(state, runtime: Runtime[Context]):- # Use context to determine model behavior- user_id = runtime.context.user_id- if user_id == "user_premium":- return FakeToolCallingModel(tool_calls=[])- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(dynamic_model, [], context_schema=Context)-- # Test with basic user- result = agent.invoke(- {"messages": [HumanMessage("hello")]},- context=Context(user_id="user_basic"),- )- assert len(result["messages"]) == 2-- # Test with premium user- result = agent.invoke(- {"messages": [HumanMessage("hello")]},- context=Context(user_id="user_premium"),- )- assert len(result["messages"]) == 2---def test_dynamic_model_with_state_schema() -> None:- """Test dynamic model with custom state schema."""-- class CustomDynamicState(AgentState):- model_preference: str = "default"-- def dynamic_model(state: CustomDynamicState, runtime: Runtime) -> BaseChatModel:- # Use custom state field to determine model- if state.get("model_preference") == "advanced":- return FakeToolCallingModel(tool_calls=[])- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(dynamic_model, [], state_schema=CustomDynamicState)-- result = agent.invoke({"messages": [HumanMessage("hello")], "model_preference": "advanced"})- assert len(result["messages"]) == 2- assert result["model_preference"] == "advanced"---def test_dynamic_model_with_prompt() -> None:- """Test dynamic model with different prompt types."""-- def dynamic_model(state: AgentState, runtime: Runtime) -> BaseChatModel:- return FakeToolCallingModel(tool_calls=[])-- # Test with string prompt- agent = create_agent(dynamic_model, [], prompt="system_msg")- result = agent.invoke({"messages": [HumanMessage("human_msg")]})- assert result["messages"][-1].content == "system_msg-human_msg"-- # Test with callable prompt- def dynamic_prompt(state: AgentState) -> list[MessageLikeRepresentation]:- """Generate a dynamic system message based on state."""- return [{"role": "system", "content": "system_msg"}, *list(state["messages"])]-- agent = create_agent(dynamic_model, [], prompt=dynamic_prompt)- result = agent.invoke({"messages": [HumanMessage("human_msg")]})- assert result["messages"][-1].content == "system_msg-human_msg"---async def test_dynamic_model_async() -> None:- """Test dynamic model with async operations."""-- def dynamic_model(state: AgentState, runtime: Runtime) -> BaseChatModel:- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(dynamic_model, [])-- result = await agent.ainvoke({"messages": [HumanMessage("hello async")]})- assert len(result["messages"]) == 2- assert result["messages"][-1].content == "hello async"---def test_dynamic_model_with_structured_response() -> None:- """Test dynamic model with structured response format."""-- class TestResponse(BaseModel):- message: str- confidence: float-- def dynamic_model(state, runtime: Runtime):- return FakeToolCallingModel(- tool_calls=[- [- ToolCall(- name="TestResponse",- args={"message": "dynamic response", "confidence": 0.9},- id="1",- type="tool_call",- )- ]- ],- )-- agent = create_agent(dynamic_model, [], response_format=TestResponse)-- result = agent.invoke({"messages": [HumanMessage("hello")]})- assert "structured_response" in result- assert result["structured_response"].message == "dynamic response"- assert result["structured_response"].confidence == 0.9---def test_dynamic_model_with_checkpointer(sync_checkpointer) -> None:- """Test dynamic model with checkpointer."""- call_count = 0-- def dynamic_model(state: AgentState, runtime: Runtime) -> BaseChatModel:- nonlocal call_count- call_count += 1- return FakeToolCallingModel(- tool_calls=[],- # Incrementing the call count as it is used to assign an id- # to the AIMessage.- # The default reducer semantics are to overwrite an existing message- # with the new one if the id matches.- index=call_count,- )-- agent = create_agent(dynamic_model, [], checkpointer=sync_checkpointer)- config = {"configurable": {"thread_id": "test_dynamic"}}-- # First call- result1 = agent.invoke({"messages": [HumanMessage("hello")]}, config)- assert len(result1["messages"]) == 2 # Human + AI message-- # Second call - should load from checkpoint- result2 = agent.invoke({"messages": [HumanMessage("world")]}, config)- assert len(result2["messages"]) == 4-- # Dynamic model should be called each time- assert call_count >= 2---def test_dynamic_model_state_dependent_tools() -> None:- """Test dynamic model that changes available tools based on state."""-- @dec_tool- def tool_a(x: int) -> str:- """Tool A."""- return f"A: {x}"-- @dec_tool- def tool_b(x: int) -> str:- """Tool B."""- return f"B: {x}"-- def dynamic_model(state, runtime: Runtime):- # Switch tools based on message history- if any("use_b" in msg.content for msg in state["messages"]):- return FakeToolCallingModel(- tool_calls=[[{"args": {"x": 2}, "id": "1", "name": "tool_b"}], []]- )- return FakeToolCallingModel(- tool_calls=[[{"args": {"x": 1}, "id": "1", "name": "tool_a"}], []]- )-- agent = create_agent(dynamic_model, [tool_a, tool_b])-- # Ask to use tool B- result = agent.invoke({"messages": [HumanMessage("use_b please")]})- last_message = result["messages"][-1]- assert isinstance(last_message, ToolMessage)- assert last_message.content == "B: 2"-- # Ask to use tool A- result = agent.invoke({"messages": [HumanMessage("hello")]})- last_message = result["messages"][-1]- assert isinstance(last_message, ToolMessage)- assert last_message.content == "A: 1"---def test_dynamic_model_error_handling() -> None:- """Test error handling in dynamic model."""-- def failing_dynamic_model(state, runtime: Runtime):- if "fail" in state["messages"][-1].content:- msg = "Dynamic model failed"- raise ValueError(msg)- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(failing_dynamic_model, [])-- # Normal operation should work- result = agent.invoke({"messages": [HumanMessage("hello")]})- assert len(result["messages"]) == 2-- # Should propagate the error- with pytest.raises(ValueError, match="Dynamic model failed"):- agent.invoke({"messages": [HumanMessage("fail now")]})---def test_dynamic_model_vs_static_model_behavior() -> None:- """Test that dynamic and static models produce equivalent results when configured the same."""- # Static model- static_model = FakeToolCallingModel(tool_calls=[])- static_agent = create_agent(static_model, [])-- # Dynamic model returning the same model- def dynamic_model(state, runtime: Runtime):- return FakeToolCallingModel(tool_calls=[])-- dynamic_agent = create_agent(dynamic_model, [])-- input_msg = {"messages": [HumanMessage("test message")]}-- static_result = static_agent.invoke(input_msg)- dynamic_result = dynamic_agent.invoke(input_msg)-- # Results should be equivalent (content-wise, IDs may differ)- assert len(static_result["messages"]) == len(dynamic_result["messages"])- assert static_result["messages"][0].content == dynamic_result["messages"][0].content- assert static_result["messages"][1].content == dynamic_result["messages"][1].content---def test_dynamic_model_receives_correct_state() -> None:- """Test that the dynamic model function receives the correct state, not the model input."""- received_states = []-- class CustomAgentState(AgentState):- custom_field: str-- def dynamic_model(state, runtime: Runtime) -> BaseChatModel:- # Capture the state that's passed to the dynamic model function- received_states.append(state)- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(dynamic_model, [], state_schema=CustomAgentState)-- # Test with initial state- input_state = {"messages": [HumanMessage("hello")], "custom_field": "test_value"}- agent.invoke(input_state)-- # The dynamic model function should receive the original state, not the processed model input- assert len(received_states) == 1- received_state = received_states[0]-- # Should have the custom field from original state- assert "custom_field" in received_state- assert received_state["custom_field"] == "test_value"-- # Should have the original messages- assert len(received_state["messages"]) == 1- assert received_state["messages"][0].content == "hello"---async def test_dynamic_model_receives_correct_state_async() -> None:- """Test that the async dynamic model function receives the correct state, not the model input."""- received_states = []-- class CustomAgentStateAsync(AgentState):- custom_field: str-- def dynamic_model(state, runtime: Runtime):- # Capture the state that's passed to the dynamic model function- received_states.append(state)- return FakeToolCallingModel(tool_calls=[])-- agent = create_agent(dynamic_model, [], state_schema=CustomAgentStateAsync)-- # Test with initial state- input_state = {- "messages": [HumanMessage("hello async")],- "custom_field": "test_value_async",- }- await agent.ainvoke(input_state)-- # The dynamic model function should receive the original state, not the processed model input- assert len(received_states) == 1- received_state = received_states[0]-- # Should have the custom field from original state- assert "custom_field" in received_state- assert received_state["custom_field"] == "test_value_async"-- # Should have the original messages- assert len(received_state["messages"]) == 1- assert received_state["messages"][0].content == "hello async"---def test_pre_model_hook() -> None:- model = FakeToolCallingModel(tool_calls=[])-- # Test `llm_input_messages`- def pre_model_hook(state: AgentState):- return {"llm_input_messages": [HumanMessage("Hello!")]}-- agent = create_agent(model, [], pre_model_hook=pre_model_hook)- assert "pre_model_hook" in agent.nodes- result = agent.invoke({"messages": [HumanMessage("hi?")]})- assert result == {- "messages": [- _AnyIdHumanMessage(content="hi?"),- AIMessage(content="Hello!", id="0"),- ]- }-- # Test `messages`- def pre_model_hook(state: AgentState):- return {"messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES), HumanMessage("Hello!")]}-- agent = create_agent(model, [], pre_model_hook=pre_model_hook)- result = agent.invoke({"messages": [HumanMessage("hi?")]})- assert result == {- "messages": [- _AnyIdHumanMessage(content="Hello!"),- AIMessage(content="Hello!", id="1"),- ]- }---def test_post_model_hook() -> None:- class FlagState(AgentState):- flag: bool-- model = FakeToolCallingModel(tool_calls=[])-- def post_model_hook(state: FlagState) -> dict[str, bool]:- return {"flag": True}-- pmh_agent = create_agent(model, [], post_model_hook=post_model_hook, state_schema=FlagState)-- assert "post_model_hook" in pmh_agent.nodes-- result = pmh_agent.invoke({"messages": [HumanMessage("hi?")], "flag": False})- assert result["flag"] is True-- events = list(pmh_agent.stream({"messages": [HumanMessage("hi?")], "flag": False}))- assert events == [- {- "agent": {- "messages": [- AIMessage(- content="hi?",- additional_kwargs={},- response_metadata={},- id="1",- )- ]- }- },- {"post_model_hook": {"flag": True}},- ]---def test_post_model_hook_with_structured_output() -> None:- class WeatherResponse(BaseModel):- temperature: float = Field(description="The temperature in fahrenheit")-- tool_calls: list[list[ToolCall]] = [- [{"args": {}, "id": "1", "name": "get_weather"}],- [{"args": {"temperature": 75}, "id": "2", "name": "WeatherResponse"}],- ]-- def get_weather() -> str:- """Get the weather"""- return "The weather is sunny and 75°F."-- expected_structured_response = WeatherResponse(temperature=75)-- class State(AgentState):- flag: bool- structured_response: WeatherResponse-- def post_model_hook(state: State) -> Union[dict[str, bool], Command]:- return {"flag": True}-- model = FakeToolCallingModel(tool_calls=tool_calls)- agent = create_agent(- model,- [get_weather],- response_format=WeatherResponse,- post_model_hook=post_model_hook,- state_schema=State,- )-- assert "post_model_hook" in agent.nodes-- response = agent.invoke({"messages": [HumanMessage("What's the weather?")], "flag": False})- assert response["flag"] is True- assert response["structured_response"] == expected_structured_response-- # Reset the state of the model- model = FakeToolCallingModel(tool_calls=tool_calls)- agent = create_agent(- model,- [get_weather],- response_format=WeatherResponse,- post_model_hook=post_model_hook,- state_schema=State,- )-- events = list(agent.stream({"messages": [HumanMessage("What's the weather?")], "flag": False}))- assert events == [- {- "agent": {- "messages": [- AIMessage(- content="What's the weather?",- additional_kwargs={},- response_metadata={},- id="0",- tool_calls=[- {- "name": "get_weather",- "args": {},- "id": "1",- "type": "tool_call",- }- ],- )- ]- }- },- {"post_model_hook": {"flag": True}},- {- "tools": {- "messages": [- _AnyIdToolMessage(- content="The weather is sunny and 75°F.",- name="get_weather",- tool_call_id="1",- )- ]- }- },- {- "agent": {- "messages": [- AIMessage(- content="What's the weather?-What's the weather?-The weather is sunny and 75°F.",- additional_kwargs={},- response_metadata={},- id="1",- tool_calls=[- {- "name": "WeatherResponse",- "args": {"temperature": 75},- "id": "2",- "type": "tool_call",- }- ],- ),- _AnyIdToolMessage(- content="Returning structured response: {'temperature': 75.0}",- name="WeatherResponse",- tool_call_id="2",- ),- ],- "structured_response": WeatherResponse(temperature=75.0),- }- },- {"post_model_hook": {"flag": True}},- ]---def test_create_agent_inject_vars_with_post_model_hook() -> None:- store = InMemoryStore()- namespace = ("test",)- store.put(namespace, "test_key", {"bar": 3})-- def tool1(- some_val: int,- state: Annotated[dict, InjectedState],- store: Annotated[BaseStore, InjectedStore()],- ) -> str:- """Tool 1 docstring."""- store_val = store.get(namespace, "test_key").value["bar"]- return some_val + state["foo"] + store_val-- tool_call = {- "name": "tool1",- "args": {"some_val": 1},- "id": "some 0",- "type": "tool_call",- }-- def post_model_hook(state: dict) -> dict:- """Post model hook is injecting a new foo key."""- return {"foo": 2}-- model = FakeToolCallingModel(tool_calls=[[tool_call], []])- agent = create_agent(- model,- ToolNode([tool1], handle_tool_errors=False),- state_schema=AgentStateExtraKey,- store=store,- post_model_hook=post_model_hook,- )- input_message = HumanMessage("hi")- result = agent.invoke({"messages": [input_message], "foo": 2})- assert result["messages"] == [- input_message,- AIMessage(content="hi", tool_calls=[tool_call], id="0"),- _AnyIdToolMessage(content="6", name="tool1", tool_call_id="some 0"),- AIMessage("hi-hi-6", id="1"),- ]- assert result["foo"] == 2---def test_response_format_using_tool_choice() -> None:- """Test response format using tool choice."""-- class WeatherResponse(BaseModel):- temperature: float = Field(description="The temperature in fahrenheit")-- tool_calls: list[list[ToolCall]] = [- [{"args": {}, "id": "1", "name": "get_weather"}],- [{"args": {"temperature": "75"}, "id": "2", "name": "WeatherResponse"}],- ]-- def get_weather() -> str:- """Get the weather"""- return "The weather is sunny and 75°F."-- expected_structured_response = WeatherResponse(temperature=75)- model = FakeToolCallingModel(tool_calls=tool_calls)- agent = create_agent(- model,- [get_weather],- response_format=WeatherResponse,- )- response = agent.invoke(- {- "messages": [- {- "role": "user",- "content": "What's the weather?",- }- ]- }- )- assert response.get("structured_response") == expected_structured_response+# import dataclasses+# import inspect+# from types import UnionType+# from typing import (+# Annotated,+# Union,+# )++# import pytest+# from langchain_core.language_models import BaseChatModel+# from langchain_core.messages import (+# AIMessage,+# HumanMessage,+# MessageLikeRepresentation,+# RemoveMessage,+# SystemMessage,+# ToolCall,+# ToolMessage,+# )+# from langchain_core.runnables import RunnableConfig, RunnableLambda+# from langchain_core.tools import BaseTool, InjectedToolCallId, ToolException+# from langchain_core.tools import tool as dec_tool+# from langgraph.checkpoint.base import BaseCheckpointSaver+# from langgraph.graph import START, MessagesState, StateGraph+# from langgraph.graph.message import REMOVE_ALL_MESSAGES+# from langgraph.runtime import Runtime+# from langgraph.store.base import BaseStore+# from langgraph.store.memory import InMemoryStore+# from langgraph.types import Command, Interrupt, interrupt+# from pydantic import BaseModel, Field+# from typing_extensions import TypedDict++# from langchain.agents import (+# AgentState,+# create_agent,+# )+# from langchain.tools import (+# ToolNode,+# InjectedState,+# InjectedStore,+# )+# from langchain.tools.tool_node import (+# _get_state_args,+# _infer_handled_types,+# )++# from .any_str import AnyStr+# from .messages import _AnyIdHumanMessage, _AnyIdToolMessage+# from .model import FakeToolCallingModel++# pytestmark = pytest.mark.anyio+++# def test_no_prompt(sync_checkpointer: BaseCheckpointSaver) -> None:+# model = FakeToolCallingModel()++# agent = create_agent(+# model,+# [],+# checkpointer=sync_checkpointer,+# )+# inputs = [HumanMessage("hi?")]+# thread = {"configurable": {"thread_id": "123"}}+# response = agent.invoke({"messages": inputs}, thread, debug=True)+# expected_response = {"messages": [*inputs, AIMessage(content="hi?", id="0")]}+# assert response == expected_response++# saved = sync_checkpointer.get_tuple(thread)+# assert saved is not None+# assert saved.checkpoint["channel_values"] == {+# "messages": [+# _AnyIdHumanMessage(content="hi?"),+# AIMessage(content="hi?", id="0"),+# ],+# }+# assert saved.metadata == {+# "parents": {},+# "source": "loop",+# "step": 1,+# }+# assert saved.pending_writes == []+++# async def test_no_prompt_async(async_checkpointer: BaseCheckpointSaver) -> None:+# model = FakeToolCallingModel()++# agent = create_agent(model, [], checkpointer=async_checkpointer)+# inputs = [HumanMessage("hi?")]+# thread = {"configurable": {"thread_id": "123"}}+# response = await agent.ainvoke({"messages": inputs}, thread, debug=True)+# expected_response = {"messages": [*inputs, AIMessage(content="hi?", id="0")]}+# assert response == expected_response++# saved = await async_checkpointer.aget_tuple(thread)+# assert saved is not None+# assert saved.checkpoint["channel_values"] == {+# "messages": [+# _AnyIdHumanMessage(content="hi?"),+# AIMessage(content="hi?", id="0"),+# ],+# }+# assert saved.metadata == {+# "parents": {},+# "source": "loop",+# "step": 1,+# }+# assert saved.pending_writes == []+++# def test_system_message_prompt() -> None:+# prompt = SystemMessage(content="Foo")+# agent = create_agent(FakeToolCallingModel(), [], system_prompt=prompt)+# inputs = [HumanMessage("hi?")]+# response = agent.invoke({"messages": inputs})+# expected_response = {"messages": [*inputs, AIMessage(content="Foo-hi?", id="0", tool_calls=[])]}+# assert response == expected_response+++# def test_string_prompt() -> None:+# prompt = "Foo"+# agent = create_agent(FakeToolCallingModel(), [], system_prompt=prompt)+# inputs = [HumanMessage("hi?")]+# response = agent.invoke({"messages": inputs})+# expected_response = {"messages": [*inputs, AIMessage(content="Foo-hi?", id="0", tool_calls=[])]}+# assert response == expected_response+++# def test_callable_prompt() -> None:+# def prompt(state):+# modified_message = f"Bar {state['messages'][-1].content}"+# return [HumanMessage(content=modified_message)]++# agent = create_agent(FakeToolCallingModel(), [], system_prompt=prompt)+# inputs = [HumanMessage("hi?")]+# response = agent.invoke({"messages": inputs})+# expected_response = {"messages": [*inputs, AIMessage(content="Bar hi?", id="0")]}+# assert response == expected_response+++# async def test_callable_prompt_async() -> None:+# async def prompt(state):+# modified_message = f"Bar {state['messages'][-1].content}"+# return [HumanMessage(content=modified_message)]++# agent = create_agent(FakeToolCallingModel(), [], system_prompt=prompt)+# inputs = [HumanMessage("hi?")]+# response = await agent.ainvoke({"messages": inputs})+# expected_response = {"messages": [*inputs, AIMessage(content="Bar hi?", id="0")]}+# assert response == expected_response+++# def test_runnable_prompt() -> None:+# prompt = RunnableLambda(+# lambda state: [HumanMessage(content=f"Baz {state['messages'][-1].content}")]+# )++# agent = create_agent(FakeToolCallingModel(), [], system_prompt=prompt)+# inputs = [HumanMessage("hi?")]+# response = agent.invoke({"messages": inputs})+# expected_response = {"messages": [*inputs, AIMessage(content="Baz hi?", id="0")]}+# assert response == expected_response+++# def test_prompt_with_store() -> None:+# def add(a: int, b: int):+# """Adds a and b"""+# return a + b++# in_memory_store = InMemoryStore()+# in_memory_store.put(("memories", "1"), "user_name", {"data": "User name is Alice"})+# in_memory_store.put(("memories", "2"), "user_name", {"data": "User name is Bob"})++# def prompt(state, config, *, store):+# user_id = config["configurable"]["user_id"]+# system_str = store.get(("memories", user_id), "user_name").value["data"]+# return [SystemMessage(system_str)] + state["messages"]++# def prompt_no_store(state, config):+# return SystemMessage("foo") + state["messages"]++# model = FakeToolCallingModel()++# # test state modifier that uses store works+# agent = create_agent(+# model,+# [add],+# prompt=prompt,+# store=in_memory_store,+# )+# response = agent.invoke({"messages": [("user", "hi")]}, {"configurable": {"user_id": "1"}})+# assert response["messages"][-1].content == "User name is Alice-hi"++# # test state modifier that doesn't use store works+# agent = create_agent(+# model,+# [add],+# prompt=prompt_no_store,+# store=in_memory_store,+# )+# response = agent.invoke({"messages": [("user", "hi")]}, {"configurable": {"user_id": "2"}})+# assert response["messages"][-1].content == "foo-hi"+++# async def test_prompt_with_store_async() -> None:+# async def add(a: int, b: int):+# """Adds a and b"""+# return a + b++# in_memory_store = InMemoryStore()+# await in_memory_store.aput(("memories", "1"), "user_name", {"data": "User name is Alice"})+# await in_memory_store.aput(("memories", "2"), "user_name", {"data": "User name is Bob"})++# async def prompt(state, config, *, store):+# user_id = config["configurable"]["user_id"]+# system_str = (await store.aget(("memories", user_id), "user_name")).value["data"]+# return [SystemMessage(system_str)] + state["messages"]++# async def prompt_no_store(state, config):+# return SystemMessage("foo") + state["messages"]++# model = FakeToolCallingModel()++# # test state modifier that uses store works+# agent = create_agent(model, [add], system_prompt=prompt, store=in_memory_store)+# response = await agent.ainvoke(+# {"messages": [("user", "hi")]}, {"configurable": {"user_id": "1"}}+# )+# assert response["messages"][-1].content == "User name is Alice-hi"++# # test state modifier that doesn't use store works+# agent = create_agent(model, [add], system_prompt=prompt_no_store, store=in_memory_store)+# response = await agent.ainvoke(+# {"messages": [("user", "hi")]}, {"configurable": {"user_id": "2"}}+# )+# assert response["messages"][-1].content == "foo-hi"+++# @pytest.mark.parametrize("tool_style", ["openai", "anthropic"])+# @pytest.mark.parametrize("include_builtin", [True, False])+# def test_model_with_tools(tool_style: str, include_builtin: bool) -> None:+# model = FakeToolCallingModel(tool_style=tool_style)++# @dec_tool+# def tool1(some_val: int) -> str:+# """Tool 1 docstring."""+# return f"Tool 1: {some_val}"++# @dec_tool+# def tool2(some_val: int) -> str:+# """Tool 2 docstring."""+# return f"Tool 2: {some_val}"++# tools: list[BaseTool | dict] = [tool1, tool2]+# if include_builtin:+# tools.append(+# {+# "type": "mcp",+# "server_label": "atest_sever",+# "server_url": "https://some.mcp.somewhere.com/sse",+# "headers": {"foo": "bar"},+# "allowed_tools": [+# "mcp_tool_1",+# "set_active_account",+# "get_url_markdown",+# "get_url_screenshot",+# ],+# "require_approval": "never",+# }+# )+# # check valid agent constructor+# with pytest.raises(ValueError):+# create_agent(+# model.bind_tools(tools),+# tools,+# )+++# # Test removed: _validate_chat_history function no longer exists+# # def test__validate_messages() -> None:+# # pass+++# def test__infer_handled_types() -> None:+# def handle(e) -> str: # type: ignore+# return ""++# def handle2(e: Exception) -> str:+# return ""++# def handle3(e: ValueError | ToolException) -> str:+# return ""++# def handle4(e: Union[ValueError, ToolException]) -> str:+# return ""++# class Handler:+# def handle(self, e: ValueError) -> str:+# return ""++# handle5 = Handler().handle++# def handle6(e: Union[Union[TypeError, ValueError], ToolException]) -> str:+# return ""++# expected: tuple = (Exception,)+# actual = _infer_handled_types(handle)+# assert expected == actual++# expected = (Exception,)+# actual = _infer_handled_types(handle2)+# assert expected == actual++# expected = (ValueError, ToolException)+# actual = _infer_handled_types(handle3)+# assert expected == actual++# expected = (ValueError, ToolException)+# actual = _infer_handled_types(handle4)+# assert expected == actual++# expected = (ValueError,)+# actual = _infer_handled_types(handle5)+# assert expected == actual++# expected = (TypeError, ValueError, ToolException)+# actual = _infer_handled_types(handle6)+# assert expected == actual++# with pytest.raises(ValueError):++# def handler(e: str) -> str:+# return ""++# _infer_handled_types(handler)++# with pytest.raises(ValueError):++# def handler(e: list[Exception]) -> str:+# return ""++# _infer_handled_types(handler)++# with pytest.raises(ValueError):++# def handler(e: Union[str, int]) -> str:+# return ""++# _infer_handled_types(handler)+++# def test_react_agent_with_structured_response() -> None:+# class WeatherResponse(BaseModel):+# temperature: float = Field(description="The temperature in fahrenheit")++# tool_calls = [+# [{"args": {}, "id": "1", "name": "get_weather"}],+# [{"name": "WeatherResponse", "id": "2", "args": {"temperature": 75}}],+# ]++# def get_weather() -> str:+# """Get the weather"""+# return "The weather is sunny and 75°F."++# expected_structured_response = WeatherResponse(temperature=75)+# model = FakeToolCallingModel[WeatherResponse](+# tool_calls=tool_calls, structured_response=expected_structured_response+# )+# agent = create_agent(+# model,+# [get_weather],+# response_format=WeatherResponse,+# )+# response = agent.invoke({"messages": [HumanMessage("What's the weather?")]})+# assert response["structured_response"] == expected_structured_response+# assert len(response["messages"]) == 5++# # Check message types in message history+# msg_types = [m.type for m in response["messages"]]+# assert msg_types == [+# "human", # "What's the weather?"+# "ai", # "What's the weather?"+# "tool", # "The weather is sunny and 75°F."+# "ai", # structured response+# "tool", # artificial tool message+# ]++# assert [m.content for m in response["messages"]] == [+# "What's the weather?",+# "What's the weather?",+# "The weather is sunny and 75°F.",+# "What's the weather?-What's the weather?-The weather is sunny and 75°F.",+# "Returning structured response: {'temperature': 75.0}",+# ]+++# class CustomState(AgentState):+# user_name: str+++# def test_react_agent_update_state(+# sync_checkpointer: BaseCheckpointSaver,+# ) -> None:+# @dec_tool+# def get_user_name(tool_call_id: Annotated[str, InjectedToolCallId]):+# """Retrieve user name"""+# user_name = interrupt("Please provider user name:")+# return Command(+# update={+# "user_name": user_name,+# "messages": [+# ToolMessage("Successfully retrieved user name", tool_call_id=tool_call_id)+# ],+# }+# )++# def prompt(state: CustomState):+# user_name = state.get("user_name")+# if user_name is None:+# return state["messages"]++# system_msg = f"User name is {user_name}"+# return [{"role": "system", "content": system_msg}] + state["messages"]++# tool_calls = [[{"args": {}, "id": "1", "name": "get_user_name"}]]+# model = FakeToolCallingModel(tool_calls=tool_calls)+# agent = create_agent(+# model,+# [get_user_name],+# state_schema=CustomState,+# prompt=prompt,+# checkpointer=sync_checkpointer,+# )+# config = {"configurable": {"thread_id": "1"}}+# # Run until interrupted+# agent.invoke({"messages": [("user", "what's my name")]}, config)+# # supply the value for the interrupt+# response = agent.invoke(Command(resume="Archibald"), config)+# # confirm that the state was updated+# assert response["user_name"] == "Archibald"+# assert len(response["messages"]) == 4+# tool_message: ToolMessage = response["messages"][-2]+# assert tool_message.content == "Successfully retrieved user name"+# assert tool_message.tool_call_id == "1"+# assert tool_message.name == "get_user_name"+++# def test_react_agent_parallel_tool_calls(+# sync_checkpointer: BaseCheckpointSaver,+# ) -> None:+# human_assistance_execution_count = 0++# @dec_tool+# def human_assistance(query: str) -> str:+# """Request assistance from a human."""+# nonlocal human_assistance_execution_count+# human_response = interrupt({"query": query})+# human_assistance_execution_count += 1+# return human_response["data"]++# get_weather_execution_count = 0++# @dec_tool+# def get_weather(location: str) -> str:+# """Use this tool to get the weather."""+# nonlocal get_weather_execution_count+# get_weather_execution_count += 1+# return "It's sunny!"++# tool_calls = [+# [+# {"args": {"location": "sf"}, "id": "1", "name": "get_weather"},+# {"args": {"query": "request help"}, "id": "2", "name": "human_assistance"},+# ],+# [],+# ]+# model = FakeToolCallingModel(tool_calls=tool_calls)+# agent = create_agent(+# model,+# [human_assistance, get_weather],+# checkpointer=sync_checkpointer,+# )+# config = {"configurable": {"thread_id": "1"}}+# query = "Get user assistance and also check the weather"+# message_types = []+# for event in agent.stream({"messages": [("user", query)]}, config, stream_mode="values"):+# if messages := event.get("messages"):+# message_types.append([m.type for m in messages])++# assert message_types == [+# ["human"],+# ["human", "ai"],+# ["human", "ai", "tool"],+# ]++# # Resume+# message_types = []+# for event in agent.stream(Command(resume={"data": "Hello"}), config, stream_mode="values"):+# if messages := event.get("messages"):+# message_types.append([m.type for m in messages])++# assert message_types == [+# ["human", "ai"],+# ["human", "ai", "tool", "tool"],+# ["human", "ai", "tool", "tool", "ai"],+# ]++# assert human_assistance_execution_count == 1+# assert get_weather_execution_count == 1+++# class AgentStateExtraKey(AgentState):+# foo: int+++# def test_create_agent_inject_vars() -> None:+# """Test that the agent can inject state and store into tool functions."""+# store = InMemoryStore()+# namespace = ("test",)+# store.put(namespace, "test_key", {"bar": 3})++# def tool1(+# some_val: int,+# state: Annotated[dict, InjectedState],+# store: Annotated[BaseStore, InjectedStore()],+# ) -> str:+# """Tool 1 docstring."""+# store_val = store.get(namespace, "test_key").value["bar"]+# return some_val + state["foo"] + store_val++# tool_call = {+# "name": "tool1",+# "args": {"some_val": 1},+# "id": "some 0",+# "type": "tool_call",+# }+# model = FakeToolCallingModel(tool_calls=[[tool_call], []])+# agent = create_agent(+# model,+# ToolNode([tool1], handle_tool_errors=False),+# state_schema=AgentStateExtraKey,+# store=store,+# )+# result = agent.invoke({"messages": [{"role": "user", "content": "hi"}], "foo": 2})+# assert result["messages"] == [+# _AnyIdHumanMessage(content="hi"),+# AIMessage(content="hi", tool_calls=[tool_call], id="0"),+# _AnyIdToolMessage(content="6", name="tool1", tool_call_id="some 0"),+# AIMessage("hi-hi-6", id="1"),+# ]+# assert result["foo"] == 2+++# async def test_return_direct() -> None:+# @dec_tool(return_direct=True)+# def tool_return_direct(input: str) -> str:+# """A tool that returns directly."""+# return f"Direct result: {input}"++# @dec_tool+# def tool_normal(input: str) -> str:+# """A normal tool."""+# return f"Normal result: {input}"++# first_tool_call = [+# ToolCall(+# name="tool_return_direct",+# args={"input": "Test direct"},+# id="1",+# ),+# ]+# expected_ai = AIMessage(+# content="Test direct",+# id="0",+# tool_calls=first_tool_call,+# )+# model = FakeToolCallingModel(tool_calls=[first_tool_call, []])+# agent = create_agent(+# model,+# [tool_return_direct, tool_normal],+# )++# # Test direct return for tool_return_direct+# result = agent.invoke({"messages": [HumanMessage(content="Test direct", id="hum0")]})+# assert result["messages"] == [+# HumanMessage(content="Test direct", id="hum0"),+# expected_ai,+# ToolMessage(+# content="Direct result: Test direct",+# name="tool_return_direct",+# tool_call_id="1",+# id=result["messages"][2].id,+# ),+# ]+# second_tool_call = [+# ToolCall(+# name="tool_normal",+# args={"input": "Test normal"},+# id="2",+# ),+# ]+# model = FakeToolCallingModel(tool_calls=[second_tool_call, []])+# agent = create_agent(model, [tool_return_direct, tool_normal])+# result = agent.invoke({"messages": [HumanMessage(content="Test normal", id="hum1")]})+# assert result["messages"] == [+# HumanMessage(content="Test normal", id="hum1"),+# AIMessage(content="Test normal", id="0", tool_calls=second_tool_call),+# ToolMessage(+# content="Normal result: Test normal",+# name="tool_normal",+# tool_call_id="2",+# id=result["messages"][2].id,+# ),+# AIMessage(content="Test normal-Test normal-Normal result: Test normal", id="1"),+# ]++# both_tool_calls = [+# ToolCall(+# name="tool_return_direct",+# args={"input": "Test both direct"},+# id="3",+# ),+# ToolCall(+# name="tool_normal",+# args={"input": "Test both normal"},+# id="4",+# ),+# ]+# model = FakeToolCallingModel(tool_calls=[both_tool_calls, []])+# agent = create_agent(model, [tool_return_direct, tool_normal])+# result = agent.invoke({"messages": [HumanMessage(content="Test both", id="hum2")]})+# assert result["messages"] == [+# HumanMessage(content="Test both", id="hum2"),+# AIMessage(content="Test both", id="0", tool_calls=both_tool_calls),+# ToolMessage(+# content="Direct result: Test both direct",+# name="tool_return_direct",+# tool_call_id="3",+# id=result["messages"][2].id,+# ),+# ToolMessage(+# content="Normal result: Test both normal",+# name="tool_normal",+# tool_call_id="4",+# id=result["messages"][3].id,+# ),+# ]+++# def test__get_state_args() -> None:+# class Schema1(BaseModel):+# a: Annotated[str, InjectedState]++# class Schema2(Schema1):+# b: Annotated[int, InjectedState("bar")]++# @dec_tool(args_schema=Schema2)+# def foo(a: str, b: int) -> float:+# """return"""+# return 0.0++# assert _get_state_args(foo) == {"a": None, "b": "bar"}+++# def test_inspect_react() -> None:+# model = FakeToolCallingModel(tool_calls=[])+# agent = create_agent(model, [])+# inspect.getclosurevars(agent.nodes["agent"].bound.func)+++# def test_react_with_subgraph_tools(+# sync_checkpointer: BaseCheckpointSaver,+# ) -> None:+# class State(TypedDict):+# a: int+# b: int++# class Output(TypedDict):+# result: int++# # Define the subgraphs+# def add(state):+# return {"result": state["a"] + state["b"]}++# add_subgraph = (+# StateGraph(State, output_schema=Output).add_node(add).add_edge(START, "add").compile()+# )++# def multiply(state):+# return {"result": state["a"] * state["b"]}++# multiply_subgraph = (+# StateGraph(State, output_schema=Output)+# .add_node(multiply)+# .add_edge(START, "multiply")+# .compile()+# )++# multiply_subgraph.invoke({"a": 2, "b": 3})++# # Add subgraphs as tools++# def addition(a: int, b: int):+# """Add two numbers"""+# return add_subgraph.invoke({"a": a, "b": b})["result"]++# def multiplication(a: int, b: int):+# """Multiply two numbers"""+# return multiply_subgraph.invoke({"a": a, "b": b})["result"]++# model = FakeToolCallingModel(+# tool_calls=[+# [+# {"args": {"a": 2, "b": 3}, "id": "1", "name": "addition"},+# {"args": {"a": 2, "b": 3}, "id": "2", "name": "multiplication"},+# ],+# [],+# ]+# )+# tool_node = ToolNode([addition, multiplication], handle_tool_errors=False)+# agent = create_agent(+# model,+# tool_node,+# checkpointer=sync_checkpointer,+# )+# result = agent.invoke(+# {"messages": [HumanMessage(content="What's 2 + 3 and 2 * 3?")]},+# config={"configurable": {"thread_id": "1"}},+# )+# assert result["messages"] == [+# _AnyIdHumanMessage(content="What's 2 + 3 and 2 * 3?"),+# AIMessage(+# content="What's 2 + 3 and 2 * 3?",+# id="0",+# tool_calls=[+# ToolCall(name="addition", args={"a": 2, "b": 3}, id="1"),+# ToolCall(name="multiplication", args={"a": 2, "b": 3}, id="2"),+# ],+# ),+# ToolMessage(content="5", name="addition", tool_call_id="1", id=result["messages"][2].id),+# ToolMessage(+# content="6",+# name="multiplication",+# tool_call_id="2",+# id=result["messages"][3].id,+# ),+# AIMessage(content="What's 2 + 3 and 2 * 3?-What's 2 + 3 and 2 * 3?-5-6", id="1"),+# ]+++# def test_react_agent_subgraph_streaming_sync() -> None:+# """Test React agent streaming when used as a subgraph node sync version"""++# @dec_tool+# def get_weather(city: str) -> str:+# """Get the weather of a city."""+# return f"The weather of {city} is sunny."++# # Create a React agent+# model = FakeToolCallingModel(+# tool_calls=[+# [{"args": {"city": "Tokyo"}, "id": "1", "name": "get_weather"}],+# [],+# ]+# )++# agent = create_agent(+# model,+# tools=[get_weather],+# prompt="You are a helpful travel assistant.",+# )++# # Create a subgraph that uses the React agent as a node+# def react_agent_node(state: MessagesState, config: RunnableConfig) -> MessagesState:+# """Node that runs the React agent and collects streaming output."""+# collected_content = ""++# # Stream the agent output and collect content+# for msg_chunk, _msg_metadata in agent.stream(+# {"messages": [("user", state["messages"][-1].content)]},+# config,+# stream_mode="messages",+# ):+# if hasattr(msg_chunk, "content") and msg_chunk.content:+# collected_content += msg_chunk.content++# return {"messages": [("assistant", collected_content)]}++# # Create the main workflow with the React agent as a subgraph node+# workflow = StateGraph(MessagesState)+# workflow.add_node("react_agent", react_agent_node)+# workflow.add_edge(START, "react_agent")+# workflow.add_edge("react_agent", "__end__")+# compiled_workflow = workflow.compile()++# # Test the streaming functionality+# result = compiled_workflow.invoke({"messages": [("user", "What is the weather in Tokyo?")]})++# # Verify the result contains expected structure+# assert len(result["messages"]) == 2+# assert result["messages"][0].content == "What is the weather in Tokyo?"+# assert "assistant" in str(result["messages"][1])++# # Test streaming with subgraphs = True+# result = compiled_workflow.invoke(+# {"messages": [("user", "What is the weather in Tokyo?")]},+# subgraphs=True,+# )+# assert len(result["messages"]) == 2++# events = []+# for event in compiled_workflow.stream(+# {"messages": [("user", "What is the weather in Tokyo?")]},+# stream_mode="messages",+# subgraphs=False,+# ):+# events.append(event)++# assert len(events) == 0++# events = []+# for event in compiled_workflow.stream(+# {"messages": [("user", "What is the weather in Tokyo?")]},+# stream_mode="messages",+# subgraphs=True,+# ):+# events.append(event)++# assert len(events) == 3+# namespace, (msg, metadata) = events[0]+# # FakeToolCallingModel returns a single AIMessage with tool calls+# # The content of the AIMessage reflects the input message+# assert msg.content.startswith("You are a helpful travel assistant")+# namespace, (msg, metadata) = events[1] # ToolMessage+# assert msg.content.startswith("The weather of Tokyo is sunny.")+++# async def test_react_agent_subgraph_streaming() -> None:+# """Test React agent streaming when used as a subgraph node."""++# @dec_tool+# def get_weather(city: str) -> str:+# """Get the weather of a city."""+# return f"The weather of {city} is sunny."++# # Create a React agent+# model = FakeToolCallingModel(+# tool_calls=[+# [{"args": {"city": "Tokyo"}, "id": "1", "name": "get_weather"}],+# [],+# ]+# )++# agent = create_agent(+# model,+# tools=[get_weather],+# prompt="You are a helpful travel assistant.",+# )++# # Create a subgraph that uses the React agent as a node+# async def react_agent_node(state: MessagesState, config: RunnableConfig) -> MessagesState:+# """Node that runs the React agent and collects streaming output."""+# collected_content = ""++# # Stream the agent output and collect content+# async for msg_chunk, _msg_metadata in agent.astream(+# {"messages": [("user", state["messages"][-1].content)]},+# config,+# stream_mode="messages",+# ):+# if hasattr(msg_chunk, "content") and msg_chunk.content:+# collected_content += msg_chunk.content++# return {"messages": [("assistant", collected_content)]}++# # Create the main workflow with the React agent as a subgraph node+# workflow = StateGraph(MessagesState)+# workflow.add_node("react_agent", react_agent_node)+# workflow.add_edge(START, "react_agent")+# workflow.add_edge("react_agent", "__end__")+# compiled_workflow = workflow.compile()++# # Test the streaming functionality+# result = await compiled_workflow.ainvoke(+# {"messages": [("user", "What is the weather in Tokyo?")]}+# )++# # Verify the result contains expected structure+# assert len(result["messages"]) == 2+# assert result["messages"][0].content == "What is the weather in Tokyo?"+# assert "assistant" in str(result["messages"][1])++# # Test streaming with subgraphs = True+# result = await compiled_workflow.ainvoke(+# {"messages": [("user", "What is the weather in Tokyo?")]},+# subgraphs=True,+# )+# assert len(result["messages"]) == 2++# events = []+# async for event in compiled_workflow.astream(+# {"messages": [("user", "What is the weather in Tokyo?")]},+# stream_mode="messages",+# subgraphs=False,+# ):+# events.append(event)++# assert len(events) == 0++# events = []+# async for event in compiled_workflow.astream(+# {"messages": [("user", "What is the weather in Tokyo?")]},+# stream_mode="messages",+# subgraphs=True,+# ):+# events.append(event)++# assert len(events) == 3+# namespace, (msg, metadata) = events[0]+# # FakeToolCallingModel returns a single AIMessage with tool calls+# # The content of the AIMessage reflects the input message+# assert msg.content.startswith("You are a helpful travel assistant")+# namespace, (msg, metadata) = events[1] # ToolMessage+# assert msg.content.startswith("The weather of Tokyo is sunny.")+++# def test_tool_node_node_interrupt(+# sync_checkpointer: BaseCheckpointSaver,+# ) -> None:+# def tool_normal(some_val: int) -> str:+# """Tool docstring."""+# return "normal"++# def tool_interrupt(some_val: int) -> str:+# """Tool docstring."""+# return interrupt("provide value for foo")++# # test inside react agent+# model = FakeToolCallingModel(+# tool_calls=[+# [+# ToolCall(name="tool_interrupt", args={"some_val": 0}, id="1"),+# ToolCall(name="tool_normal", args={"some_val": 1}, id="2"),+# ],+# [],+# ]+# )+# config = {"configurable": {"thread_id": "1"}}+# agent = create_agent(+# model,+# [tool_interrupt, tool_normal],+# checkpointer=sync_checkpointer,+# )+# result = agent.invoke({"messages": [HumanMessage("hi?")]}, config)+# expected_messages = [+# _AnyIdHumanMessage(content="hi?"),+# AIMessage(+# content="hi?",+# id="0",+# tool_calls=[+# {+# "name": "tool_interrupt",+# "args": {"some_val": 0},+# "id": "1",+# "type": "tool_call",+# },+# {+# "name": "tool_normal",+# "args": {"some_val": 1},+# "id": "2",+# "type": "tool_call",+# },+# ],+# ),+# _AnyIdToolMessage(content="normal", name="tool_normal", tool_call_id="2"),+# ]+# assert result["messages"] == expected_messages++# state = agent.get_state(config)+# assert state.next == ("tools",)+# task = state.tasks[0]+# assert task.name == "tools"+# assert task.interrupts == (+# Interrupt(+# value="provide value for foo",+# id=AnyStr(),+# ),+# )diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_react_agent_graph.py b/libs/langchain_v1/tests/unit_tests/agents/test_react_agent_graph.pydeleted file mode 100644index aa7f281035..0000000000--- a/libs/langchain_v1/tests/unit_tests/agents/test_react_agent_graph.py+++ /dev/null@@ -1,58 +0,0 @@-from collections.abc import Callable-from typing import Union--import pytest-from pydantic import BaseModel-from syrupy.assertion import SnapshotAssertion--from langchain.agents import create_agent--from .model import FakeToolCallingModel--model = FakeToolCallingModel()---def tool() -> None:- """Testing tool."""---def pre_model_hook() -> None:- """Pre-model hook."""---def post_model_hook() -> None:- """Post-model hook."""---class ResponseFormat(BaseModel):- """Response format for the agent."""-- result: str--[email protected]("tools", [[], [tool]])[email protected]("pre_model_hook", [None, pre_model_hook])[email protected]("post_model_hook", [None, post_model_hook])-def test_react_agent_graph_structure(- snapshot: SnapshotAssertion,- tools: list[Callable],- pre_model_hook: Union[Callable, None],- post_model_hook: Union[Callable, None],-) -> None:- agent = create_agent(- model,- tools=tools,- pre_model_hook=pre_model_hook,- post_model_hook=post_model_hook,- )- try:- assert agent.get_graph().draw_mermaid(with_styles=False) == snapshot- except Exception as e:- msg = (- "The graph structure has changed. Please update the snapshot."- "Configuration used:\n"- f"tools: {tools}, "- f"pre_model_hook: {pre_model_hook}, "- f"post_model_hook: {post_model_hook}, "- )- raise ValueError(msg) from ediff --git a/libs/langchain_v1/tests/unit_tests/agents/test_response_format.py b/libs/langchain_v1/tests/unit_tests/agents/test_response_format.pyindex 8d2affb266..4101a03e77 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_response_format.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_response_format.py@@ -82,12 +82,14 @@ location_json_schema = {}+@tooldef get_weather() -> str:"""Get the weather."""return "The weather is sunny and 75°F."+@tooldef get_location() -> str:"""Get the current location."""@@ -748,7 +750,7 @@ class TestDynamicModelWithResponseFormat:)with patch(- "langchain.agents.middleware_agent._supports_provider_strategy",+ "langchain.agents.factory._supports_provider_strategy",side_effect=mock_supports_provider_strategy,):response = agent.invoke({"messages": [HumanMessage("What's the weather?")]})diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py b/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.pyindex e0bf1e9efe..d38849a040 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py@@ -5,7 +5,7 @@ from langchain_core.messages import AIMessage, HumanMessage, ToolCall, ToolMessafrom langchain_core.tools import toolfrom langgraph.checkpoint.memory import InMemorySaver-from langchain.agents.middleware_agent import create_agent+from langchain.agents.factory import create_agentfrom langchain.agents.middleware.tool_call_limit import (ToolCallLimitExceededError,ToolCallLimitMiddleware,@@ -318,7 +318,8 @@ def test_multiple_middleware_instances():model=model,tools=[search, calculator],middleware=[global_limiter, search_limiter],- ).compile(checkpointer=InMemorySaver())+ checkpointer=InMemorySaver(),+ )# First invocation: 2 search calls, 2 calculator calls (4 total)# Should succeed - within both limits@@ -365,8 +366,8 @@ def test_run_limit_with_multiple_human_messages():# Run limit of 2 tool calls per runmiddleware = ToolCallLimitMiddleware(run_limit=2)- agent = create_agent(model=model, tools=[search], middleware=[middleware]).compile(- checkpointer=InMemorySaver()+ agent = create_agent(+ model=model, tools=[search], middleware=[middleware], checkpointer=InMemorySaver())thread_config = {"configurable": {"thread_id": "test_thread"}}