OpenAIError
together_ai.openai.OpenAIError
Stack trace
together_ai.openai.OpenAIError: API request failed with status code 401: Unauthorized
File "/app/main.py", line 23, in <module>
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
File "/usr/local/lib/python3.9/site-packages/together_ai/openai.py", line 112, in create
raise OpenAIError(f"API request failed with status code {response.status_code}: {response.text}") Why it happens
Together AI's SDK mimics OpenAI's API but requires correct API key setup and network connectivity. This error occurs when the API key is missing, invalid, or the request format is incorrect, causing the SDK to raise an OpenAIError.
Detection
Monitor API call responses and catch together_ai.openai.OpenAIError exceptions to log detailed error messages and identify misconfigurations before the app crashes.
Causes & fixes
API key is missing or not set in environment variables
Set the TOGETHER_API_KEY environment variable before running your application to authenticate requests.
Using an invalid or expired API key
Verify your API key on the Together AI dashboard and update the environment variable with a valid key.
Incorrect usage of the Together AI SDK chat completion method parameters
Ensure you use the correct method signature: client.chat.completions.create(model=..., messages=...) with properly formatted messages.
Network connectivity issues or API endpoint unreachable
Check your network connection and firewall settings to allow outbound requests to Together AI's API endpoints.
Code: broken vs fixed
from together_ai import OpenAI
client = OpenAI()
messages = [{"role": "user", "content": "Hello!"}]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages) # Raises OpenAIError due to missing API key
print(response.choices[0].message.content) import os
from together_ai import OpenAI
os.environ["TOGETHER_API_KEY"] = "your_api_key_here" # Set your API key here
client = OpenAI()
messages = [{"role": "user", "content": "Hello!"}]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages) # Fixed: API key set
print(response.choices[0].message.content) Workaround
Wrap the API call in try/except OpenAIError, log the error details, and retry after confirming API key and network status.
Prevention
Always set and validate the TOGETHER_API_KEY environment variable before deployment and implement error handling for API call failures.