Skip to content

Execute AI Model returns response of type ai.AIRunResponseAudio for TextToImage request #4153

@amanbolat

Description

@amanbolat

Confirm this is a Go library issue and not an underlying Cloudflare API issue

  • This is an issue with the Go library

Describe the bug

Hi,

I tried to use this library to generate an image from the text prompt. According to the example here the response should follow the scheme:

{
    "type": "object",
    "contentType": "application/json",
    "properties": {
        "image": {
            "type": "string",
            "description": "The generated image in Base64 format."
        }
    }
}

Besides, I would expect that ai.AIRunResponseUnion could be casted to a TextToImage specific struct.

However, when I run the provided code snippet, I get the result where the type of the response is ai.AIRunResponseAudio.

Questions:
Is it an expected behavior?
Is there a specific type for TextToImage response?

Example spew dump of the response is below:

(ai.AIRunResponseAudio) {
 Audio: (string) "",
 JSON: (ai.aiRunResponseAudioJSON) {
  Audio: (apijson.Field) {
   raw: (string) "",
   status: (apijson.status) 0
  },
  raw: (string) (len=814380) "{\"image\":\"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH"}",
  ExtraFields: (map[string]apijson.Field) (len=1) {
   (string) (len=5) "image": (apijson.Field) {
    raw: (string) (len=814370) 
"\"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH",
    status: (apijson.status) 2
   }
  }
 }
}

To Reproduce

  1. Run client.AI.Run with model @cf/black-forest-labs/flux-1-schnell and body ai.AIRunParamsBodyTextToImage
  2. Debug the response using spew.

Code snippets

package ai_test

import (
	"context"
	"errors"
	"fmt"
	"os"
	"testing"

	"github.com/cloudflare/cloudflare-go/v4"
	"github.com/cloudflare/cloudflare-go/v4/ai"
	"github.com/cloudflare/cloudflare-go/v4/option"
	"github.com/davecgh/go-spew/spew"
	"github.com/stretchr/testify/require"
)

func TestName(t *testing.T) {
	client := cloudflare.NewClient(
		option.WithAPIToken("<api_token>"),
	)
	res, err := client.AI.Run(
		context.TODO(),
		"@cf/black-forest-labs/flux-1-schnell",
		ai.AIRunParams{
			AccountID: cloudflare.F("<account_id>"),
			Body: ai.AIRunParamsBodyTextToImage{
				Prompt: cloudflare.F("red cat on the table"),
			},
		},
	)
	if err != nil {
		var apierr *cloudflare.Error
		if errors.As(err, &apierr) {
			t.Log(string(apierr.DumpRequest(true)))
		}
		t.Fatalf("err should be nil: %s", err.Error())
	}

	fmt.Printf("%T", *res)

	// v, ok := (*res).(*ai.AIRunResponseAudio)

	f, err := os.Create("./dump.txt")
	require.NoError(t, err)
	spew.Fdump(f, *res)
}

OS

macOS

Go version

go1.24.2

Library version

github.com/cloudflare/cloudflare-go/v4 v4.2.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions