Implementation:Togethercomputer Together python ImageResponse Handling
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Image_Generation, API_Client |
| Last Updated | 2026-02-15 16:00 GMT |
Overview
The ImageResponse and ImageChoicesData types define the structure of image generation results returned by the Together AI API, and this page documents how to extract and save generated images from these objects.
Description
The image response types are defined as Pydantic models in src/together/types/images.py. The ImageResponse model wraps the API response with metadata and a list of generated images. Each image is represented by an ImageChoicesData instance containing either base64-encoded image data or a hosted URL.
The type hierarchy is:
ImageRequest: Defines the request parameters (prompt,model,seed,n,height,width,negative_prompt). Used internally byImages.generate()for request serialization.ImageChoicesData: Represents a single generated image with fieldsindex(int),b64_json(str | None), andurl(str | None).ImageResponse: The top-level response object with fieldsid(str | None),model(str | None),object(Literal["list"] | None), anddata(List[ImageChoicesData] | None).
Users typically do not import these types directly; they receive an ImageResponse instance as the return value of client.images.generate().
Usage
Use this after calling client.images.generate() to extract and process the generated images. Access image data through response.data[i].b64_json or response.data[i].url.
Code Reference
Source Location
- Repository: together-python
- File: src/together/types/images.py (lines 8-43)
Signature
class ImageRequest(BaseModel):
prompt: str
model: str
seed: int | None = None
n: int | None = 1
height: int | None = 1024
width: int | None = 1024
negative_prompt: str | None = None
class ImageChoicesData(BaseModel):
index: int
b64_json: str | None = None
url: str | None = None
class ImageResponse(BaseModel):
id: str | None = None
model: str | None = None
object: Literal["list"] | None = None
data: List[ImageChoicesData] | None = None
Import
# Typically accessed from the return value of client.images.generate()
from together import Together
client = Together()
response = client.images.generate(...) # Returns ImageResponse
# Direct import (rarely needed)
from together.types import ImageResponse, ImageChoicesData
I/O Contract
Inputs
The ImageResponse is not constructed by users directly. It is returned by Images.generate() and AsyncImages.generate().
Outputs (ImageResponse Fields)
| Name | Type | Description |
|---|---|---|
| id | None | Unique identifier for the generation job. |
| model | None | The model that was used for generation. |
| object | None | Object type, always "list".
|
| data | None | List of generated image results. |
Outputs (ImageChoicesData Fields)
| Name | Type | Description |
|---|---|---|
| index | int |
Position index of the image in the batch (0-based). |
| b64_json | None | Base64-encoded image data. Decode with base64.b64decode() to get raw bytes.
|
| url | None | Hosted URL where the generated image can be downloaded. |
Usage Examples
Accessing Base64 Image Data
from together import Together
client = Together()
response = client.images.generate(
prompt="A watercolor painting of a sunflower field",
model="black-forest-labs/FLUX.1-schnell-Free",
)
# Access the first image's base64 data
image_data_b64 = response.data[0].b64_json
print(f"Response ID: {response.id}")
print(f"Model used: {response.model}")
print(f"Base64 length: {len(image_data_b64)}")
Decoding and Saving to File
import base64
from together import Together
client = Together()
response = client.images.generate(
prompt="A minimalist logo design of a mountain",
model="black-forest-labs/FLUX.1-schnell-Free",
)
# Decode base64 to raw image bytes and save
image_bytes = base64.b64decode(response.data[0].b64_json)
with open("generated_image.png", "wb") as f:
f.write(image_bytes)
Processing Multiple Images
import base64
from together import Together
client = Together()
response = client.images.generate(
prompt="Abstract geometric art in bold colors",
model="stabilityai/stable-diffusion-xl-base-1.0",
n=4,
)
# Iterate over all generated images and save each one
for choice in response.data:
if choice.b64_json is not None:
image_bytes = base64.b64decode(choice.b64_json)
with open(f"image_{choice.index}.png", "wb") as f:
f.write(image_bytes)
print(f"Saved image_{choice.index}.png")
elif choice.url is not None:
print(f"Image {choice.index} available at: {choice.url}")
Accessing URL-Based Results
from together import Together
client = Together()
response = client.images.generate(
prompt="A photorealistic landscape of rolling hills",
model="black-forest-labs/FLUX.1-schnell-Free",
)
# Check if URL is available
if response.data[0].url:
print(f"Image URL: {response.data[0].url}")
# Download using requests or urllib if needed
Inspecting Response Metadata
from together import Together
client = Together()
response = client.images.generate(
prompt="A steampunk clockwork owl",
model="black-forest-labs/FLUX.1-schnell-Free",
seed=42,
)
# Inspect the full response structure
print(f"Job ID: {response.id}")
print(f"Model: {response.model}")
print(f"Object type: {response.object}") # "list"
print(f"Number of images: {len(response.data)}")
for choice in response.data:
print(f" Image {choice.index}:")
print(f" Has b64_json: {choice.b64_json is not None}")
print(f" Has url: {choice.url is not None}")