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-ai-sdk-adapter

With FastAPI support:

pip install langchain-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

The adapter produces SSE in AI SDK useChat format:

data: {"id":"msg_abc123","role":"assistant","content":"Hello"}\n\n
data: {"id":"msg_abc123","role":"assistant","content":" world"}\n\n
...
data: [DONE]\n\n

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.1.0.tar.gz (5.4 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.1.0.tar.gz.

File metadata

File hashes

Hashes for langchain_vercel_ai_sdk_adapter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2fae21a05244c495a30bb98a9a2064b06f03576f0096722835af1947cf4cb94a
MD5 d1970df239672ac173cd99462347e57e
BLAKE2b-256 9bea0ffdbaa1739a530d9680d05fe62e131be73c31f8205746ca5fa13a1f0a1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langchain_vercel_ai_sdk_adapter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83922ea259769f011ab4899622674de2d6d308e6ef1a0a5bd6508f6dabe1fccd
MD5 da99036bf38c11cda95afea08aa919bd
BLAKE2b-256 7922222982bfc432f3f9b612b14a9f1af16e297104ce6d141e1edd9b25e23915

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