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.
pip install togetherVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize the synchronous client and make a basic chat completion request using an environment variable for the API key.
Refer to the official Python SDK Migration Guide for detailed API-by-API notes and code snippets. Update all API calls to use keyword arguments and adjust error handling and type imports accordingly.
Always use `param=value` syntax for all arguments when calling methods such as `client.chat.completions.create()`.
Wrap streaming calls with `with client.chat.completions.with_streaming_response(...) as stream_response:` and then iterate over `stream_response.iter_bytes()`, `iter_text()`, or `iter_lines()` for efficient token handling.
Ensure all libraries in your project are compatible with a single Pydantic version (preferably v2). If conflicts are unavoidable, consider using tools like `pydantic-compat` or isolating environments, though direct compatibility is ideal. `together-py` v2 is designed for Pydantic v2 environments.
Instantiate the `together.Together()` client and then use its methods like `client.chat.completions.create`.
```python
import together
client = together.Together()
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello world"}
],
model="mistralai/Mixtral-8x7B-Instruct-v0.1"
)
print(chat_completion.choices[0].message.content)
```Ensure your Together AI API key is correctly set as an environment variable (recommended) or passed directly to the `Together` client constructor. ```python import together import os # Option 1: Set environment variable (e.g., in your shell: export TOGETHER_API_KEY="your_key") # client = together.Together() # Option 2: Pass directly client = together.Together(api_key="YOUR_API_KEY") ```
Verify the exact model name from the Together AI model list (e.g., on their dashboard) and ensure it is correctly passed in the `model` parameter of your request.
```python
import together
client = together.Together()
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": "Hello"}
],
model="mistralai/Mixtral-8x7B-Instruct-v0.1" # Example correct model name
)
```Install the library using pip. ```bash pip install together ```