BUG: gradio RuntimeError: async generator raised StopAsyncIteration

BUG: gradio RuntimeError: async generator raised StopAsyncIteration

环境

python 复制代码
gradio                        4.20.0

详情

在使用gradio编写大模型可视化demo的时候,大模型正常输出,但gradio弹出此错误。

经过排除,发现是返回方式的问题,gradio传输信息给web网页的时候,我使用了return,但实际上应该 使用yield

解决方法

return改为yield

例子

python 复制代码
# 旧
def generate(message: str) -> Iterator[str]:
    stream = False
    response = "你好"
    if stream:
        for _ in range(5):
		        response += response
            yield response
    else:
        return response  # 错误

# 新
def generate(message: str) -> Iterator[str]:
    stream = False
    response = "你好"
    if stream:
        for _ in range(5):
		        response += response
            yield response
    else:
        yield response  # 正确

相关推荐
Code额1 小时前
Python 连接 DeepSeek API,OpenAI
开发语言·python·ai·ai编程
AAA@峥2 小时前
Python 基础开篇:认识 Python、版本选择、环境部署与首个 HelloWorld
开发语言·python
Metaphor6922 小时前
使用 Python 快速在 Word 文档中插入图片
python·word
2601_966871402 小时前
Python 爬虫 + 数据挖掘实战:数据抓取与深度挖掘分析全流程
爬虫·python·数据挖掘
青禾8373 小时前
Linux 系统信息、权限管理与 Python 并发编程(协程与线程)完全指南
linux·python
曲无忆3 小时前
密码学三大核心技术解析
python·密码学
qq21084629533 小时前
在python中什么是 self 和 cls?
开发语言·前端·python
nanawinona3 小时前
2026年手工思路量化后,工具重点会怎样变化
人工智能·python
今天AI了吗3 小时前
从聊天到委派:AI Agent 如何推进长期任务
数据库·人工智能·python·sql·rust