pythonintermediate
LangChain Streaming Callback Handler
Capture LLM tokens as they stream using a custom callback handler for real-time UI updates.
pythonPress ⌘/Ctrl + Shift + C to copy
from langchain_core.callbacks import BaseCallbackHandler
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
import sys
class StreamHandler(BaseCallbackHandler):
def __init__(self):
self.text = ''
def on_llm_new_token(self, token: str, **kwargs) -> None:
self.text += token
print(token, end='', flush=True)
def on_llm_end(self, *args, **kwargs) -> None:
print()
handler = StreamHandler()
llm = ChatOpenAI(model='gpt-4o-mini', streaming=True, callbacks=[handler])
prompt = ChatPromptTemplate.from_template('Write a haiku about {topic}')
chain = prompt | llm
chain.invoke({'topic': 'data pipelines'})
print(f'Total chars: {len(handler.text)}')Use Cases
- streaming UI
- token capture
- real-time display
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonadvanced
OpenAI Realtime API WebSocket
Connect to the OpenAI Realtime API via WebSocket for low-latency voice and text streaming.
Best for: voice AI
#openai#realtime
typescriptintermediate
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Best for: chatbot UI
#openai#streaming
pythonbeginner
LangChain Prompt Chain (Python)
Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.
Best for: prompt chaining
#langchain#openai
typescriptintermediate
Next.js AI Streaming Route Handler
Stream OpenAI responses from a Next.js App Router route handler using the Vercel AI SDK.
Best for: AI chatbot backend
#nextjs#openai