Adapter to integrate LangChain with Vercel AI SDK SSE streaming
Project description
LangChain AI SDK Adapter
Adapter to integrate Python LangChain backends with Vercel AI SDK frontend clients.
Features
to_base_messages()- Convert AI SDK UIMessage format to LangChain messagesto_ui_message_stream()- Convert LangChain stream to AI SDK SSE formatcreate_ui_message_stream_response()- Create a Starlette/FastAPI StreamingResponse
Installation
pip install langchain-vercel-ai-sdk-adapter
With FastAPI support:
pip install langchain-vercel-ai-sdk-adapter[fastapi]
Usage
Basic Integration with FastAPI
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from langchain_anthropic import ChatAnthropic
from langchain_ai_sdk_adapter import to_base_messages, to_ui_message_stream, create_ui_message_stream_response
app = FastAPI()
@app.post("/chat")
async def chat(request: Request):
model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
langchain_messages = await to_base_messages(await request.json())
stream = await model.astream(langchain_messages)
return create_ui_message_stream_response(to_ui_message_stream(stream))
Integration with LangGraph
from langgraph.graph import StateGraph
from langchain_ai_sdk_adapter import to_base_messages, to_ui_message_stream, create_ui_message_stream_response
@app.post("/chat")
async def chat(request: Request):
langchain_messages = await to_base_messages(await request.json())
graph = create_graph()
stream = graph.astream_events(
{"messages": langchain_messages},
version="v2"
)
return create_ui_message_stream_response(to_ui_message_stream(stream))
SSE Format (AI SDK 5.0)
The adapter outputs SSE in AI SDK 5.0 protocol format, compatible with useChat:
data: {"type":"start","messageId":"msg_<uuid>"}
data: {"type":"text-start","id":"text_<uuid>"}
data: {"type":"text-delta","id":"text_<uuid>","delta":"Hello"}
data: {"type":"text-delta","id":"text_<uuid>","delta":" world"}
data: {"type":"text-end","id":"text_<uuid>"}
data: {"type":"finish","finishReason":"stop","usage":{"inputTokens":0,"outputTokens":0}}
Compatible with Vercel AI SDK 5.0 useChat hook.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz.
File metadata
- Download URL: langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
924ce47dcd1a3b0ce1067ea308be7e965e57f4c42a9099f163df3c6c3a8c5e12
|
|
| MD5 |
394fcebc69ecf3e806852ee886f12d5a
|
|
| BLAKE2b-256 |
3884ebc2e3eac296d5ce49bec7e9ff065b43fbba37067dc6a2d04583d66665c6
|
File details
Details for the file langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
576a7ccd8ff22a93c97514cf31f301a742796b2cc77816f3d05082a64bfb5444
|
|
| MD5 |
040f2d792b3b0d90bcbda0216b92e887
|
|
| BLAKE2b-256 |
86bca75c03c9bc8544f284208ea96415e504503493fea4a45c39c542c97e7a99
|