ImagesResource class
Resource for image operations.
Provides image generation, editing, and variation capabilities using
GPT image models (e.g. gpt-image-2) and DALL-E.
Access this resource through OpenAIClient.images.
Example
// Generate an image with GPT Image 2
final response = await client.images.generate(
ImageGenerationRequest(
model: ImageModels.gptImage2,
prompt: 'A white cat sitting on a windowsill',
size: ImageSize.size1024x1024,
background: ImageBackground.transparent,
),
);
// GPT image models always return base64; decode before using as bytes.
final b64Json = response.data.first.b64Json;
if (b64Json == null) {
throw StateError('Expected base64 image data but got null');
}
final bytes = base64Decode(b64Json);
print('Tokens used: ${response.usage?.totalTokens}');
- Inheritance
-
- Object
- ResourceBase
- ImagesResource
- Mixed-in types
Constructors
- ImagesResource({required OpenAIConfig config, required Client httpClient, required InterceptorChain interceptorChain, required RequestBuilder requestBuilder, void ensureNotClosed()?, Client streamClientFactory()?})
- Creates an ImagesResource.
Properties
- config → OpenAIConfig
-
Client configuration.
finalinherited
- ensureNotClosed → void Function()?
-
Callback to check if the client has been closed.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- httpClient → Client
-
HTTP client for making requests.
finalinherited
- interceptorChain → InterceptorChain
-
Interceptor chain for request/response processing.
finalinherited
- requestBuilder → RequestBuilder
-
Request builder for constructing HTTP requests.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- streamClientFactory → Client Function()?
-
Factory for creating dedicated HTTP clients for streaming requests.
finalinherited
Methods
-
createVariation(
ImageVariationRequest request) → Future< ImageResponse> - Creates variations of an existing image.
-
edit(
ImageEditRequest request) → Future< ImageResponse> - Creates edited or extended images.
-
editJson(
ImageEditJsonRequest request) → Future< ImageResponse> - Creates edited images using JSON payload references.
-
editJsonStream(
ImageEditJsonRequest request, {Future< void> ? abortTrigger}) → Stream<ImageEditStreamEvent> - Streams an image edit using a JSON payload (GPT image models only).
-
editStream(
ImageEditRequest request, {Future< void> ? abortTrigger}) → Stream<ImageEditStreamEvent> - Streams a multipart image edit (GPT image models only).
-
generate(
ImageGenerationRequest request) → Future< ImageResponse> - Generates images from a text prompt.
-
generateStream(
ImageGenerationRequest request, {Future< void> ? abortTrigger}) → Stream<ImageGenStreamEvent> - Streams image generation as Server-Sent Events (GPT image models only).
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
parseStreamError(
int statusCode, String body, String requestId) → ApiException -
Parses an error response from a streaming request.
inherited
-
sendStream(
{required BaseRequest request, String? jsonBody, Map< String, String> ? additionalHeaders, Future<void> ? abortTrigger}) → Future<StreamedResponse> -
Sends a streaming POST request and yields parsed SSE JSON events.
inherited
-
sendStreamRequest(
{required String endpoint, required Map< String, dynamic> body, Map<String, String> ? additionalHeaders, Future<void> ? abortTrigger}) → Future<StreamedResponse> -
Helper to create a streaming POST request with a JSON body.
inherited
-
streamSseEvents(
{required String endpoint, required Map< String, dynamic> body, Map<String, String> ? additionalHeaders, Future<void> ? abortTrigger}) → Stream<Map< String, dynamic> > -
Streams SSE events from a streaming POST request.
inherited
-
throwInlineStreamError(
Map< String, dynamic> json, String? sseEvent, Object? error) → Never -
Checks for inline errors in SSE stream data and throws
StreamException if found.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited