Skip to content

openai==1.2.3: segmentation fault with async #791

@earonesty

Description

@earonesty

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

seg fault when using async and running openai integration tests with the latest version on windows

running this

python -q -X faulthandler test.py

gets this:

ChatCompletion(id='chatcmpl-8JvRae0P4EOZRkIJuGQQMFBQ6lRpf', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='Hello! How can I assist you today?', role='a
ssistant', function_call=None, tool_calls=None))], created=1699759738, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=
9, prompt_tokens=18, total_tokens=27))
Windows fatal exception: access violation

Current thread 0x000032c8 (most recent call first):
  Garbage-collecting
  File "C:\Users\erik\.pyenv\pyenv-win\versions\3.11.6\Lib\asyncio\proactor_events.py", line 82 in __repr__
  File "C:\Users\erik\.pyenv\pyenv-win\versions\3.11.6\Lib\asyncio\proactor_events.py", line 116 in __del__
Segmentation fault

i updated to 3.11.6 jut to see if that helped... nope

i don't normally have issues with async code, and i have unit tests and code passing fine. only the openai lib has this issue.

fortunately it only fails at exit, so i can keep on keeping on for now without worrying too much

To Reproduce

using windows pyenv python 3.11.5 or 3.11.6, use the async openai client to make a single chat completion in a pytest test.

it works, but then your program segfaults

all other httpx async/rest/tests all work, even respx live tests, etc.

only when i do the integration test with openai does it fail

Code snippets

example of failure


from openai import AsyncOpenAI
import asyncio

async def x():
    model="gpt-3.5-turbo"
    client = AsyncOpenAI()
    messages = [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello"
        },
    ]

    response = await client.chat.completions.create(
        model=model,
        messages=messages,
        max_tokens=500
    )

    return response

print(asyncio.run(x()))


### OS

windows

### Python version

3.11.5

### Library version

openai==1.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions