Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BUG] AI binding fails to handle binary responses from image generation models #783

@guitaripod

Description

@guitaripod

Is there an existing issue for this?

  • I have searched the existing issues

What version of workers-rs are you using?

0.6.0

What version of wrangler are you using?

4.25.0

Describe the bug

The worker::Ai binding cannot properly handle binary responses from AI image
generation models like Stable Diffusion. When calling ai.run() with an image
generation model, the binary image data gets lost and converted to an empty object
{}.

Current Behavior

When using the AI binding to generate images, the response is always an empty
object instead of the actual image bytes:

let ai = env.ai("AI")?;
let request = ImageGenRequest {
    prompt: "a beautiful sunset".to_string(),
};

// This returns an empty object {} instead of image bytes
let response: serde_json::Value =
ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", &request).await?;
// response is Object({}) - the actual image data is lost

Expected Behavior

The AI binding should return the raw image bytes. The JavaScript version works
correctly:

  const imageResponse = await
  env.AI.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", { prompt });
  // imageResponse contains the actual PNG image data
  return new Response(imageResponse, {
      headers: { "content-type": "image/png" }
  });

Steps To Reproduce

Reproduction

Minimal example that demonstrates the issue:

  use worker::*;
  use serde::{Deserialize, Serialize};

  #[derive(Serialize)]
  struct ImageGenRequest {
      prompt: String,
  }

  #[event(fetch)]
  async fn fetch(_req: Request, env: Env, _ctx: Context) -> Result<Response> {
      let ai = env.ai("AI")?;

      let request = ImageGenRequest {
          prompt: "test image".to_string(),
      };

      // This fails - returns empty object instead of image bytes
      let response: serde_json::Value =
  ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", &request).await?;

      console_log!("Response: {:?}", response); // Logs: Response: Object({})

      Response::error("Image data lost", 500)
  }

Impact

This makes it impossible to use Rust workers for AI image generation, speech
synthesis, or any other AI models that return binary data. This is a significant
limitation for developers wanting to use Rust for AI-powered applications on
Cloudflare Workers.

Console Output

  Polished prompt: <prompt text>
  Response from AI: Object({})
  Error generating image: Error: invalid type: JsValue(Object({})), expected a
  sequence

  The error shows the worker is trying to deserialize to Vec<u8> but receives an
  empty object instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions