openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
23.24k stars 3.27k forks source link

Shared data content when used with FastAPI backgroundtasks and client.beta.threads.runs.create_and_poll #1345

Open Robban1980 opened 7 months ago

Robban1980 commented 7 months ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

I have a FastAPI API that I have one endpoint in /execute. This endpoint creates a FastAPI backgroundtask to do a call to the assistants API. In here I make a request to client.beta.threads.runs.create_and_poll, there are no issues starting the request and getting the expected outcome. However, if another request comes in to the API, when the first request background task is still waiting for the create_and_poll, both requests will complete at the same time and the result out from create_and_poll will be based on the first request it received. I am creating the OpenAI client in the backgrountask method to not use a global one.

I am a beginner in python and still trying to learn and understand how things are isolated, could this be a usage error or is there no thread isolation for the client.beta.threads.runs.create_and_poll method?

To Reproduce

-

Code snippets

No response

OS

Ubuntu

Python version

Python 3.10

Library version

openai 1.20

rattrayalex commented 7 months ago

My instinct is that this is a bug in your code with how the background worker is being used. I don't think it's likely this library would have a concurrency bug of the nature you describe.

On Thu, Apr 18 2024 at 10:58 PM, Robert Karlsson @.***> wrote:

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

I have a FastAPI API that I have one endpoint in /execute. This endpoint creates a FastAPI backgroundtask to do a call to the assistants API. In here I make a request to client.beta.threads.runs.create_and_poll, there are no issues starting the request and getting the expected outcome. However, if another request comes in to the API, when the first request background task is still waiting for the create_and_poll, both requests will complete at the same time and the result out from create_and_poll will be based on the first request it received. I am creating the OpenAI client in the backgrountask method to not use a global one.

I am a beginner in python and still trying to learn and understand how things are isolated, could this be a usage error or is there no thread isolation for the client.beta.threads.runs.create_and_poll method? To Reproduce

-

Code snippets

No response OS

Ubuntu Python version

Python 3.10 Library version

openai 1.20

— Reply to this email directly, view it on GitHub https://github.com/openai/openai-python/issues/1345, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFL6LURAUFSAKVK6UJW7C3Y6CB3XAVCNFSM6AAAAABGOMGTCGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI2TCOJXGU4DKNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>