imagegen

package
v0.260604.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2026 License: MPL-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package imagegen provides vendor adapters for the text-to-image surfaces that do NOT speak the OpenAI /images/generations contract. Image generation is one of the most fragmented surfaces across AI providers: some ship entirely bespoke schemas (async task polling, custom request bodies). This package hides that fragmentation behind a single Client interface.

It is an implementation detail of client.OpenAIClient: that client's ImagesGenerate dispatches DashScope / MiniMax providers here and serves every OpenAI-compatible provider through its own native path. The package is intentionally a leaf (it does not import internal/client) so the client layer can depend on it without an import cycle.

Vendor landscape (derived from internal/data/providers.json):

OpenAI-compatible (POST {base}/images/generations, OpenAI request/response):
  openai-com, x-ai, volces-com, z-ai/bigmodel-cn, siliconflow, stepfun,
  together-xyz, modelscope-cn, googleapis-com (Gemini compat), baidubce-com,
  deepinfra-com, novita-ai, fireworks-ai, openrouter-ai, ...
  -> NOT handled here; client.OpenAIClient serves these natively.

OpenAI Responses API (image_generation tool, no /images/generations):
  codex (ChatGPT OAuth).
  -> NOT handled here; client.CodexClient.ImagesGenerate serves these.

Native async task API (submit -> poll task_id):
  dashscope-cn, dashscope-intl (Alibaba Wan / Tongyi Wanxiang, qwen-image).
  -> handled by dashscopeClient.

Native sync custom schema:
  minimaxi-com, minimax-io (MiniMax image-01, POST /v1/image_generation).
  -> handled by minimaxClient.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("imagegen: provider does not support image generation")

ErrUnsupported is returned by New when a provider has no native image generation adapter in this package. The caller (client.OpenAIClient) only dispatches DashScope / MiniMax providers here, so in practice this signals a detection/routing bug rather than an expected condition.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Generate produces one or more images for the given request.
	Generate(ctx context.Context, req *Request) (*Response, error)
	// Provider returns the upstream provider this client is bound to.
	Provider() *typ.Provider
	// Vendor returns the detected vendor family for diagnostics.
	Vendor() Vendor
	// Close releases any resources held by the client.
	Close() error
}

Client is the vendor-neutral image generation contract. Every vendor adapter implements it, so the gateway forwards image requests through a single path regardless of the upstream's native API shape.

func New

func New(provider *typ.Provider, model string) (Client, error)

New builds a native image generation Client for the given provider. It only serves vendors with a bespoke (non-OpenAI) image API — currently DashScope and MiniMax. The model argument is the already-routed upstream model id; it is not used for vendor selection (that is host-based) but adapters may read it.

OpenAI-compatible and Codex providers are NOT served here: client.OpenAIClient and client.CodexClient handle those on their own native paths. New returns ErrUnsupported for them, which in practice signals a routing bug since the caller is expected to dispatch only DashScope / MiniMax here.

type Image

type Image struct {
	URL           string
	B64JSON       string
	RevisedPrompt string
}

Image is a single generated image. Exactly one of URL or B64JSON is set, depending on the upstream and the requested ResponseFormat.

type Request

type Request struct {
	// Model is the upstream model id (already resolved by routing).
	Model string
	// Prompt is the text description of the desired image(s).
	Prompt string
	// N is the number of images to generate (default 1).
	N int
	// Size is "WIDTHxHEIGHT" (e.g. "1024x1024"). Adapters that expect an
	// aspect ratio or a "W*H" form convert from this value.
	Size string
	// Quality is one of standard|hd|low|medium|high|auto (vendor-dependent).
	Quality string
	// Style is one of vivid|natural (OpenAI dall-e-3 only).
	Style string
	// ResponseFormat is "url" or "b64_json". Not every vendor honors both.
	ResponseFormat string
	// Background is transparent|opaque|auto (GPT image models).
	Background string
	// OutputFormat is png|jpeg|webp (GPT image models).
	OutputFormat string
	// User is an opaque end-user identifier for abuse monitoring.
	User string
	// Extra carries vendor-specific passthrough parameters that have no
	// normalized field. Adapters merge these into their native request body.
	Extra map[string]any
}

Request is the normalized image generation request. It mirrors the common subset of the OpenAI Images API; vendor adapters translate it into their native schema and ignore fields they do not support (logging a warning).

func RequestFromOpenAI

func RequestFromOpenAI(p *openai.ImageGenerateParams) *Request

RequestFromOpenAI converts an OpenAI Images API request into the normalized Request. This lets the OpenAI-compatible inbound surface feed the gateway without each handler re-deriving the mapping.

func (*Request) ToOpenAIParams

func (req *Request) ToOpenAIParams() openai.ImageGenerateParams

ToOpenAIParams converts the normalized Request into OpenAI SDK params. It is used by the OpenAI-compatible adapter.

type Response

type Response struct {
	Created int64
	Model   string
	Data    []Image
	Usage   Usage
}

Response is the normalized image generation response.

func (*Response) ToOpenAI

func (r *Response) ToOpenAI() *openai.ImagesResponse

ToOpenAI converts the normalized Response back into the OpenAI Images API response shape, so OpenAI-compatible inbound clients see a familiar payload no matter which vendor served the request.

type Usage

type Usage struct {
	InputTokens  int64
	OutputTokens int64
	TotalTokens  int64
}

Usage holds token accounting when the upstream reports it (GPT image models and a few others). Zero values mean the upstream did not report usage.

type Vendor

type Vendor string

Vendor identifies the image generation surface a provider exposes. It is derived from the provider's API base host (and OAuth issuer), not from a user-chosen provider name, so it stays stable regardless of how the user labelled the provider.

const (
	// VendorOpenAICompat covers every provider that speaks the OpenAI
	// /images/generations contract. This is the large majority.
	VendorOpenAICompat Vendor = "openai_compat"
	// VendorCodex is ChatGPT OAuth: image generation only via Responses API.
	VendorCodex Vendor = "codex"
	// VendorDashScope is Alibaba Model Studio / DashScope (Wan, qwen-image):
	// native async task-submit-then-poll API.
	VendorDashScope Vendor = "dashscope"
	// VendorMinimax is MiniMax: native POST /v1/image_generation.
	VendorMinimax Vendor = "minimax"
	// VendorUnknown is a provider with no known image surface.
	VendorUnknown Vendor = "unknown"
)

func DetectVendor

func DetectVendor(provider *typ.Provider) Vendor

DetectVendor inspects a provider and returns the image generation vendor family it belongs to. Detection is host-based so it works for both the canonical providers in internal/data and user-defined clones that point at the same hosts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL