# OpenAI Assistants API Streaming

**URL:** <https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690>\
**Category:** Using Streamlit\
**Tags:** debugging\
**Created:** [March 13, 2024, 11:08pm UTC](https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690 "2024-03-13T23:08:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanK](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@RyanK](https://discuss.streamlit.io/u/RyanK)\
**Post date:** [March 13, 2024, 11:08pm UTC](https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690/1 "2024-03-13T23:08:22Z")

</div>

OpenAI just pushed an update to allow for streaming from the Assistants API. When I do an st.write with streamlit, it starts writing the message, but after a character or two, it stops. The streaming is fully functional in my terminal. Here is my code:

```auto
class EventHandler(AssistantEventHandler):    
  @override
  def on_text_created(self, text) -> None:
    # print(f"assistant > ", end="", flush=True)
    st.write(text.value)
      
  @override
  def on_text_delta(self, delta, snapshot):
    print(delta.value, end="", flush=True)
    # st.write(delta.value)
      
  def on_tool_call_created(self, tool_call):
    print(f"assistant > {tool_call.type}", flush=True)
    # st.write(f"assistant > {tool_call.type}")
  
  def on_tool_call_delta(self, delta, snapshot):
    if delta.type == 'code_interpreter':
      if delta.code_interpreter.input:
        print(delta.code_interpreter.input, end="", flush=True)
        # st.write(delta.code_interpreter.input)
      if delta.code_interpreter.outputs:
        print(f"output >", flush=True)
        # st.write(f"output >")
        for output in delta.code_interpreter.outputs:
          if output.type == "logs":
            print(f"{output.logs}", flush=True)
            # st.write(f"{output.logs}")
  
assistant = client.beta.assistants.retrieve('x')

st.title("💬 Chatbot")
st.caption("🚀 A streamlit chatbot powered by x")

prompt = st.text_input("Enter your message")

if prompt:
    thread = client.beta.threads.create()
    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content=prompt
    )
    with client.beta.threads.runs.create_and_stream(
        thread_id=thread.id,
        assistant_id=assistant.id,
        event_handler=EventHandler(),
    ) as stream:
        st.write(stream.until_done())

```

1. App is local
2. The message starts to stream and then it stops

---

<div class="post-metadata">

**Author:** ![ferdy](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ferdy/32/29957_2.png) [@ferdy](https://discuss.streamlit.io/u/ferdy)\
**Post date:** [March 14, 2024, 2:47am UTC](https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690/2 "2024-03-14T02:47:50Z")

</div>

To format the code, select it and press the button pointed by the green arrow.

![image](https://global.discourse-cdn.com/streamlit/original/3X/4/9/499c307d85e305d3f0bfbdb1e8cbe816e75577f4.png)

---

<div class="post-metadata">

**Author:** ![Imtiaz\_Hasan](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/imtiaz_hasan/32/40182_2.png) [@Imtiaz\_Hasan](https://discuss.streamlit.io/u/Imtiaz_Hasan)\
**Post date:** [March 14, 2024, 10:03pm UTC](https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690/3 "2024-03-14T22:03:07Z")

</div>

bro. check this out. I updated your code to mimic the streaming function

[![](https://global.discourse-cdn.com/streamlit/original/3X/d/f/dfc852d12a801944fcdcb1fc1c014d83e280a13a.jpeg "Unleash Open AI Assistant's New Streaming Feature - Step By Step") ](https://www.youtube.com/watch?v=nfCO0Oh2kLk)

---

<div class="post-metadata">

**Author:** ![RyanK](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@RyanK](https://discuss.streamlit.io/u/RyanK)\
**Post date:** [March 14, 2024, 10:18pm UTC](https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690/4 "2024-03-14T22:18:51Z")

</div>

Great video! You explained the process well, thanks for making a video based on this!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [March 16, 2024, 10:18pm UTC](https://discuss.streamlit.io/t/openai-assistants-api-streaming/64690/5 "2024-03-16T22:18:54Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
