Image.create() fails with request_timeout param
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)
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()
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.
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.
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