OpenAI images.generate() does not respond with Dall-e-3 / Python SDK

139 views Asked by At

I am trying to create some Dall-e-3 pictures using OpenAI Image Generation API from Firebase Functions (Python). Normally I get the pictures generated within 15 seconds. However, sometimes (5% time) they don't arrive after my own timeout expires (60 seconds), "Start" is printed out but not "End", and I don't even get an exception, only my own "Error: image generation timeout" which happens after 50 seconds. I didn't have the same problem with dall-e-2.

Note: I tried using AsyncOpenAI client instead of OpenAI's while keeping the rest of the code. It results in having exactly the same issue 50% of the time.

def generate_image(uid, client):
        
    prompt = get_image_prompt()

    try:
        print("Start")
        image_resp = client.images.generate(prompt=prompt, n=1, size="1024x1024", model="dall-e-3", response_format="b64_json", user=uid)
        print ("End")
        if image_resp.data is not None and len(image_resp.data) > 0 and image_resp.data[0].b64_json is not None:
            return image_resp.data[0].b64_json

        else:
            print("Unexpected response format")
            return None

    except OpenAIError as e:
        print(f"Error generating image: {e.error}")
        print(f"Prompt: {prompt}")
        return None


def generate_impl(req, uid, headers):

    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
    
    def stream():
        
        image_queue = Queue()
        def get_image():
            image = generate_image(uid, client)
            image_queue.put(image)

        try:
            completion = client.chat.completions.create(
                model=LLM_MODEL_ID,
                messages=[{'role': 'user', 'content': prompt}],
                stream=True,
                timeout=60)
        except Exception as e:
            print(f"Error: {e}")
            return ('Internal error', 500)

        # Some text streaming initialization happening here

        try:
            for line in completion:
                # Other stuff happening
                threading.Thread(target=get_image).start()
        except Exception as e:
            print(f"An error occurred during the streaming: {e}")
            return ('Stream disconnected unexpectedly', 500)

        image = wait_and_return_image(image_queue)
        image_queue.task_done    

    return (flask.Response(stream(), mimetype='text/event-stream'), 200, headers)


def wait_and_return_image(image_queue):

    retries = 0

    while retries < 1200: # 60 seconds
        if not image_queue.empty():
            image = image_queue.get()
            return image
        
        retries += 1
        time.sleep(0.5)

    print("Error: image generation timeout")
    return None
0

There are 0 answers