Skip to main content

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 messages
  • to_ui_message_stream() - Convert LangChain stream to AI SDK SSE format
  • create_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz.

File metadata

File hashes

Hashes for langchain_vercel_ai_sdk_adapter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 924ce47dcd1a3b0ce1067ea308be7e965e57f4c42a9099f163df3c6c3a8c5e12
MD5 394fcebc69ecf3e806852ee886f12d5a
BLAKE2b-256 3884ebc2e3eac296d5ce49bec7e9ff065b43fbba37067dc6a2d04583d66665c6

See more details on using hashes here.

File details

Details for the file langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_vercel_ai_sdk_adapter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 576a7ccd8ff22a93c97514cf31f301a742796b2cc77816f3d05082a64bfb5444
MD5 040f2d792b3b0d90bcbda0216b92e887
BLAKE2b-256 86bca75c03c9bc8544f284208ea96415e504503493fea4a45c39c542c97e7a99

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page