Together Python Client

2.7.0 · active · verified Thu Apr 09

The Together Python library (v2.x) provides convenient access to the Together AI REST API for Python 3.9+ applications. It offers strongly-typed request parameters and response fields, with both synchronous and asynchronous clients powered by httpx. This modern SDK is generated from the OpenAPI specification using Stainless, ensuring a 1:1 mapping to the API and rapid feature delivery. Version 1.x is now in maintenance mode, with all new development focused on v2.x.

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the synchronous client and make a basic chat completion request using an environment variable for the API key.

import os
from together import Together

# Ensure TOGETHER_API_KEY is set in your environment variables
# e.g., export TOGETHER_API_KEY="your_api_key_here"
client = Together(
    api_key=os.environ.get("TOGETHER_API_KEY", "")
)

try:
    chat_completion = client.chat.completions.create(
        messages=[
            {"role": "user", "content": "Say this is a test!"}
        ],
        model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
    )
    print(chat_completion.choices[0].message.content)
except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure TOGETHER_API_KEY is set and valid, and you have access to the specified model.")

view raw JSON →