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
Metadata
Release files for langchain-vercel-ai-sdk-adapter 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz
| Download URL | langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
924ce47dcd1a3b0ce1067ea308be7e965e57f4c42a9099f163df3c6c3a8c5e12
|
|
BLAKE2b-256 checksum How to use checksums |
3884ebc2e3eac296d5ce49bec7e9ff065b43fbba37067dc6a2d04583d66665c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl
| Download URL | langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
576a7ccd8ff22a93c97514cf31f301a742796b2cc77816f3d05082a64bfb5444
|
|
BLAKE2b-256 checksum How to use checksums |
86bca75c03c9bc8544f284208ea96415e504503493fea4a45c39c542c97e7a99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|