🚀 Introducing `streamlit-openai`: A Streamlit Component for Building Powerful OpenAI Chat Interfaces

Hey Streamlit Community! :waving_hand:

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.


:sparkles: Key Features

  • :repeat_button: Real-time streaming with OpenAI’s Responses API
  • :hammer_and_wrench: Function calling for extending your chatbot with custom tools (e.g., image generation, web search, transcription)
  • :card_file_box: File input & search, including PDFs and static uploads
  • :eyes: Vision support for working with images and visual data
  • :brain: Code interpreter access for dynamic, in-chat Python computation
  • :speech_balloon: Chat history management across sessions
  • :artist_palette: Full UI customization, including model, temperature, avatars, placeholder text, and more

:compass: 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.

:test_tube: Try It Out in Seconds

  1. Install the package:
pip install streamlit-openai streamlit openai
  1. 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()
  1. Run the app:
streamlit run app.py

:white_check_mark: That’s it — you now have a fully functional AI chat interface with streaming responses!


:puzzle_piece: 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_folder: 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.


:light_bulb: 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

:package: 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).


:books: Learn More

Check out the full documentation and examples on GitHub:
:backhand_index_pointing_right: 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. :rocket:

Happy building!

— Steven

5 Likes

Wow, this is fantastic work, Steven! :tada:

I love how streamlit-openai makes it so seamless to integrate advanced OpenAI features like function calling, file handling, and even vision into a Streamlit interface.

I’m the creator of qdiidata.streamlit.app, a Streamlit-based app for financial analysis, I have integrated your package to my project.

Thanks again for sharing this amazing tool.

— Kevin Gao

2 Likes

Hi Kevin,

Thanks for your reply. It’s incredible how quickly you integrated streamlit-openai into your project! I’m glad you found it helpful. I have many more features planned for the package, so stay tuned. And please don’t hesitate to reach out if you have any questions or feature requests.

Best,
Steven

Hi Steven,

Thanks again — the package is fantastic and already very helpful!

I’d love to share a few feature suggestions that might enhance the user experience even further:

  1. Model selector in the sidebar
    A dropdown to choose from available models (e.g., gpt-4o, gpt-o3, gpt-o4-mini). This would make testing across models super convenient.

  2. API key input in the sidebar
    A text input field (with password masking) for users to input their own OpenAI API key, especially useful in deployment or multi-user setups.

  3. Copy output button
    A one-click button to copy the assistant’s full response, maybe appearing next to or under each message.

  4. “New conversation” button
    A clear button to reset the chat history and start a fresh conversation, similar to what ChatGPT offers.

  5. Chat history viewer in the sidebar
    Display past conversations (even just the titles or first message) as clickable entries for users to revisit or resume previous chats.

These small additions could make the app even more intuitive and user-friendly. Happy to help explore implementation ideas if useful!

Best,
Kevin

1 Like

I’m excited to share that a new version of streamlit-openai (v0.1.2) is now available! The version referenced in the original post was v0.1.0, and since then, many new features have been added, as shown below. You can read the package’s full documentation here. Please feel free to reach out if you have any questions or comments.

0.1.2 (2025-06-19)

  • Add support for OpenAI’s web search capability.
  • Add support for OpenAI’s remote MCP capability.
  • Fix a bug causing incorrect handling of the history file.
  • Add support for OpenAI’s reasoning capability.
  • Add support for OpenAI’s image generation capability.
  • Code blocks are now rendered as collapsible.

0.1.1 (2025-06-15)

  • Fix a typo in Chat.save.
  • The welcome message is no longer shown when a user provides chat history.
  • Fix a bug causing incorrect handling of example messages.
  • Attachment file names are now included in user messages.