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  # 正确

相关推荐
大鱼>4 分钟前
Scikit-learn Pipeline:构建可复用的 ML 流水线
python·机器学习·scikit-learn
闲猫7 分钟前
深入理解 Skills:从 API 调用到智能体行为封装
人工智能·python·langchain
还是鼠鼠14 分钟前
AI掘金头条新闻系统 (Toutiao News)-缓存相关推荐新闻
后端·python·mysql·fastapi·web
骑士雄师17 分钟前
大模型:runnable
python·embedding
2401_8685347824 分钟前
系统分析师案例分析题常考知识点
python·计算机网络
江华森1 小时前
Python 实现 2048 游戏(三):面向对象重构与AI模拟
python
可以飞的话2 小时前
五、数据处理1
python
鱼毓屿御2 小时前
Python 装饰器与函数调用机制(复习笔记 · 2026-07-07)
开发语言·python
AC赳赳老秦2 小时前
采购专员自动化:OpenClaw 自动比价、生成询价单、跟踪供应商报价进度实战指南
开发语言·数据库·人工智能·python·自动化·github·openclaw
南墙上的石头2 小时前
麒麟 V10 SP1 踩坑:`./startup.sh` 报“解释器错误: 权限不够“,根因是 KySec
前端·chrome·python