Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

APIConnectionError raised when using AsyncOpenAI along FastAPI and uvicorn(uvloop)

未关闭
#1,927 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@LuminaX-alt 已经在做这个了。

开始于 2025年8月4日。

  • #41 来自 @LuminaX-alt —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
fastapi, python
领域
api, backend

调研方向

先从 pyproject.toml 中的依赖组合和 main.py 示例开始,然后在启用和禁用 uvloop 的情况下,使用 Python 3.11 重现 /test 请求。比较生成的 traceback,并确定失败发生在库中还是事件循环集成中;对于列出的版本,识别出可复现的原因并验证解决方案后,即视为完成。

由索引模型根据 Issue 内容生成。

描述

bug sdk
Confirm this is an issue with the Python library and not an underlying OpenAI API
  • This is an issue with the Python library
Describe the bug

Exception raised when using AsyncOpenAI with FastAPI and uvicorn issue seems to be compability issues with uvloop that is added through the extras = ["standard"].

fastapi = "0.115.6"
uvicorn = {extras = ["standard"], version = "0.32.1"}
openai = "^1.54.4"

Installing dependencies manually and leaving out uvloop works.

fastapi = "0.115.6"
uvicorn = "0.32.1"
# from extras = ["standard"]
websockets="14.1"
watchfiles="1.0.0"
pyyaml="6.0.2"
python-dotenv="1.0.1"
httptools="0.6.4"
# uvloop="0.21.0" leads to openAI async exception
To Reproduce
  1. pyproject.toml with poetry dep.
    [tool.poetry.dependencies]
    python = ">=3.11,<3.12"
    fastapi = "0.115.6"
    uvicorn = {extras = ["standard"], version = "0.32.1"}
    openai = "^1.54.4"

  2. Run app main.py.

  3. Call endpoint
    http://localhost:50051/test

Exception:
File "/Users/badrelfarri/Documents/Code/RevVue/simple-async-openai-assistant/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 1610, in _request
raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

Code snippets
main.py


from fastapi import FastAPI
import uvicorn
from openai import AsyncOpenAI

apiKey = ""
app = FastAPI()

@app.get("/test")
async def test():
    systemMessage = "Get a title for the conversation, the title shall not have more than 4 words"
    messages = [
        {"role": "system", "content": systemMessage},
        {
            "role": "user",
            "content": "Is the earth flat",
        },
    ]
    result = await AsyncOpenAI(api_key=apiKey, timeout=30).chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
    )
    title = result.choices[0].message.content
    return title

if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=50051)
OS

macOS

Python version

Python 3.11.4

Library version

openai v1.54.4

主要语言
Python
星标
31.8k
派生
8k
平均合并
1 天 1 小时
30 天内合并 PR
137

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openai/openai-python 的其他 Issue

查看 openai/openai-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。