replicate/replicate-python

httpx.WriteTimeout: The write operation timed out

ahmadmustafaanis opened this issue · 4 comments

    image1 = open("/Users/ahmadanis/Downloads/0.png", "rb")
    image2 = open("/Users/ahmadanis/Downloads/1.png", "rb")

    output = replicate.run(
        "cjwbw/fastcomposer:0521e26704d37373ee0856a39541984aec673e4e85a79c1ea78d1c030004dc44",
        input={
            "alpha": 0.7,
            "width": 512,
            "height": 512,
            "image1": image1,
            "image2": image2,
            "prompt": "A man<A*> and a man<A*> singing in the park together.",
            "num_steps": 50,
            "guidance_scale": 5,
            "num_images_per_prompt": 1
        },
    )
    print(output)

Error:

  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py", line 67, in map_httpcore_exceptions
    yield
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py", line 231, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 216, in handle_request
    raise exc from None
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 196, in handle_request
    response = connection.handle_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 101, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 132, in handle_request
    raise exc
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 93, in handle_request
    self._send_request_body(**kwargs)
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 155, in _send_request_body
    self._send_event(event, timeout=timeout)
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 164, in _send_event
    self._network_stream.write(bytes_to_send, timeout=timeout)
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py", line 133, in write
    with map_exceptions(exc_map):
  File "/Users/ahmadanis/anaconda3/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.WriteTimeout: The write operation timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/ahmadanis/Desktop/Ahmad/PersonalProjects/img_function.py", line 73, in <module>
    s = generate_img(image0, image1)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/Desktop/Ahmad/PersonalProjects/img_function.py", line 20, in generate_img
    output = replicate.run(
             ^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/replicate/client.py", line 148, in run
    return run(self, ref, input, **params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/replicate/run.py", line 40, in run
    prediction = client.predictions.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/replicate/prediction.py", line 364, in create
    resp = self._client._request(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/replicate/client.py", line 85, in _request
    resp = self._client.request(method, path, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_client.py", line 828, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_client.py", line 915, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_client.py", line 943, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_client.py", line 980, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_client.py", line 1016, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/replicate/client.py", line 258, in handle_request
    response = self._wrapped_transport.handle_request(request)  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py", line 230, in handle_request
    with map_httpcore_exceptions():
  File "/Users/ahmadanis/anaconda3/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/ahmadanis/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py", line 84, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.WriteTimeout: The write operation timed out

What I tried: Added the following to my own script

import httpx

client = httpx.Client(timeout=None)

But still getting the same error.

Hi @ahmadmustafaanis. File inputs are automatically transformed to base64-encoded data URIs. Because the files are encoded in the request object itself, large files may cause timeouts. To mitigate this, try uploading the files somewhere and pass a publicly accessible URL as an input instead.

This doesn't seem like a very intuitive solution.

How about adding the extra timeout parameter in **kwargs and setting httpx timeout to none.

I can create a pr for it.

@ahmadmustafaanis Large requests are rejected by the API server, so increasing the timeout wouldn't necessarily help. We're working on a solution that automatically uploads files, so please look out for that. To unblock yourself in the meantime, I'd recommend uploading to an S3 bucket or a similar file storage service.

@mattt I have requests that are not rejected by the server (I can see in the dashboard they complete successfully). However I receive WriteTimeout errors from the underlying httpx client. I can see that the replicate client has a timeout property but does not pass it to the httpx client. Would this not be an easy solution?