`openai.AsyncOpenAI` not safe when shared across `async` tests
Confirm this is an issue with the Python library and not an underlying OpenAI API
- [X] This is an issue with the Python library
Describe the bug
When sharing AsyncOpenAI across async tests, it's possible to hit openai.APIConnectionError: Connection error.
To Reproduce
https://colab.research.google.com/drive/1icWDkN2iYPl3mxCEwA0-kE7GSSOeM9E8?usp=sharing
Code snippets
import pytest
from openai import AsyncOpenAI
client = AsyncOpenAI()
@pytest.mark.asyncio
async def test_completion1() -> None:
await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{
"content": (
"Here is a question, the correct answer to the question, and a proposed"
" answer to the question. Please tell me if the proposed answer is"
" correct, given the correct answer. ONLY SAY 'YES' OR 'NO'. No other"
" output is permitted.\n\nQuestion: What is 25 * 10? \n\nCorrect"
" answer: 250 \n\nProposed answer: 250"
),
"role": "user",
}],
)
@pytest.mark.asyncio
async def test_completion2() -> None:
await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{
"content": (
"Here is a question, the correct answer to the question, and a proposed"
" answer to the question. Please tell me if the proposed answer is"
" correct, given the correct answer. ONLY SAY 'YES' OR 'NO'. No other"
" output is permitted.\n\nQuestion: What is 25 * 10? \n\nCorrect"
" answer: 250 \n\nProposed answer: 250"
),
"role": "user",
}],
)
OS
linux
Python version
3.10.12
Library version
1.51.0
Thanks for the report, what version of httpx / httpcore are you using? (I can't open the colab link right now)
Hello @RobertCraigie thanks for the quick response, it's httpx=0.27.2 and httpcore==1.0.6
I see the same issue. httpx==0.27.2 httpcore==1.0.6 In my case I use pytest with async and ainvoke on ChatOpenAI BUT, if I use ChatBedrock, everything is working.
Since I was breaking my head against this, one hint is that I've tried to trace it and I printed the "real" error which is not a connection error, but "Event loop is closed"
This is the line it fails, if you print the err object, you would see it.
https://github.com/openai/openai-python/blob/main/src/openai/_base_client.py#L1584
raise APIConnectionError(request=request,message=str(err)) from err
Hope that will give us some more insights..
update your OpenAI to the latest.
Thanks for reaching out. An update has been sent to fix the issue. We will let you know once the issue is resolved.
God bless you. Grace be unto you and Peace be multiplied.
Anointing J. Mayami +2348140070908 @.***
On Tue, Oct 8, 2024 at 12:53 PM Rotem @.***> wrote:
I see the same issue. httpx==0.27.2 httpcore==1.0.6 In my case I use pytest with async and ainvoke on ChatOpenAI BUT, if I use ChatBedrock, everything is working.
Since I was breaking my head against this, one hint is that I've tried to trace it and I printed the "real" error which is not a connection error, but "Event loop is closed"
This is the line it fails, if you print the err object, you would see it.
https://github.com/openai/openai-python/blob/main/src/openai/_base_client.py#L1584 raise APIConnectionError(request=request,message=str(err)) from err
Hope that will give us some more insights..
— Reply to this email directly, view it on GitHub https://github.com/openai/openai-python/issues/1777#issuecomment-2399633471, or unsubscribe https://github.com/notifications/unsubscribe-auth/BK7UCY3CEGU5JCVXCI5YMLLZ2PBSRAVCNFSM6AAAAABPN3LTC2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJZGYZTGNBXGE . You are receiving this because you are subscribed to this thread.Message ID: @.***>
It happens because by default pytest-asyncio creates a new event loop for each test. But your tests are using the same client. And when you try to use the same client from different event loops, this error occurs.
It is not an issue with openai library. This can happen when testing other asyncio libraries too, if you reuse the same client.
Change your test decorators to @pytest.mark.asyncio(loop_scope="session") – it should resolve the issue.
Thanks @bhumkong, I'm going to close this as it doesn't appear to be an SDK bug. Please re-open if you're still running into issues!
Thank you @bhumkong, nice work and appreciate your breakdown
@bhumkong Thank you! I can confirm this worked.