Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Created January 23, 2026 17:41
Show Gist options
  • Select an option

  • Save wilyJ80/629077c49a20751e3d41099ad8706142 to your computer and use it in GitHub Desktop.

Select an option

Save wilyJ80/629077c49a20751e3d41099ad8706142 to your computer and use it in GitHub Desktop.
chainlit-langchain
from typing import Any, cast
import chainlit as cl
from langchain_core.runnables import Runnable, RunnableConfig
from langchain_google_genai import ChatGoogleGenerativeAI
@cl.on_chat_start
async def on_chat_start():
model = 'gemini-2.5-flash-lite'
llm = ChatGoogleGenerativeAI(model=model)
cl.user_session.set("runnable", llm)
@cl.on_message
async def on_message(message: cl.Message):
runnable = cast(Runnable[Any, Any], cl.user_session.get("runnable"))
msg = cl.Message(content="")
async for chunk in runnable.astream(
message.content,
config = RunnableConfig(callbacks=[cl.LangchainCallbackHandler()])
):
await msg.stream_token(chunk.content)
await msg.send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment