How to set a timeout on an API function call using using the Python library

Hi, I like using the Python library but I need to set a timeout on responses. Has anyone found a way that’s easier than this:

I know I could call the REST API directly using requests and set the timeout there, but I would rather keep using the Python library if possible, so I don’t have to change too much code.

1 Like

What issue is requiring you to change the timeout?

@daveshapautomator I’m firing off 100 requests in parallel, 99 of them come back within a few seconds, 1 takes over 30 seconds. I want my app to gracefully kill long-running requests so i can return the 99 successful responses without waiting for the long-running thread.

1 Like

Ah makes sense. I usually just set a pause and retry.

doh just realized that there is a timeout parameter in the Python binding

Signature: openai.Completion.create(*args, timeout=None, **kwargs)
Create a new instance of this model.

timeout (float): the number of seconds to wait on the promise returned by the API, where 0 means wait forever.
File:      /opt/anaconda3/lib/python3.7/site-packages/openai/api_resources/
Type:      method
1 Like