Hey Streamlit Community!
I’m excited to share a new Python package I’ve been working on: streamlit-openai
— a Streamlit component designed to make it super easy to build interactive chat apps powered by OpenAI’s latest models.
Whether you’re prototyping a personal chatbot, building tools with GPT-4o, or integrating advanced capabilities like file search, code interpretation, and image analysis — streamlit-openai
helps you get there faster with minimal code.
Key Features
Real-time streaming with OpenAI’s Responses API
Function calling for extending your chatbot with custom tools (e.g., image generation, web search, transcription)
File input & search, including PDFs and static uploads
Vision support for working with images and visual data
Code interpreter access for dynamic, in-chat Python computation
Chat history management across sessions
Full UI customization, including model, temperature, avatars, placeholder text, and more
How It Works: Under the Hood
At the core of streamlit-openai
is a flexible architecture built around Section
and Block
classes, which work together to create a dynamic, modular chat interface. Each chat message, system tool, or UI element is structured as a Block
inside a Section
, making it easy to manage layout, behavior, and customization.
The diagram below gives a high-level overview of this structure:
This setup allows for fine-grained control over how chats are displayed and handled — including things like file uploads, function calls, and streaming outputs — while still staying easy to use with just a few lines of code.
Try It Out in Seconds
- Install the package:
pip install streamlit-openai streamlit openai
- Write your app:
import streamlit as st
import streamlit_openai
if "chat" not in st.session_state:
st.session_state.chat = streamlit_openai.Chat()
st.session_state.chat.run()
- Run the app:
streamlit run app.py
That’s it — you now have a fully functional AI chat interface with streaming responses!
Extend with Custom Functions
With a few lines of code, you can teach your assistant to generate images, search the web, or transcribe audio:
import streamlit as st
import openai
import streamlit_openai
if "chat" not in st.session_state:
def handler(audio_file):
client = openai.OpenAI()
response = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=open(audio_file, "rb"),
)
return response.text
transcribe_audio = streamlit_openai.CustomFunction(
name="transcribe_audio",
description="Convert speech to text.",
parameters={
"type": "object",
"properties": {
"audio_file": {
"type": "string",
"description": "The audio file to transcribe.",
}
},
"required": ["audio_file"]
},
handler=handler
)
st.session_state.chat = streamlit_openai.Chat(
functions=[transcribe_audio],
)
st.session_state.chat.run()
Pass your function to the Chat
component and let OpenAI’s function calling do the rest!
File Handling, Vision, and Search
From uploading files directly in the chat window to letting GPT-4o “see” images or search through PDF content — streamlit-openai
gives your app powerful, context-aware capabilities with minimal setup.
Need to search across documents? Use vector stores.
Want vision-based PDF parsing? It’s built-in.
Designed for Flexibility
You can customize:
- The model (
gpt-4o
,gpt-4
, etc.) - Temperature, instructions, and welcome messages
- Assistant and user avatars
- Example messages and input box text
- Storage control and chat history saving
All Backed by OpenAI’s Latest APIs
This package uses OpenAI’s Responses API under the hood — a more advanced and unified way to build AI assistants. And it’s already aligned with OpenAI’s roadmap (including upcoming Assistants API deprecation).
Learn More
Check out the full documentation and examples on GitHub:
GitHub - sbslee/streamlit-openai: Build AI chatbots with Streamlit and OpenAI
I’d love to hear what you think! Feedback, issues, feature requests — or examples of what you build — are all very welcome.
Happy building!
— Steven