# Configuring timeout for ChatCompletion Python

**URL:** <https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226>\
**Category:** API\
**Created:** [March 20, 2023, 12:38am UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226 "2023-03-20T00:38:08Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![itai.marks](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/itai.marks/32/51056_2.png) [@itai.marks](https://community.openai.com/u/itai.marks)\
**Post date:** [March 24, 2023, 1:43pm UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/22 "2023-03-24T13:43:19Z")

</div>

You can use this client implemented with asyncio and httpx.  
It supports fine grained connect/read timeout setting and connection reuse.

```auto
from httpx import Timeout
from openai_async_client import AsyncCreate, Message, ChatCompletionRequest, SystemMessage, OpenAIParams

create = AsyncCreate(api_key=os.environ["OPENAI_API_KEY"])
messages = [
    Message(
        role="user",
        content=f"ChatGPT, Give a brief overview of the Pride and Prejudice by Jane Austen.",
    )
]
response = create.completion(ChatCompletionRequest(prompt=messages),client_timeout=Timeout(1.0,read=10.0),retries=3)

create = AsyncCreate()
response = create.completion(TextCompletionRequest(prompt=f"DaVinci, Give a brief overview of Moby Dick by Herman Melville."))

```

> **[openai-async-client](https://pypi.org/project/openai-async-client/)**
>
> OpenAI async API with client side timeout, retry with exponential backoff and connection reuse

> **[GitHub - itaim/openai-async-client: OpenAI async httpx client](https://github.com/itaim/openai-async-client)**
>
> OpenAI async httpx client. Contribute to itaim/openai-async-client development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![anon10827405](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon10827405](https://community.openai.com/u/anon10827405)\
**Post date:** [March 25, 2023, 3:49am UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/23 "2023-03-25T03:49:52Z")

</div>

I completely recommend granular timeouts, but in a general use-case it makes absolutely no sense. Especially in the context of an OpenAI request.

I’d say it’s like recommending sport car parts to someone who just wants to fix their Honda Civic (fantastic car btw).

---

<div class="post-metadata">

**Author:** ![lmm333](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@lmm333](https://community.openai.com/u/lmm333)\
**Post date:** [April 5, 2023, 6:02pm UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/24 "2023-04-05T18:02:09Z")

</div>

The easiest way is to add parameter request\_timeout, it will be pass to requests.post(timeout=xxx)

eg:

openai.ChatCompletion.create(  
model=“gpt-3.5-turbo”,  
messages=[  
{  
“role”: “user”,  
“content”: prompt,  
}  
],  
request\_timeout=60,  
)

---

<div class="post-metadata">

**Author:** ![anon10827405](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon10827405](https://community.openai.com/u/anon10827405)\
**Post date:** [April 5, 2023, 6:07pm UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/25 "2023-04-05T18:07:18Z")

</div>

This is a great way.

I’d just like to add that a retry / backoff library is also a great option. In the event that a timeout, or some sort of intermittent error occurs, it will automatically retry using dynamic intervals.

---

<div class="post-metadata">

**Author:** ![jmtexas19](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jmtexas19/32/66378_2.png) [@jmtexas19](https://community.openai.com/u/jmtexas19)\
**Post date:** [April 6, 2023, 4:02am UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/26 "2023-04-06T04:02:49Z")

</div>

This worked for me along with using Ronald’s suggestion to use the retry with it. Thanks!

---

<div class="post-metadata">

**Author:** ![hongda.bu](https://avatars.discourse-cdn.com/v4/letter/h/dfb087/32.png) [@hongda.bu](https://community.openai.com/u/hongda.bu)\
**Post date:** [July 27, 2023, 10:54am UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/27 "2023-07-27T10:54:21Z")

</div>

Yes, request\_timeout is very important. Most people tell how to write retry decorator, but it can not solve the problem of SLOW. Set this parameter will help a lot.  
Meanwhile, use other Parallel method will help when you do not reach you RPM of your account, Like pool.apply\_async().  
In conclusion, retry decorator+request\_timeout parameter+parallel method will accelerate your chatgpt application.

---

<div class="post-metadata">

**Author:** ![brunouy](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/brunouy/32/193680_2.png) [@brunouy](https://community.openai.com/u/brunouy)\
**Post date:** [September 6, 2023, 1:30pm UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/28 "2023-09-06T13:30:50Z")

</div>

This worked perfect for me. Easy and straightforward.

---

<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 12, 2023, 7:00pm UTC](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226/29 "2023-12-12T19:00:03Z")

</div>

Closed on December 12, 2023, 7:00pm UTC



[Previous page](https://community.openai.com/t/configuring-timeout-for-chatcompletion-python/107226.md?page=1)
