APIConnectionError raised when using AsyncOpenAI along FastAPI and uvicorn(uvloop)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.6k
- Forks
- 5.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 96
Description
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
-
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" -
Run app main.py.
-
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the pyproject.toml dependency combinations and the main.py example, then reproduce the /test request under Python 3.11 with uvloop enabled and disabled. Compare the resulting traceback and isolate whether the failure is in the library or the event-loop integration; done means identifying a reproducible cause and a verified resolution for the listed versions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100