How to beginner · 3 min read

Together AI image generation

Quick answer
Use the OpenAI SDK with base_url="https://api.together.xyz/v1" and your TOGETHER_API_KEY to generate images by calling client.chat.completions.create with an image generation prompt. Specify the model and prompt in the request messages to get image URLs in the response.

PREREQUISITES

  • Python 3.8+
  • Together AI API key (set TOGETHER_API_KEY environment variable)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your Together AI API key as an environment variable.

  • Install SDK: pip install openai
  • Set environment variable: export TOGETHER_API_KEY="your_api_key_here" (Linux/macOS) or set TOGETHER_API_KEY=your_api_key_here (Windows)
bash
pip install openai

Step by step

This example shows how to generate an image using Together AI's OpenAI-compatible API endpoint. It sends a prompt to the meta-llama/Llama-3.3-70B-Instruct-Turbo model with an image generation request and prints the returned image URL.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["TOGETHER_API_KEY"], base_url="https://api.together.xyz/v1")

messages = [
    {"role": "user", "content": "Generate an image of a futuristic city skyline at sunset."}
]

response = client.chat.completions.create(
    model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
    messages=messages
)

# The image URL is typically in the content or tool_calls depending on API response
print("Response content:", response.choices[0].message.content)

# If the response includes tool calls for image generation, parse accordingly
if response.choices[0].finish_reason == "tool_calls":
    tool_call = response.choices[0].message.tool_calls[0]
    print("Image generation arguments:", tool_call.function.arguments)
output
Response content: [Image URL or generation confirmation text]
Image generation arguments: {"prompt": "Generate an image of a futuristic city skyline at sunset."}

Common variations

You can use different models optimized for image generation if available on Together AI. For asynchronous calls, use async with an async client. Streaming is not typically used for image generation but is supported for chat completions.

  • Change model to an image-specific model if provided by Together AI.
  • Use async calls with asyncio for concurrency.
  • Adjust prompt style to control image details.
python
import asyncio
from openai import OpenAI

async def generate_image():
    client = OpenAI(api_key=os.environ["TOGETHER_API_KEY"], base_url="https://api.together.xyz/v1")
    messages = [{"role": "user", "content": "A fantasy forest with glowing plants."}]
    response = await client.chat.completions.create(
        model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
        messages=messages
    )
    print(response.choices[0].message.content)

asyncio.run(generate_image())
output
https://together.ai/generated-images/abc123.png

Troubleshooting

  • If you get authentication errors, verify your TOGETHER_API_KEY environment variable is set correctly.
  • If the response does not contain image URLs, check if the model supports image generation or if you need to specify additional parameters.
  • For network errors, ensure your internet connection and that https://api.together.xyz/v1 is reachable.

Key Takeaways

  • Use the OpenAI SDK with base_url="https://api.together.xyz/v1" to access Together AI.
  • Set your API key in the TOGETHER_API_KEY environment variable for authentication.
  • Send prompts via chat.completions.create to generate images and parse the returned URLs.
  • Use async calls for concurrency and switch models if Together AI offers dedicated image generation models.
  • Check API response structure for tool calls to handle image generation arguments properly.
Verified 2026-04 · meta-llama/Llama-3.3-70B-Instruct-Turbo
Verify ↗