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

**URL:** <https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213>\
**Category:** Prompting\
**Created:** [June 9, 2021, 7:42pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213 "2021-06-09T19:42:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sridhar1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sridhar1/32/443_2.png) [@sridhar1](https://community.openai.com/u/sridhar1)\
**Post date:** [June 9, 2021, 7:42pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/1 "2021-06-09T19:42:10Z")

</div>

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:

> <https://stackoverflow.com/questions/492519/timeout-on-a-function-call>

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.

---

<div class="post-metadata">

**Author:** ![daveshapautomator](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/daveshapautomator/32/8779_2.png) [@daveshapautomator](https://community.openai.com/u/daveshapautomator)\
**Post date:** [June 9, 2021, 8:11pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/2 "2021-06-09T20:11:04Z")

</div>

What issue is requiring you to change the timeout?

---

<div class="post-metadata">

**Author:** ![sridhar1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sridhar1/32/443_2.png) [@sridhar1](https://community.openai.com/u/sridhar1)\
**Post date:** [June 9, 2021, 8:25pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/3 "2021-06-09T20:25:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveshapautomator](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/daveshapautomator/32/8779_2.png) [@daveshapautomator](https://community.openai.com/u/daveshapautomator)\
**Post date:** [June 9, 2021, 8:37pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/4 "2021-06-09T20:37:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sridhar1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sridhar1/32/443_2.png) [@sridhar1](https://community.openai.com/u/sridhar1)\
**Post date:** [June 10, 2021, 8:01pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/5 "2021-06-10T20:01:38Z")

</div>

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

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

Parameters:
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/completion.py
Type: method

```

---

<div class="post-metadata">

**Author:** ![kumarM](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/kumarm/32/49867_2.png) [@kumarM](https://community.openai.com/u/kumarM)\
**Post date:** [March 20, 2023, 2:12pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/6 "2023-03-20T14:12:39Z")

</div>

Tried using the timeout parameter. It doesn’t seem to have any effect. No Error received after timeout interval is passed.

---

<div class="post-metadata">

**Author:** ![phuib](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/phuib/32/57959_2.png) [@phuib](https://community.openai.com/u/phuib)\
**Post date:** [March 28, 2023, 8:53pm UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/7 "2023-03-28T20:53:50Z")

</div>

Setting `request_timeout` worked for me.  
e.g.

```python
openai.Completion.create(request_timeout=1)

```

I was able to get this exception message.

> Request timed out: HTTPSConnectionPool(host=‘[api.openai.com](http://api.openai.com)’, port=443): Read timed out. (read timeout=1)

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [December 16, 2023, 10:48am UTC](https://community.openai.com/t/how-to-set-a-timeout-on-an-api-function-call-using-using-the-python-library/4213/8 "2023-12-16T10:48:50Z")

</div>

Closed on December 16, 2023, 10:48am UTC


