Skip to content

Commit

Permalink
Improve error handling in st.write_stream (streamlit#8036)
Browse files Browse the repository at this point in the history
* Improve error handling in write stream

* Add spaces

* Remove import

* Update error text
  • Loading branch information
LukasMasuch authored and zyxue committed Apr 16, 2024
1 parent dd8f9b1 commit 7817ed4
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/streamlit/elements/write.py
Expand Up @@ -14,7 +14,6 @@

from __future__ import annotations

import contextlib
import dataclasses
import inspect
import json as json
Expand Down Expand Up @@ -203,14 +202,30 @@ def flush_stream_response():
if type_util.is_type(
chunk, "openai.types.chat.chat_completion_chunk.ChatCompletionChunk"
):
# Try to convert openai chat completion chunk to a string:
with contextlib.suppress(Exception):
# Try to convert OpenAI chat completion chunk to a string:
try:
chunk = chunk.choices[0].delta.content or ""
except AttributeError as err:
raise StreamlitAPIException(
"Failed to parse the OpenAI ChatCompletionChunk. "
"The most likely cause is a change of the chunk object structure "
"due to a recent OpenAI update. You might be able to fix this "
"by downgrading the OpenAI library or upgrading Streamlit. Also, "
"please report this issue to: https://github.com/streamlit/streamlit/issues."
) from err

if type_util.is_type(chunk, "langchain_core.messages.ai.AIMessageChunk"):
# Try to convert langchain_core message chunk to a string:
with contextlib.suppress(Exception):
# Try to convert LangChain message chunk to a string:
try:
chunk = chunk.content or ""
except AttributeError as err:
raise StreamlitAPIException(
"Failed to parse the LangChain AIMessageChunk. "
"The most likely cause is a change of the chunk object structure "
"due to a recent LangChain update. You might be able to fix this "
"by downgrading the OpenAI library or upgrading Streamlit. Also, "
"please report this issue to: https://github.com/streamlit/streamlit/issues."
) from err

if isinstance(chunk, str):
first_text = False
Expand Down

0 comments on commit 7817ed4

Please sign in to comment.