Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#1,927 コメント 3 件 リアクション 0 件 担当者 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時間
マージ済み PR(30日)
137

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openai/openai-python のほかの issue

openai/openai-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。