{"id":2313,"library":"together","title":"Together Python Client","description":"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.","status":"active","version":"2.7.0","language":"en","source_language":"en","source_url":"https://github.com/togethercomputer/together-py","tags":["AI","LLM","API Client","Together AI","generative AI","chat","embeddings","fine-tuning"],"install":[{"cmd":"pip install together","lang":"bash","label":"Install via pip"},{"cmd":"uv add together","lang":"bash","label":"Install via uv (recommended)"}],"dependencies":[{"reason":"Requires Python 3.9 or higher for v2.x of the library.","package":"python","optional":false},{"reason":"Optional dependency for improved concurrency performance when using the `AsyncTogether` client. Install with `pip install 'together[aiohttp]'`.","package":"aiohttp","optional":true}],"imports":[{"symbol":"Together","correct":"from together import Together"},{"symbol":"AsyncTogether","correct":"from together import AsyncTogether"}],"quickstart":{"code":"import os\nfrom together import Together\n\n# Ensure TOGETHER_API_KEY is set in your environment variables\n# e.g., export TOGETHER_API_KEY=\"your_api_key_here\"\nclient = Together(\n    api_key=os.environ.get(\"TOGETHER_API_KEY\", \"\")\n)\n\ntry:\n    chat_completion = client.chat.completions.create(\n        messages=[\n            {\"role\": \"user\", \"content\": \"Say this is a test!\"}\n        ],\n        model=\"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo\",\n    )\n    print(chat_completion.choices[0].message.content)\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please ensure TOGETHER_API_KEY is set and valid, and you have access to the specified model.\")\n","lang":"python","description":"Demonstrates how to initialize the synchronous client and make a basic chat completion request using an environment variable for the API key."},"warnings":[{"fix":"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.","message":"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.*`.","severity":"breaking","affected_versions":">=2.0.0 (from v1.x)"},{"fix":"Always use `param=value` syntax for all arguments when calling methods such as `client.chat.completions.create()`.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions (potential conflict with other libraries)"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}