How Open-SWE Integrates with LangChain: Architecture and Implementation
Open-SWE integrates with LangChain by leveraging LangChain Core message types, the init_chat_model factory for LLM abstraction, and DeepAgents orchestration on top of LangGraph state machines, enabling a customizable coding assistant with deterministic execution and multimodal support.
The langchain-ai/open-swe repository demonstrates how Open-SWE integrates with LangChain to build a powerful internal coding assistant. This architecture combines LangChain's standardized message abstractions and chat model factories with DeepAgents orchestration and LangGraph persistence layers to create an extensible agent framework.
Core LangChain Message Abstractions
Open-SWE builds upon LangChain Core's message model to represent and manipulate LLM-generated content. The framework uses ContentBlock, AIMessage, and related types to normalize multimodal conversation data.
In agent/utils/messages.py, the system imports ContentBlock directly from langchain_core.messages to handle message normalization. The web application layer in agent/webapp.py leverages create_text_block from langchain_core.messages.content to construct properly formatted text content blocks for LLM consumption.
Chat Model Integration via init_chat_model
All LLM calls in Open-SWE funnel through LangChain's init_chat_model factory, which abstracts provider-specific implementations such as OpenAI, Anthropic, and others behind a unified interface.
The agent/utils/model.py file implements a make_model wrapper that calls init_chat_model to construct the underlying model instance. This implementation specifically handles provider-specific configuration, including injecting the OpenAI response streaming URL when model IDs begin with the openai: prefix. This abstraction allows Open-SWE to switch between different LLM providers without modifying the core agent logic.
DeepAgents and LangGraph Orchestration
The primary integration point for agent behavior is the create_deep_agent function from the DeepAgents package, which composes a LangChain-compatible agent on top of a LangGraph execution graph.
In agent/server.py, the system imports create_deep_agent and invokes it to build the final agent instance. This construction passes the LangChain model (from make_model), system prompts, tool registries, and middleware pipelines to create a deterministic state machine. The resulting agent supports sub-agent spawning, middleware processing, and precise state handling through LangGraph's graph-based execution model.
For long-running conversations, Open-SWE utilizes the LangGraphClient from langgraph_sdk. The agent/webapp.py module acquires this client via get_client to read and write per-thread metadata, including sandbox IDs and repository configuration, enabling persistent agent state across interactions.
Tool Integration with LangChain Interface
Open-SWE registers LangChain-style tools that the LLM can invoke through standard tool-calling interfaces. These tools follow LangChain's tool definition patterns and integrate directly with the agent's execution loop.
The agent/tools/ directory contains implementations for http_request, fetch_url, commit_and_open_pr, linear_comment, slack_thread_reply, and github_comment. In agent/server.py, these tools are collected into a list and passed directly to create_deep_agent, making them available for the LLM to invoke during task execution. This design allows external service integration (GitHub, Slack, Linear) through LangChain's standardized tool interface.
Multimodal Support via LangChain Core
When processing visual content, Open-SWE utilizes LangChain Core's multimodal utilities to embed images into messages. The agent/utils/multimodal.py file imports create_image_block from LangChain Core to handle image data, while agent/webapp.py uses create_text_block for text content. This enables the coding assistant to process architecture diagrams, screenshots, and other visual inputs alongside code and text.
Implementation Examples
Building an Open-SWE Agent
This example demonstrates the core integration pattern: initializing a LangChain model and wrapping it with DeepAgents orchestration.
from open_swe.agent.utils.model import make_model
from deepagents import create_deep_agent
from open_swe.agent.tools import (
http_request,
fetch_url,
commit_and_open_pr,
linear_comment,
slack_thread_reply,
github_comment,
)
# Initialize any LangChain-compatible model
model = make_model(
"anthropic:claude-opus-4-6",
temperature=0,
max_tokens=20_000,
)
# Construct the agent with LangChain tools and DeepAgents middleware
agent = create_deep_agent(
model=model,
system_prompt="You are an internal coding assistant.",
tools=[
http_request,
fetch_url,
commit_and_open_pr,
linear_comment,
slack_thread_reply,
github_comment,
],
middleware=[],
)
Invoking the Agent with State Management
This pattern shows how Open-SWE uses LangGraph's state management through the RunnableConfig interface.
from langgraph.graph.state import RunnableConfig
# Configure thread metadata for LangGraph persistence
config: RunnableConfig = {
"configurable": {
"thread_id": "thread-123",
"repo": {"owner": "langchain-ai", "name": "open-swe"},
"__is_for_execution__": True,
},
"metadata": {},
}
# Get the configured agent (spawns sandbox and clones repo)
agent = await get_agent(config)
# Send user prompt through the LangChain-compatible interface
response = await agent.ainvoke(
{"messages": [{"role": "user", "content": "Create a new Python file that prints hello"}]},
config,
)
Processing Multimodal Content
This example utilizes LangChain Core utilities for handling mixed content types.
from langchain_core.messages.content import create_text_block, create_image_block
# Construct multimodal message using LangChain primitives
msg = [
create_text_block("Here is an architecture diagram:"),
create_image_block(url="https://example.com/arch.png"),
]
# Pass to the agent for processing
response = await agent.ainvoke({"messages": msg}, config)
Summary
- LangChain Core Types: Open-SWE uses
ContentBlockand message utilities fromlangchain_core.messagesto standardize multimodal content handling. - Model Abstraction: The
init_chat_modelfactory inagent/utils/model.pyenables provider-agnostic LLM integration supporting OpenAI, Anthropic, and others. - DeepAgents Orchestration:
create_deep_agentinagent/server.pycombines LangChain components with LangGraph state machines for deterministic execution. - Persistent State: The
LangGraphClientmanages thread metadata and sandbox state through thelanggraph_sdkintegration inagent/webapp.py. - Tool Ecosystem: LangChain-compatible tools in
agent/tools/provide standardized interfaces for GitHub, Slack, Linear, and HTTP operations.
Frequently Asked Questions
What LangChain components does Open-SWE use?
Open-SWE utilizes LangChain Core for message abstractions (ContentBlock, AIMessage), LangChain Chat Models via init_chat_model for LLM provider abstraction, and LangGraph through the langgraph_sdk for state persistence and thread management. The integration also uses DeepAgents, which builds upon LangChain's agent architecture to provide advanced orchestration capabilities.
How does Open-SWE handle different LLM providers?
The framework handles provider diversity through the make_model function in agent/utils/model.py, which wraps LangChain's init_chat_model. This factory method accepts model identifiers like anthropic:claude-opus-4-6 or openai:gpt-4 and automatically configures the appropriate client, handling provider-specific parameters such as streaming URLs and authentication without requiring changes to the agent logic.
What is the role of DeepAgents in the integration?
DeepAgents serves as the orchestration layer that combines LangChain's model and tool interfaces with LangGraph's state machine execution. The create_deep_agent function accepts LangChain-compatible models and tools, then constructs an agent capable of deterministic state management, sub-agent spawning, and middleware pipeline processing. This extends standard LangChain agents with advanced workflow capabilities required for complex coding tasks.
How does Open-SWE manage conversation state?
Conversation state persists through the LangGraphClient from langgraph_sdk, as implemented in agent/webapp.py. The system stores per-thread metadata—including sandbox IDs, repository configurations, and execution context—in a LangGraph server. This enables the agent to resume operations across multiple interactions while maintaining context about the current coding environment and previous tool invocations.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →