Skip to main content

LangChain integration guide

How to use LangGraph workflows with LiveKit Agents.

Available in
Python

Overview

LangChain is a framework for developing applications powered by large language models. The LiveKit LangChain plugin allows you to integrate existing LangGraph workflows as LLMs in your voice AI applications.

Quick reference

This section includes a basic usage example and some reference material. For links to more detailed documentation, see Additional resources.

Installation

Install the LiveKit LangChain plugin from PyPI:

pip install "livekit-plugins-langchain~=1.1"

Usage

Use LangGraph workflows within an AgentSession by wrapping them with the LLMAdapter. For example, you can use this LLM in the Voice AI quickstart.

from langgraph.graph import StateGraph
from livekit.agents import AgentSession, Agent
from livekit.plugins import langchain
# Define your LangGraph workflow
def create_workflow():
workflow = StateGraph(...)
# Add your nodes and edges
return workflow.compile()
# Use the workflow as an LLM
session = AgentSession(
llm=langchain.LLMAdapter(
graph=create_workflow()
),
# ... stt, tts, vad, turn_detection, etc.
)

The LLMAdapter automatically converts the LiveKit chat context to LangChain messages. The mapping is as follows:

  • system and developer messages to SystemMessage
  • user messages to HumanMessage
  • assistant messages to AIMessage

Parameters

This section describes the available parameters for the LLMAdapter. See the plugin reference for a complete list of all available parameters.

graphPregelProtocolRequired

The LangGraph workflow to use as an LLM. Must be a locally compiled graph. To learn more, see Graph Definitions.

configRunnableConfig | NoneOptionalDefault: None

Configuration options for the LangGraph workflow execution. This can include runtime configuration, callbacks, and other LangGraph-specific options. To learn more, see RunnableConfig.

Additional resources

The following resources provide more information about using LangChain with LiveKit Agents.