Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in st.write_stream #8036

Merged
merged 4 commits into from Jan 29, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 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,28 @@ 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. "
"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. "
"You might be able to fix this by downgrading the LangChain 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