[Python] Stability AI API + Cloudflare Workers Python Code Results in Attempted to access...

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use CloudFlare's Workers but I have not deployed or published this code. The error appears to occur before the request is fully sent, suggesting it's related to how I'm constructing the request.

    Even though it's written in Python, I use npx wrangler@latest dev to run the worker code locally.

    On a separate Python project, I was able to make successful calls using request, but for this API project I am bound to use CF Workers with async support.

    After several attempts, I continue to get: [image_generator] Exception: Attempted to access streaming request content, without having called read()

    Here is the function currently being used:

    import httpx
    import certifi
    from js import console


    async def simple_image_generator(api_key, params, timeout=1000):
    if not api_key:
    raise Exception("API key is required")

    url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
    headers = {
    "Accept": "application/json, image/*",
    "Authorization": f"Bearer {api_key}",
    }

    # Force multipart/form-data
    files = []
    for key, value in params.items():
    files.append((key, (None, str(value))))
    files.append(("none", ("empty.txt", b"", "application/octet-stream")))

    async with httpx.AsyncClient(verify=certifi.where()) as client:
    try:
    console.info(f"\n[image_simple] Trying to POST {url}\n")

    response = await client.post(
    url,
    headers=headers,
    files=files,
    timeout=timeout,
    )

    await response.aread()
    response.raise_for_status()

    console.info("\n[image_simple] API Request sent successfully\n")
    return response

    except Exception as e:
    console.error(f"\n[image_simple] Exception: {str(e)}\n")
    raise Exception(f"API request failed: {str(e)}") from e


    Example params:

    params = {
    "prompt": "a dragon flying over a futuristic city",
    "mode": "text-to-image",
    "model": "sd3",
    "output_format": "png",
    "aspect_ratio": "1:1"
    }


    Some things already tried:

    • Calling await response.aread() before raise_for_status() with and without await
    • Adding a dummy files={"none": ("none", b"", "application/octet-stream")}
    • Moving all fields into files= instead of data= (Sending all parameters via the files parameter as tuples)
    • Using a response event hook to buffer the response (response buffering hooks)
    • Removing stream=True (if present—wasn’t)

    What’s the proper way to:


    1. Send a multipart/form-data POST request to Stability’s /sd3 endpoint without uploading an actual file?


    2. Prevent httpx from throwing the Attempted to access streaming request content, without having called 'read()' exception when reading the response?

    This is running in an async Python environment (Pyodide), if that matters.

    Any working minimal example or workaround would be appreciated!

    Continue reading...

Compartilhe esta Página