> ## Documentation Index
> Fetch the complete documentation index at: https://langchain.idochub.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 子图概览

<警告>
  **Alpha 版本提示：** 本文档涵盖的是 **v1-alpha** 版本。内容尚不完整，且可能随时更改。

  如需查阅最新稳定版本，请参阅当前的 [LangGraph Python 文档](https://langchain-ai.github.io/langgraph/) 或 [LangGraph JavaScript 文档](https://langchain-ai.github.io/langgraphjs/)。
</警告>

子图是一个[图](/oss/python/langgraph/graph-api#graphs)，它被用作另一个图中的[节点](/oss/python/langgraph/graph-api#nodes) —— 这是将封装概念应用于LangGraph的具体体现。子图允许你构建包含多个组件的复杂系统，而这些组件本身也是图。

[子图](/oss/images/subgraph.png)

子图的用途包括：

* 构建[多智能体系统](/oss/python/langchain/multi-agent)
* 当你希望在多个图中复用一组节点时
* 当你希望不同团队能够独立开发图的不同部分时，你可以将每个部分定义为一个子图；只要子图接口（输入和输出模式）保持一致，父图即可在不了解子图内部细节的情况下构建完成

在添加子图时，主要问题在于父图与子图之间如何通信，即它们在图执行过程中如何传递[状态](/oss/python/langgraph/graph-api#state)。存在两种情况：

* 父图与子图在其状态[模式](/oss/python/langgraph/graph-api#state)中具有**共享的状态键**。在这种情况下，你可以[将子图作为节点直接包含在父图中](/oss/python/langgraph/use-subgraphs#shared-state-schemas)

  ```python {highlight={12,17}} theme={null}
  from langgraph.graph import StateGraph, MessagesState, START

  # 子图

  def call_model(state: MessagesState):
      response = model.invoke(state["messages"])
      return {"messages": response}

  subgraph_builder = StateGraph(State)
  subgraph_builder.add_node(call_model)
  ...
  subgraph = subgraph_builder.compile()

  # 父图

  builder = StateGraph(State)
  builder.add_node("subgraph_node", subgraph)
  builder.add_edge(START, "subgraph_node")
  graph = builder.compile()
  ...
  graph.invoke({"messages": [{"role": "user", "content": "hi!"}]})
  ```

* 父图与子图具有**不同的模式**（其状态[模式](/oss/python/langgraph/graph-api#state)中没有共享的状态键）。在这种情况下，你必须[在父图的某个节点内部调用子图](/oss/python/langgraph/use-subgraphs#different-state-schemas)：当父图与子图具有不同的状态模式，且你需要在调用子图前后对状态进行转换时，这种方法非常有用。
  ```python {highlight={7,11,19,28}} theme={null}
  from typing_extensions import TypedDict, Annotated
  from langchain_core.messages import AnyMessage
  from langgraph.graph import StateGraph, MessagesState, START
  from langgraph.graph.message import add_messages

  class SubgraphMessagesState(TypedDict):
      subgraph_messages: Annotated[list[AnyMessage], add_messages]

  # 子图

  def call_model(state: SubgraphMessagesState):
      response = model.invoke(state["subgraph_messages"])
      return {"subgraph_messages": response}

  subgraph_builder = StateGraph(SubgraphMessagesState)
  subgraph_builder.add_node("call_model_from_subgraph", call_model)
  subgraph_builder.add_edge(START, "call_model_from_subgraph")
  ...
  subgraph = subgraph_builder.compile()

  # 父图

  def call_subgraph(state: MessagesState):
      response = subgraph.invoke({"subgraph_messages": state["messages"]})
      return {"messages": response["subgraph_messages"]}

  builder = StateGraph(State)
  builder.add_node("subgraph_node", call_subgraph)
  builder.add_edge(START, "subgraph_node")
  graph = builder.compile()
  ...
  graph.invoke({"messages": [{"role": "user", "content": "hi!"}]})
  ```
