openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

`openai.AsyncOpenAI` not safe when shared across `async` tests

Open jamesbraza opened this issue 2 years ago • 9 comments

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

jamesbraza avatar Oct 06 '24 01:10 jamesbraza

Thanks for the report, what version of httpx / httpcore are you using? (I can't open the colab link right now)

RobertCraigie avatar Oct 06 '24 02:10 RobertCraigie

Hello @RobertCraigie thanks for the quick response, it's httpx=0.27.2 and httpcore==1.0.6

jamesbraza avatar Oct 06 '24 02:10 jamesbraza

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..

rotem925 avatar Oct 08 '24 11:10 rotem925

update your OpenAI to the latest.

anointingmayami avatar Oct 08 '24 12:10 anointingmayami

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: @.***>

anointingmayami avatar Oct 08 '24 13:10 anointingmayami

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.

anton-m-py avatar Oct 09 '24 09:10 anton-m-py

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!

RobertCraigie avatar Oct 09 '24 12:10 RobertCraigie

Thank you @bhumkong, nice work and appreciate your breakdown

jamesbraza avatar Oct 09 '24 16:10 jamesbraza

@bhumkong Thank you! I can confirm this worked.

rotem925 avatar Oct 10 '24 04:10 rotem925