gradio-app / gradio

Build and share delightful machine learning apps, all in Python. ๐ŸŒŸ Star to support our work!
http://www.gradio.app
Apache License 2.0
32.32k stars 2.41k forks source link

Chatbot Error while streaming. Unexpected client exception TypeError: Cannot read properties of undefined (reading '1') #7980

Closed usnavy13 closed 5 months ago

usnavy13 commented 5 months ago

Describe the bug

Chat will throw an error while streaming. I cant do anything specific to reproduce the error but while the response is streaming around 10% of the time the front end will throw an error that can only be cleared by refreshing.

index.js:947 Unexpected client exception TypeError: Cannot read properties of undefined (reading '1') at Me (index.js:192:15) at index.js:215:11 at Array.forEach () at Ke (index.js:214:8) at index.js:987:30 at Array.forEach () at je (index.js:986:24) at N (index.js:915:27) N @ index.js:947

Have you searched existing issues? ๐Ÿ”Ž

Reproduction

import gradio as gr

def convert_to_openai_format(history):
    history_openai_format = []
    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human })
        if assistant != None:
            history_openai_format.append({"role": "assistant", "content":assistant})
    return history_openai_format

def convo_prep(user_message, history):
        return "", history + [[user_message, None]]

def rty(history):
    history[-1][1] = None
    return history

def chat(history):
    messages = convert_to_openai_format(history)
    response = run_conversation(messages)
    for chunk in response:
        if chunk.choices and chunk.choices[0].delta.content is not None:
            if history[-1][1] == None:
                history[-1][1] = chunk.choices[0].delta.content
            else:
                history[-1][1] += chunk.choices[0].delta.content
            yield history

theme = gr.themes.Default(
    primary_hue="sky",
    secondary_hue="slate",
    neutral_hue="zinc",
    text_size="md",
    spacing_size="sm").set(
    body_background_fill="#d4ddef")

app_name = "Chat with me"

with gr.Blocks(theme=theme, title=app_name) as demo:
    with gr.Row():
        chatbot = gr.Chatbot(show_copy_button=True,
                            bubble_full_width=False,
                            height = 550,
                            layout = "bubble",
                            label = "Chat") 
    with gr.Row():
        with gr.Column(scale=6):
            msg = gr.Textbox(placeholder="Ask me a Question!", container=False, scale=6 ,max_lines= 5, label="Question", autofocus=True)

        with gr.Row():
            send = gr.Button(value="Submit", scale=1)
            clear = gr.ClearButton([msg, chatbot, ], scale=1)

        with gr.Row():
            retry = gr.Button(value="Retry", scale=1)
            stop = gr.Button(value="Stop", scale=1) 

    click1 = msg.submit(convo_prep, [msg, chatbot], [msg, chatbot]).then(chat, [chatbot], [chatbot])
    click2 = send.click(convo_prep, [msg, chatbot], [msg, chatbot]).then(chat, [chatbot], [chatbot])
    click3 = retry.click(rty, [chatbot], [chatbot]).then(chat, [chatbot], [chatbot])
    stop.click(fn=None, inputs=None, outputs=None, cancels=[click1, click2])

demo.queue(api_open=False,default_concurrency_limit=5)
if __name__ == "__main__":
    demo.launch(debug=True)

Screenshot

image

Logs

Where is the log file???? No errors were thrown on the terminal

System Info

Gradio Environment Information:
------------------------------
Operating System: Windows
gradio version: 4.25.0
gradio_client version: 0.15.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
altair: 5.3.0
fastapi: 0.110.1
ffmpy: 0.3.2
gradio-client==0.15.0 is not installed.
httpx: 0.27.0
huggingface-hub: 0.22.2
importlib-resources: 6.4.0
jinja2: 3.1.3
markupsafe: 2.1.5
matplotlib: 3.8.4
numpy: 1.26.4
orjson: 3.10.0
packaging: 24.0
pandas: 2.2.1
pillow: 10.3.0
pydantic: 2.6.4
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.3.5
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.1
typing-extensions: 4.11.0
uvicorn: 0.29.0
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.3.1
httpx: 0.27.0
huggingface-hub: 0.22.2
packaging: 24.0
typing-extensions: 4.11.0
websockets: 11.0.3

Severity

I can work around it

pngwn commented 5 months ago

Can you try upgrading to 4.26.0 to see if the issues persists? That release contains some improvements that may resolve this issue.

acylam commented 5 months ago

Can you try upgrading to 4.26.0 to see if the issues persists? That release contains some improvements that may resolve this issue.

Also encountered this issue with gradio 4.25. Seems to be resolved after upgrading to 4.26.0!