Migrating from LangGraph/LangChain to Haystack
Whether you're planning to migrate to Haystack or just comparing LangChain/LangGraph and Haystack to choose the proper framework for your AI application, this guide will help you map common patterns between frameworks.
In this guide, you'll learn how to translate core LangGraph concepts, like nodes, edges, and state, into Haystack components, pipelines, and agents. The goal is to preserve your existing logic while leveraging Haystack's flexible, modular ecosystem.
It's most accurate to think of Haystack as covering both LangChain and LangGraph territory: Haystack provides the building blocks for everything from simple sequential flows to fully agentic workflows with custom logic.
Why you might explore or migrate to Haystack
You might consider Haystack if you want to build your AI applications on a stable, actively maintained foundation with an intuitive developer experience.
- Unified orchestration framework. Haystack supports both deterministic pipelines and adaptive agentic flows, letting you combine them with the right level of autonomy in a single system.
- High-quality codebase and design. Haystack is engineered for clarity and reliability with well-tested components, predictable APIs, and a modular architecture that simply works.
- Ease of customization. Extend core components, add your own logic, or integrate custom tools with minimal friction.
- Reduced cognitive overhead. Haystack extends familiar ideas rather than introducing new abstractions, helping you stay focused on applying concepts, not learning them.
- Comprehensive documentation and learning resources. Every concept, from components and pipelines to agents and tools, is supported by detailed and well-maintained docs, tutorials, and educational content.
- Frequent release cycles. New features, improvements, and bug fixes are shipped regularly, ensuring that the framework evolves quickly while maintaining backward compatibility.
- Scalable from prototype to production. Start small and expand easily. The same code you use for a proof of concept can power enterprise-grade deployments through the whole Haystack ecosystem.
Concept mapping: LangGraph/LangChain → Haystack
Here's a table of key concepts and their approximate equivalents between the two frameworks. Use this when auditing your LangGraph/Langchain architecture and planning the migration.
| LangGraph/LangChain concept | Haystack equivalent | Notes |
|---|---|---|
| Node | Component | A unit of logic in both frameworks. In Haystack, a Component can run standalone, in a pipeline, or as a tool with agent. You can create custom components or use built-in ones like Generators and Retrievers. |
| Edge / routing logic | Connection / Branching / Looping | Pipelines connect component inputs and outputs with type-checked links. They support branching, routing, and loops for flexible flow control. |
| Graph / Workflow (nodes + edges) | Pipeline or Agent | LangGraph explicitly defines graphs; Haystack achieves similar orchestration through pipelines or Agents when adaptive logic is needed. |
| Subgraphs | SuperComponent | A SuperComponent wraps a full pipeline and exposes it as a single reusable component |
| Models / LLMs | ChatGenerator Components | Haystack's ChatGenerators unify access to open and proprietary models, with support for streaming, structured outputs, and multimodal data. |
Agent Creation (create_agent, multi-agent from LangChain) | Agent Component | Haystack provides a simple, pipeline-based Agent abstraction that handles reasoning, tool use, and multi-step execution. |
| Tool (Langchain) | Tool / PipelineTool / ComponentTool / MCPTool | Haystack exposes Python functions, pipelines, components, external APIs and MCP servers as agent tools. |
| Multi-Agent Collaboration (LangChain) | Multi-Agent System | Using ComponentTool, agents can use other agents as tools, enabling multi-agent architectures within one framework. |
Model Context Protocol load_mcp_tools MultiServerMCPClient | Model Context Protocol - MCPTool, MCPToolset, StdioServerInfo, StreamableHttpServerInfo | Haystack provides various MCP primitives for connecting multiple MCP servers and organizing MCP toolsets. |
| Memory (State, short-term, long-term) | Memory (Agent State, short-term, long-term) | Agent State provides a structured way to share data between tools and store intermediate results during agent execution. For long-term memory, Haystack offers memory stores such as Mem0MemoryStore and CogneeMemoryStore to persist conversation history across sessions. |
| Time travel (Checkpoints) | Breakpoints (Breakpoint, PipelineSnapshot) | Breakpoints let you pause, inspect, modify, and resume a pipeline for debugging or iterative development. |
| Human-in-the-Loop (Interrupts / Commands) | Human-in-the-loop (ConfirmationHook with confirmation strategies) | Haystack applies confirmation strategies through a ConfirmationHook registered under the Agent's before_tool hook point to pause or block the execution to gather user feedback |
Ecosystem and Tooling Mapping: LangChain → Haystack
At deepset, we're building the tools to make LLMs truly usable in production, open source and beyond.
- Haystack, AI Orchestration Framework → Open Source AI framework for building production-ready, AI-powered agents and applications, on your own or with community support.
- Haystack Enterprise Starter → Private and secure engineering support, advanced pipeline templates, deployment guides, and early access features for teams needing more support and guidance.
- Haystack Enterprise Platform → An enterprise-ready platform for teams running Gen AI apps in production, with security, governance, and scalability built in with a free version.
Here's the product equivalent of two ecosystems:
| LangChain Ecosystem | Haystack Ecosystem | Notes |
|---|---|---|
| LangChain, LangGraph, Deep Agents | Haystack | Core AI orchestration framework for components, pipelines, and agents. Supports deterministic workflows and agentic execution with explicit, modular building blocks. |
| LangSmith (Observability) | Haystack Enterprise Platform | Integrated tooling for building, debugging and iterating. Assemble agents and pipelines visually with the Builder, which includes component validation, testing and debugging. The Prompt Explorer is used to iterate and evaluate models and prompts. Built-in chat interfaces to enable fast SME and stakeholder feedback. Collaborative building environment for engineers and business. |
| LangSmith (Deployment) | Hayhooks Haystack Enterprise Starter (deployment guides + advanced best practice templates) Haystack Enterprise Platform (1-click deployment, on-prem/VPC options) | Multiple deployment paths: lightweight API exposure via Hayhooks, structured enterprise deployment patterns through Haystack Enterprise Starter, and full managed or self-hosted deployment through the Haystack Enterprise Platform. |
Code Comparison
Agentic Flows with Haystack vs LangGraph
Here's an example graph-based agent with access to a list of tools, comparing the LangGraph and Haystack APIs.
Step 1: Define tools
Both frameworks use a @tool decorator to expose Python functions as tools the LLM can call. The function signature and docstring define the tool's interface, which the LLM uses to understand when and how to invoke each tool.
# pip install haystack-ai anthropic-haystack
from haystack.tools import tool
# Define tools
@tool
def multiply(a: int, b: int) -> int:
"""Multiply `a` and `b`.
Args:
a: First int
b: Second int
"""
return a * b
@tool
def add(a: int, b: int) -> int:
"""Adds `a` and `b`.
Args:
a: First int
b: Second int
"""
return a + b
@tool
def divide(a: int, b: int) -> float:
"""Divide `a` and `b`.
Args:
a: First int
b: Second int
"""
return a / b
# pip install langchain-anthropic langgraph langchain
from langchain.tools import tool
# Define tools
@tool
def multiply(a: int, b: int) -> int:
"""Multiply `a` and `b`.
Args:
a: First int
b: Second int
"""
return a * b
@tool
def add(a: int, b: int) -> int:
"""Adds `a` and `b`.
Args:
a: First int
b: Second int
"""
return a + b
@tool
def divide(a: int, b: int) -> float:
"""Divide `a` and `b`.
Args:
a: First int
b: Second int
"""
return a / b
Step 2: Initialize the LLM
The frameworks connect tools to the LLM differently. In Haystack, you only initialize the ChatGenerator component here: the tools are passed to the Agent in Step 3, which forwards them to the LLM. In LangGraph, you first initialize the model, then bind tools using .bind_tools() to create a tool-enabled LLM instance.
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
# Initialize the LLM; the tools are passed to the Agent in Step 3
tools = [add, multiply, divide]
model = AnthropicChatGenerator(
model="claude-sonnet-4-5-20250929",
generation_kwargs={"temperature": 0},
)
from langchain.chat_models import init_chat_model
# Augment the LLM with tools
model = init_chat_model(
"claude-sonnet-4-5-20250929",
temperature=0,
)
tools = [add, multiply, divide]
tools_by_name = {tool.name: tool for tool in tools}
llm_with_tools = model.bind_tools(tools)
Step 3: Assemble the agent
This is where the frameworks diverge most. In Haystack, you create the Agent component from the chat generator and tools - the agentic loop is built in. The Agent accumulates the conversation (LLM replies and tool results) internally, executes the tool calls prepared by the LLM, and iterates until an exit condition is met. The default exit_conditions=["text"] stops the loop as soon as the LLM replies without tool calls; tool names can also be used to exit after a specific tool runs.
In LangGraph, you build the loop explicitly: a node function (llm_call) that invokes the LLM on the accumulated MessagesState, a node function (tool_node) that executes tool calls and wraps the results in ToolMessage objects, and a conditional edge function (should_continue) that decides whether to continue the loop or finish. You then wire nodes and edges together in a StateGraph and compile the graph into an executable agent.
from haystack.components.agents import Agent
# Create the agent - the agentic loop (LLM calls,
# tool execution, iteration) is built in
agent = Agent(
chat_generator=model,
tools=tools,
system_prompt="You are a helpful assistant tasked with performing arithmetic on a set of inputs.",
exit_conditions=["text"], # default
)
from typing import Literal
from langgraph.graph import MessagesState, StateGraph, START, END
from langchain.messages import SystemMessage, ToolMessage
# Node: the LLM decides whether to call a tool or not
def llm_call(state: MessagesState):
return {
"messages": [
llm_with_tools.invoke(
[
SystemMessage(
content="You are a helpful assistant tasked with performing arithmetic on a set of inputs."
)
]
+ state["messages"]
)
]
}
# Node: performs the tool calls
def tool_node(state: dict):
result = []
for tool_call in state["messages"][-1].tool_calls:
tool = tools_by_name[tool_call["name"]]
observation = tool.invoke(tool_call["args"])
result.append(ToolMessage(content=observation, tool_call_id=tool_call["id"]))
return {"messages": result}
# Conditional edge: route to the tool node or end
# based upon whether the LLM made a tool call
def should_continue(state: MessagesState) -> Literal["tool_node", END]:
last_message = state["messages"][-1]
if last_message.tool_calls:
return "tool_node"
return END
# Build workflow
agent_builder = StateGraph(MessagesState)
# Add nodes
agent_builder.add_node("llm_call", llm_call)
agent_builder.add_node("tool_node", tool_node)
# Add edges to connect nodes
agent_builder.add_edge(START, "llm_call")
agent_builder.add_conditional_edges(
"llm_call",
should_continue,
["tool_node", END]
)
agent_builder.add_edge("tool_node", "llm_call")
# Compile the agent
agent = agent_builder.compile()
Step 4: Run the agent
Finally, we execute the agent with a user message. Haystack calls .run() on the Agent with initial messages, while LangGraph calls .invoke() on the compiled agent. Both return the conversation history.
from haystack.dataclasses import ChatMessage
# Run the agent
result = agent.run(messages=[
ChatMessage.from_user(text="Add 3 and 4.")
])
print(result["last_message"].text)
from langchain.messages import HumanMessage
# Invoke
messages = [
HumanMessage(content="Add 3 and 4.")
]
messages = agent.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
Creating Agents
The Agentic Flows walkthrough above stepped through the agent loop piece by piece. In Haystack, the high-level Agent class wraps the full loop - LLM calls, tool invocation, and iteration - into a single component. LangGraph offers an equivalent shortcut through create_react_agent in langgraph.prebuilt. Both produce a ReAct-style agent that handles tool calling and multi-step reasoning automatically. Here are the complete examples side by side:
# pip install haystack-ai anthropic-haystack
from haystack.components.agents import Agent
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.tools import tool
@tool
def multiply(a: int, b: int) -> int:
"""Multiply `a` and `b`."""
return a * b
@tool
def add(a: int, b: int) -> int:
"""Add `a` and `b`."""
return a + b
# Create an agent - the agentic loop is handled automatically
agent = Agent(
chat_generator=AnthropicChatGenerator(
model="claude-sonnet-4-5-20250929",
generation_kwargs={"temperature": 0},
),
tools=[multiply, add],
system_prompt="You are a helpful assistant that performs arithmetic.",
)
result = agent.run(messages=[
ChatMessage.from_user("What is 3 multiplied by 7, then add 5?")
])
print(result["messages"][-1].text) # or print(result["last_message"].text)
# pip install langchain-anthropic langgraph
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage, SystemMessage
@tool
def multiply(a: int, b: int) -> int:
"""Multiply `a` and `b`."""
return a * b
@tool
def add(a: int, b: int) -> int:
"""Add `a` and `b`."""
return a + b
# Create an agent - the agentic loop is handled automatically
model = ChatAnthropic(
model="claude-sonnet-4-5-20250929",
temperature=0,
)
agent = create_agent(
model,
tools=[multiply, add],
system_prompt=SystemMessage(
content="You are a helpful assistant that performs arithmetic."
),
)
result = agent.invoke({
"messages": [HumanMessage(content="What is 3 multiplied by 7, then add 5?")]
})
print(result["messages"][-1].content)
Connecting to Document Stores
Document stores are the foundation of retrieval-augmented generation (RAG). In Haystack, document stores integrate natively with pipeline components like Retrievers and Prompt Builders via explicit typed connections. LangChain centers retrieval around its vector store abstraction composed using LCEL (LangChain Expression Language).
Both frameworks offer in-memory stores for prototyping and a wide range of production backends (Elasticsearch, Qdrant, Weaviate, Pinecone, and more) via integrations.
Step 1: Create a document store and add documents
# pip install haystack-ai sentence-transformers-haystack
from haystack import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.embedders.sentence_transformers import SentenceTransformersDocumentEmbedder
# Embed and write documents to the document store
document_store = InMemoryDocumentStore()
doc_embedder = SentenceTransformersDocumentEmbedder(
model="sentence-transformers/all-MiniLM-L6-v2"
)
docs = [
Document(content="Paris is the capital of France."),
Document(content="Berlin is the capital of Germany."),
Document(content="Tokyo is the capital of Japan."),
]
docs_with_embeddings = doc_embedder.run(docs)["documents"]
document_store.write_documents(docs_with_embeddings)
# pip install langchain-community langchain-huggingface sentence-transformers
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import InMemoryVectorStore
from langchain_core.documents import Document
# Embed and add documents to the vector store
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
vectorstore = InMemoryVectorStore(embedding=embeddings)
vectorstore.add_documents([
Document(page_content="Paris is the capital of France."),
Document(page_content="Berlin is the capital of Germany."),
Document(page_content="Tokyo is the capital of Japan."),
])
Step 2: Build a RAG pipeline
from haystack import Pipeline
from haystack_integrations.components.embedders.sentence_transformers import SentenceTransformersTextEmbedder
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
# ChatPromptBuilder expects a List[ChatMessage] as template
template = [ChatMessage.from_user("""
Given the following documents, answer the question.
{% for doc in documents %}{{ doc.content }}{% endfor %}
Question: {{ question }}
""")]
rag_pipeline = Pipeline()
rag_pipeline.add_component(
"text_embedder",
SentenceTransformersTextEmbedder(model="sentence-transformers/all-MiniLM-L6-v2")
)
rag_pipeline.add_component(
"retriever", InMemoryEmbeddingRetriever(document_store=document_store)
)
rag_pipeline.add_component(
"prompt_builder", ChatPromptBuilder(template=template)
)
rag_pipeline.add_component(
"llm", AnthropicChatGenerator(model="claude-sonnet-4-5-20250929")
)
rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
rag_pipeline.connect("retriever.documents", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder.prompt", "llm.messages")
result = rag_pipeline.run({
"text_embedder": {"text": "What is the capital of France?"},
"prompt_builder": {"question": "What is the capital of France?"},
})
print(result["llm"]["replies"][0].text)
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
def format_docs(docs):
return "\n".join(doc.page_content for doc in docs)
retriever = vectorstore.as_retriever()
model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
template = """
Given the following documents, answer the question.
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| model
| StrOutputParser()
)
result = rag_chain.invoke("What is the capital of France?")
print(result)
Using MCP Tools
Both frameworks support the Model Context Protocol (MCP), letting agents connect to external tools and services exposed by MCP servers. Haystack provides MCPTool and MCPToolset through the mcp-haystack integration package, which plug directly into the Agent component. LangChain's MCP support relies on the separate langchain-mcp-adapters package and requires an async workflow throughout.
# pip install haystack-ai mcp-haystack anthropic-haystack
from haystack_integrations.tools.mcp import MCPToolset, StdioServerInfo
from haystack.components.agents import Agent
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage
# Connect to an MCP server - tools are auto-discovered
toolset = MCPToolset(
server_info=StdioServerInfo(
command="uvx",
args=["mcp-server-fetch"],
)
)
agent = Agent(
chat_generator=AnthropicChatGenerator(model="claude-sonnet-4-5-20250929"),
tools=toolset,
system_prompt="You are a helpful assistant that can fetch web content.",
)
result = agent.run(messages=[
ChatMessage.from_user("Fetch the content from https://haystack.deepset.ai")
])
print(result["messages"][-1].text) # or print(result["last_message"].text)
# pip install langchain-mcp-adapters langgraph langchain-anthropic
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage, SystemMessage
model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
async def run():
client = MultiServerMCPClient(
{
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"],
"transport": "stdio",
}
}
)
tools = await client.get_tools()
agent = create_agent(
model,
tools,
system_prompt=SystemMessage(
content="You are a helpful assistant that can fetch web content."
),
)
result = await agent.ainvoke(
{
"messages": [
HumanMessage(content="Fetch the content from https://haystack.deepset.ai")
]
}
)
print(result["messages"][-1].content)
asyncio.run(run())
Hear from Haystack Users
See how teams across industries use Haystack to power their production AI systems, from RAG applications to agentic workflows.
"Haystack allows its users a production ready, easy to use framework that covers just about all of your needs, and allows you to write integrations easily for those it doesn't." - Josh Longenecker, GenAI Specialist at AWS
"Haystack's design philosophy significantly accelerates development and improves the robustness of AI applications, especially when heading towards production. The emphasis on explicit, modular components truly pays off in the long run." - Rima Hajou, Data & AI Technical Lead at Accenture
Featured Stories
- TELUS Agriculture & Consumer Goods Built an Agentic Chatbot with Haystack to Transform Trade Promotions Workflows
- Lufthansa Industry Solutions Uses Haystack to Power Enterprise RAG
Start Building with Haystack
👉 Thinking about migrating or evaluating Haystack? Jump right in with the Haystack Get Started guide or contact our team, we'd love to support you.