Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Togethercomputer Together python Images Generate

From Leeroopedia
Knowledge Sources
Domains Computer_Vision, Image_Generation, API_Client
Last Updated 2026-02-15 16:00 GMT

Overview

The Images.generate method sends a text prompt and generation parameters to a hosted diffusion model via the Together AI API and returns an ImageResponse containing the generated image data.

Description

The Images class (and its async counterpart AsyncImages) provides the generate() method as the primary interface for text-to-image generation. The method:

  1. Constructs an ImageRequest from the provided parameters using Pydantic model validation.
  2. Serializes the request with model_dump(exclude_none=True) to omit unset optional fields.
  3. Sends a POST request to the images/generations endpoint via the APIRequestor.
  4. Wraps the response data in an ImageResponse object.

The method accepts required parameters (prompt, model) and several optional parameters for controlling dimensions, count, seed, and negative prompts. Additional model-specific parameters (such as steps and image_base64) are passed through **kwargs.

Usage

Use this method whenever you need to generate images from text descriptions. Access it through an initialized Together client as client.images.generate(...).

Code Reference

Source Location

Signature

class Images:
    def __init__(self, client: TogetherClient) -> None:
        self._client = client

    def generate(
        self,
        *,
        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,
        **kwargs: Any,
    ) -> ImageResponse:
        ...
class AsyncImages:
    async def generate(
        self,
        *,
        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,
        **kwargs: Any,
    ) -> ImageResponse:
        ...

Import

from together import Together

client = Together()
# Access via: client.images.generate(...)

I/O Contract

Inputs

Name Type Required Default Description
prompt str Yes -- A text description of the desired image. Maximum length varies by model.
model str Yes -- The model identifier to use for generation (e.g., "black-forest-labs/FLUX.1-schnell-Free").
seed None No None Seed for reproducible generation. Same seed with same parameters yields the same image.
n None No 1 Number of images to generate per request.
height None No 1024 Height of the generated image in pixels.
width None No 1024 Width of the generated image in pixels.
negative_prompt None No None Text describing elements to avoid in the generated image.
**kwargs Any No -- Additional model-specific parameters, including steps (int, number of denoising steps) and image_base64 (str, reference image for image-to-image generation).

Outputs

Name Type Description
(return) ImageResponse Response object containing generated image data. Fields: id (str), model (str), object (Literal["list"]), data (List[ImageChoicesData]).

Usage Examples

Basic Image Generation

from together import Together

client = Together()

response = client.images.generate(
    prompt="A serene mountain lake at sunset with snow-capped peaks",
    model="black-forest-labs/FLUX.1-schnell-Free",
)

# Access the first generated image
print(response.data[0].b64_json)  # Base64-encoded image data

Generation with Full Parameters

from together import Together

client = Together()

response = client.images.generate(
    prompt="A cyberpunk street market with neon signs and rain reflections",
    model="stabilityai/stable-diffusion-xl-base-1.0",
    negative_prompt="blurry, low quality, watermark, text",
    n=2,
    height=768,
    width=1024,
    seed=42,
    steps=30,
)

# Access multiple generated images
for choice in response.data:
    print(f"Image {choice.index}: {len(choice.b64_json)} bytes (base64)")

Async Image Generation

import asyncio
from together import AsyncTogether

async def generate_image():
    client = AsyncTogether()

    response = await client.images.generate(
        prompt="An astronaut riding a horse on Mars, digital art",
        model="black-forest-labs/FLUX.1-schnell-Free",
        seed=12345,
    )

    return response.data[0]

result = asyncio.run(generate_image())

Reproducible Generation with Seed

from together import Together

client = Together()

# Both calls produce the same image
response1 = client.images.generate(
    prompt="A red fox in a snowy forest",
    model="black-forest-labs/FLUX.1-schnell-Free",
    seed=42,
)

response2 = client.images.generate(
    prompt="A red fox in a snowy forest",
    model="black-forest-labs/FLUX.1-schnell-Free",
    seed=42,
)

# response1.data[0].b64_json == response2.data[0].b64_json

Related Pages

Implements

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment