openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Image.create() fails with request_timeout param

Open memorable opened this issue 2 years ago • 3 comments

Describe the bug

When I use request_timeout on a text completion, it works fine:

openai.Completion.create(prompt="1,2,3,4,", request_timeout=10.0)

But when I try to do it with an image creation, it fails: openai.Image.create(prompt="beautiful sunset", request_timeout=10.0) ==> openai.error.InvalidRequestError: Additional properties are not allowed ('request_timeout' was unexpected)

I'm not familiar enough with the code base to know how this ought to work, but I can get it to work with an ugly hack like (starting at line 36 of openai/api_resouces/image.py):

response, _, api_key = requestor.request(
           "post", cls._get_url("generations"), params
)

becomes:

request_timeout = params.pop("request_timeout", None) 
response, _, api_key = requestor.request(
            "post", cls._get_url("generations"), params, request_timeout=request_timeout
 )

To Reproduce

import openai
openai.api_key = ...
openai.Image.create(prompt="beautiful sunset", request_timeout=10.0)

Code snippets

No response

OS

macOS

Python version

Python 3.11.1

Library version

openai-python v0.26.5 (and others)

memorable avatar Feb 14 '23 23:02 memorable

You should add the timeout to the requests's modules get call because the Image.create returns a url string. The below snippet works for me.

response = openai.Image.create(
                prompt=prompt,
                size=size,
                response_format="url",
                **({"filter": filter} if filter else {}),
            )
            image_data = requests.get(
                response.get("data")[0]["url"], timeout=300
            ).content
            image = Image.open(BytesIO(image_data))
            image.show()

raiyanyahya avatar Feb 17 '23 23:02 raiyanyahya

Thanks for the suggestion, raiyanyahya.

I think that still leaves me with a problem, though... The create call itself might take a long time to return, and I'd like to have control over the max duration there. The README.md says that the create function itself supports the request_timeout param, and it seems like it ought to -- and very nearly does.

memorable avatar Feb 18 '23 01:02 memorable

I see that they have mentioned that in the readme but they have also attached a link to the requests module's get call. I agree that this could be an old readme or some confusion in expectations.

Currently i dont see any timeout arguments to the create function.

raiyanyahya avatar Feb 18 '23 16:02 raiyanyahya

In our v1 beta we've restructured how timeouts work so that a timeout argument can passed to the client constructor or configured per-request using client.with_options which will allow you to customise it for every endpoint.

from openai import OpenAI

# Configure the default for all requests:
client = OpenAI(
    # default is 60s
    timeout=20.0,
)

# More granular control:
client = OpenAI(
    timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
)

# Override per-request:
client.with_options(timeout=5 * 1000).chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "How can I list all files in a directory using Python?",
        }
    ],
)

https://github.com/openai/openai-python/tree/v1?tab=readme-ov-file#timeouts

RobertCraigie avatar Oct 18 '23 07:10 RobertCraigie