AG-UI LangGraph Integration

0.0.33 · active · verified Thu Apr 16

ag-ui-langgraph is a Python library that provides a comprehensive integration of the Agent User Interaction (AG-UI) protocol with LangGraph. It enables standardized, event-driven communication between frontend applications and LangGraph-powered AI agents, facilitating real-time streaming interactions and state synchronization. The current version is 0.0.33 and it appears to have a relatively active release cadence with frequent updates to align with LangChain/LangGraph developments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to expose a simple LangGraph agent as a FastAPI endpoint using `ag-ui-langgraph`. It defines a basic `StateGraph` with a single node that calls an OpenAI model, then integrates it into a FastAPI application, ready to serve AG-UI compatible requests.

import os
from fastapi import FastAPI
from langgraph.graph import StateGraph, MessagesState
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, AIMessage
from ag_ui_langgraph import add_langgraph_fastapi_endpoint

# Ensure OPENAI_API_KEY is set in your environment
os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY', 'your_openai_api_key_here')

# Define your LangGraph workflow
class AgentState(MessagesState):
    pass

def call_model(state):
    messages = state['messages']
    model = ChatOpenAI(model="gpt-4o-mini") # Using a cost-effective model for quickstart
    response = model.invoke(messages)
    return {"messages": [response]}

workflow = StateGraph(AgentState)
workflow.add_node("oracle", call_model)
workflow.set_entry_point("oracle")
workflow.set_finish_point("oracle")

graph = workflow.compile()

# Create a FastAPI app
app = FastAPI()

# Integrate LangGraph with FastAPI using ag-ui-langgraph
# The endpoint will be available at /agent
add_langgraph_fastapi_endpoint(app, graph, "/agent")

# To run this:
# 1. Save as, e.g., main.py
# 2. pip install 'fastapi[all]' uvicorn ag-ui-langgraph langchain-openai langgraph
# 3. uvicorn main:app --reload --port 8000
# 4. Access http://localhost:8000/agent with a compatible AG-UI frontend
#    or use curl:
#    curl -X POST "http://localhost:8000/agent" \
#      -H "Content-Type: application/json" \
#      -d '{ "thread_id": "test_thread_123", "messages": [{ "role": "user", "content": "Hello!" }] }'

view raw JSON →