Together Python Client
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
- breaking The Together Python SDK v2.0 (released December 2025) introduced significant breaking changes from v1.x. Key areas affected include constructor parameters (e.g., `supplied_headers` changed to `default_headers`), error handling class internals and exceptions, mandatory keyword arguments for all API calls (no positional parameters), and changes to many response type names in `together.types.*`.
- gotcha API calls in v2.x and later strictly require all parameters to be passed as keyword arguments. Positional arguments are no longer supported. Attempting to use positional arguments will result in runtime errors.
- gotcha For streaming responses (e.g., chat completions), the default `create` method eagerly reads the full response. To stream tokens incrementally as they are generated by the model, you must explicitly use the `.with_streaming_response` method within a context manager.
- gotcha The library extensively uses Pydantic for type validation and data modeling. If your project integrates `together-py` with other Python libraries (e.g., LangChain, FastAPI) that have differing or strict Pydantic v1 or v2 dependencies, you might encounter compatibility issues. Pydantic v2 is a major rewrite and not fully backward compatible with v1.
Install
-
pip install together -
uv add together
Imports
- Together
from together import Together
- AsyncTogether
from together import AsyncTogether
Quickstart
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.")