LangChain can retrieve in parallel with RunnableParallel. When do I still need LangGraph?

June 10, 2025

On June 10, 2025, I was researching Caché in hospital systems when I followed a link from the InterSystems learning platform to a basic RAG application. Then I looked through LangChain’s Python tutorials on tagging documents, extracting fields, and calling tools. At 11:54 I opened a page called Graphs in the integrations directory. Sixteen seconds later I searched for langgraph. After reading a chatbot example, I searched for the relationship between LangGraph and LangChain, then between LangGraph and GraphRAG, and finally for RunnableParallel示例代码 (RunnableParallel example code).12

The names are easier to explain with a practical question. Imagine a printer manual assistant. A user asks, “My P2 reports E17. What does that mean?” The program can search the user manual and service index simultaneously. The harder part comes next: if it finds instructions only for a different model, should it answer immediately or search again with the model name?

How does LangChain collect results when one question goes to two sources?

LangChain calls an invokable step a Runnable. Connect two steps with |, and the second receives the first step’s output. For example, extract the model and fault code from the user’s words, then pass both to a retriever. If two retrievers need the same input, RunnableParallel can invoke them simultaneously. It stores each result under its own name in a dictionary, letting downstream code distinguish the manual from the service records.3

I’ll use two fictional entries in place of search services. Put the three Python snippets below into one file in order. They need neither a model nor an API key. In a separate Python 3.12 environment, install these versions:

python -m pip install langchain-core==0.3.63 langgraph==0.4.8 langgraph-checkpoint==2.0.26 langgraph-prebuilt==0.2.2
from langchain_core.runnables import RunnableParallel

manual = {"P2 E17": "操作手册第 8 页:E17 表示标签尺寸与模板不一致。"}
service = {"P1 E17": "维修索引第 3 页:E17 表示纸仓盖未合好。"}

search = RunnableParallel(manual=manual.get, service=service.get)
print(search.invoke("P2 E17"))
# {'manual': '操作手册第 8 页:E17 表示标签尺寸与模板不一致。', 'service': None}

If both the model and the fault code are known, one round of retrieval is enough. The two searches can start together because neither needs the other’s answer. If the manual must return a model alias before the service records can be searched, there’s a data dependency. A sequential call expresses that directly.

If the user chose service records, should I search the manual too?

In the framework selection article I read at the time, 机器姬 wrote, “链可以分支,即根据逻辑条件将单一流程拆分为多个路径。” In other words, a chain can branch into several paths based on logical conditions.4 Suppose there are two choices beside the search box. Selecting “service records” should search just that index. RunnableBranch accepts condition/action pairs and executes the first action whose condition holds. Its final entry is the default action, used when none of the conditions hold.5

from langchain_core.runnables import RunnableBranch

lookup_one = RunnableBranch(
    (lambda r: r["source"] == "操作手册", lambda r: manual.get(r["query"])),
    (lambda r: r["source"] == "维修记录", lambda r: service.get(r["query"])),
    lambda r: "请选择操作手册或维修记录。",
)
print(lookup_one.invoke({"source": "维修记录", "query": "P1 E17"}))
# 维修索引第 3 页:E17 表示纸仓盖未合好。

One detail is easy to misread. Select “service records” and enter P2 E17, and the program returns None. It followed the selected service branch. That index just has no P2 entry. The default prompt appears only when the user hasn’t chosen either source.

If the interface has only a chat box, the user might simply say, “Look up the last service record.” You could expose both retrieval functions as agent tools and let the model choose one from that sentence. After the tool is selected, the program still has to decide whether an empty result warrants another search with different terms.4

You can only decide whether to retry after the first results arrive

Now suppose the device page already knows the model is P2, but the user enters only E17. When the first search returns nothing, the program can add the model from the page and try again. It needs to remember three things: what it searched for, which materials came back, and whether another attempt is available. LangGraph keeps these in state. A node does a step of work and returns an update. A conditional edge reads the updated state to choose the next step. Who decides whether to retry? Here a few lines of Python suffice. When the LangChain team launched LangGraph, it warned, “It is important that your cycles be able to end eventually!” The conditional edge below therefore also checks the number of attempts.67

flowchart LR
    search["查手册和维修索引"] --> check{"这次查到了吗?"}
    check -->|查到| done["结束"]
    check -->|首次落空且有型号| refine["补上型号"]
    refine --> search
    check -->|再次落空或缺型号| done
from typing import TypedDict
from langgraph.graph import END, START, StateGraph

class Lookup(TypedDict):
    model: str
    query: str
    attempts: int
    pages: dict[str, str | None]

def retrieve(state: Lookup):
    return {
        "pages": search.invoke(state["query"]),
        "attempts": state["attempts"] + 1,
    }

def choose_next(state: Lookup):
    if any(state["pages"].values()):
        return "finish"
    if state["attempts"] == 1 and state["model"]:
        return "refine"
    return "finish"

def add_model(state: Lookup):
    return {"query": f'{state["model"]} {state["query"]}'}

graph = StateGraph(Lookup)
graph.add_node("retrieve", retrieve)
graph.add_node("add_model", add_model)
graph.add_edge(START, "retrieve")
graph.add_conditional_edges(
    "retrieve", choose_next, {"finish": END, "refine": "add_model"}
)
graph.add_edge("add_model", "retrieve")
lookup = graph.compile()

result = lookup.invoke({"model": "P2", "query": "E17", "attempts": 0, "pages": {}})
print(result["attempts"], result["pages"]["manual"])  # 第 2 次查到手册第 8 页

Both branches return empty values on the first round. The conditional edge selects add_model, changing the query from E17 to P2 E17. On the second round the manual matches and the workflow ends. If the device is a P3 and neither round finds anything, it also ends. The interface can say it lacks instructions for that model. Exact dictionary matching lets this program treat a match as supporting evidence. With semantic retrieval, you must still check that the passage actually applies to P2. Even a crystal-clear P1 manual cannot explain a P2 fault code.

An ordinary Python loop is readable with just this retry condition. A graph pays off when you also need to wait for users to supply missing models or ask a person to resolve two conflicting manuals. A reader can trace each exit from the retrieval node and follow a returning edge to see exactly which steps repeat. This is one situation where combining the two tools, as 机器姬 suggests, makes sense. The retrieve node calls the existing LangChain retriever; the graph separately defines when to add the model and how many attempts to allow. Those rules can survive replacing the dictionary with a real manual search service.74

Where would a knowledge graph fit?

A single edge distinguishes the different “graphs” I was reading about. In a manual’s knowledge graph, an edge might say fault code E17 applies to model P2, a fact a retriever can verify. In the LangGraph above, an edge from add_model to retrieve means “after adding the model, run retrieval again.” A knowledge graph query can be wrapped in a retrieval node, just as that node could search two manuals. Extracting relationships from many documents and organizing evidence by community are the subjects of the Microsoft GraphRAG article.

I’d start with a question that actually determines the implementation: Can both searches start now, or does what the second one searches for depend on what comes back from the first? RunnableParallel expresses the first case clearly. In the second, start with a condition or a loop. Move that part to StateGraph when you need to inspect what each step retains, which conditions allow a return, and which require termination.

References

Footnotes

  1. InterSystems. (n.d.). Exploring a basic RAG application. InterSystems Learning Services. 基础 RAG 应用课程。当时的工作背景见在已有的医院信息系统上开发 AI 功能。 ↩

  2. 深度学习机器. (2024, December 25). LangGraph:基于图结构的大模型智能体开发框架. 博客园. “基本介绍”里的状态、节点和边。weixin_44217158. (n.d.). 使用LangChain中的RunnableParallel进行并行化. CSDN. 当时打开的并行示例。 ↩

  3. LangChain. (2025). RunnableSequence and RunnableParallel (langchain-core 0.3.63) [Source code]. GitHub. 顺序组合和并行分发同一输入与按键收集结果。 ↩

  4. 机器姬. (2025, March 12). 专业场景建议:用LangChain还LangGraph? 知乎专栏. 引文见“LangChain 和 LangGraph 的基础组件”中 Chain 条目,工具选择与组合建议分别见“工具调用”和“那么,应该在什么情况下使用?”。 ↩ ↩2 ↩3

  5. LangChain. (2025). RunnableBranch (langchain-core 0.3.63) [Source code]. GitHub. 按顺序测试条件与默认分支。 ↩

  6. The LangChain Team. (2024, January 17). LangGraph. LangChain Blog. 介绍 END 节点和循环终止条件的段落。 ↩

  7. LangChain. (2025). StateGraph (LangGraph 0.4.8) [Source code]. GitHub. 状态更新、条件边和编译。 ↩ ↩2

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-06-10). LangChain can retrieve in parallel with RunnableParallel. When do I still need LangGraph?. https://caiborui.com/blog/2025-06-10-zh-langchain-runnableparallel-or-langgraph-entrans

@misc{borui2025-1c6b58bc,
  author = {borui},
  title = {{LangChain can retrieve in parallel with RunnableParallel. When do I still need LangGraph?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-06-10-zh-langchain-runnableparallel-or-langgraph-entrans}
}