Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.8"
".": "0.1.0-alpha.9"
}
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## 0.1.0-alpha.9 (2025-05-31)

Full Changelog: [v0.1.0-alpha.8...v0.1.0-alpha.9](https://github.com/togethercomputer/together-py/compare/v0.1.0-alpha.8...v0.1.0-alpha.9)

### Features

* **api:** get file upload working ([cb8b8b8](https://github.com/togethercomputer/together-py/commit/cb8b8b86974721c2b2366e8481b88b3cb4851f0c))
* **api:** move upload to be a method of existing files resource ([b7c43be](https://github.com/togethercomputer/together-py/commit/b7c43be446e48390528994ee5a070699c490cec4))


### Bug Fixes

* **api:** correct file reroute handling, error message ([b8bc101](https://github.com/togethercomputer/together-py/commit/b8bc1010e047ba0b1bd75a311cb1220f13366f04))

## 0.1.0-alpha.8 (2025-05-29)

Full Changelog: [v0.1.0-alpha.7...v0.1.0-alpha.8](https://github.com/togethercomputer/together-py/compare/v0.1.0-alpha.7...v0.1.0-alpha.8)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "together"
version = "0.1.0-alpha.8"
version = "0.1.0-alpha.9"
description = "The official Python library for the together API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
6 changes: 6 additions & 0 deletions src/together/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,9 @@ def request(
if self.custom_auth is not None:
kwargs["auth"] = self.custom_auth

if options.follow_redirects is not None:
kwargs["follow_redirects"] = options.follow_redirects

log.debug("Sending HTTP Request: %s %s", request.method, request.url)

response = None
Expand Down Expand Up @@ -1460,6 +1463,9 @@ async def request(
if self.custom_auth is not None:
kwargs["auth"] = self.custom_auth

if options.follow_redirects is not None:
kwargs["follow_redirects"] = options.follow_redirects

log.debug("Sending HTTP Request: %s %s", request.method, request.url)

response = None
Expand Down
2 changes: 2 additions & 0 deletions src/together/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ class FinalRequestOptionsInput(TypedDict, total=False):
idempotency_key: str
json_data: Body
extra_json: AnyMapping
follow_redirects: bool


@final
Expand All @@ -750,6 +751,7 @@ class FinalRequestOptions(pydantic.BaseModel):
files: Union[HttpxRequestFiles, None] = None
idempotency_key: Union[str, None] = None
post_parser: Union[Callable[[Any], Any], NotGiven] = NotGiven()
follow_redirects: Union[bool, None] = None

# It should be noted that we cannot use `json` here as that would override
# a BaseModel method in an incompatible fashion.
Expand Down
2 changes: 2 additions & 0 deletions src/together/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class RequestOptions(TypedDict, total=False):
params: Query
extra_json: AnyMapping
idempotency_key: str
follow_redirects: bool


# Sentinel class used until PEP 0661 is accepted
Expand Down Expand Up @@ -215,3 +216,4 @@ class _GenericAlias(Protocol):

class HttpxSendArgs(TypedDict, total=False):
auth: httpx.Auth
follow_redirects: bool
2 changes: 1 addition & 1 deletion src/together/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "together"
__version__ = "0.1.0-alpha.8" # x-release-please-version
__version__ = "0.1.0-alpha.9" # x-release-please-version
6 changes: 3 additions & 3 deletions src/together/lib/cli/api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def files(_ctx: click.Context) -> None:
@click.option(
"--purpose",
type=str,
default="fine-tunes",
help="Purpose of file upload. Acceptable values in enum `together.types.FilePurpose`. Defaults to `fine-tunes`.",
default="fine-tune",
help="Purpose of file upload. Acceptable values in enum `together.types.FilePurpose`. Defaults to `fine-tune`.",
)
@click.option(
"--check/--no-check",
Expand All @@ -42,7 +42,7 @@ def upload(ctx: click.Context, file: pathlib.Path, purpose: str, check: bool) ->

client: Together = ctx.obj

response = client.files.upload(file=file, purpose=purpose, check=check)
response = client.files.upload_file(file=file, purpose=purpose, check=check)

click.echo(json.dumps(response.model_dump(exclude_none=True), indent=4))

Expand Down
151 changes: 74 additions & 77 deletions src/together/lib/resources/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import stat
import uuid
import shutil
import logging
import tempfile
from typing import Tuple
from typing import IO, Tuple, cast
from pathlib import Path
from functools import partial

Expand All @@ -21,6 +22,8 @@
from ..types.error import DownloadError, FileTypeError
from ..._exceptions import APIStatusError, AuthenticationError

log: logging.Logger = logging.getLogger(__name__)


def chmod_and_replace(src: Path, dst: Path) -> None:
"""Set correct permission before moving a blob from tmp directory to cache dir.
Expand Down Expand Up @@ -225,6 +228,7 @@ def get_upload_url(
path=url,
cast_to=httpx.Response,
body=data,
options={"headers": {"Content-Type": "multipart/form-data"}, "follow_redirects": False},
)
except APIStatusError as e:
if e.response.status_code == 401:
Expand All @@ -235,15 +239,14 @@ def get_upload_url(
response=e.response,
body=e.body,
) from e
raise

# Raise error for non 302 status codes
if response.status_code != 302:
raise APIStatusError(
f"Unexpected error raised by endpoint: {response.content.decode()}, headers: {response.headers}",
response=response,
body=response.content.decode(),
)
if e.response.status_code != 302:
raise APIStatusError(
f"Unexpected error raised by endpoint: {e.response.content.decode()}, headers: {e.response.headers}",
response=e.response,
body=e.response.content.decode(),
) from e
response = e.response

redirect_url = response.headers["Location"]
file_id = response.headers["X-Together-File-Id"]
Expand All @@ -263,21 +266,19 @@ def upload(
url: str,
file: Path,
purpose: FilePurpose,
redirect: bool = False,
) -> FileRetrieveResponse:
file_id = None

redirect_url = None
if redirect:
if file.suffix == ".jsonl":
filetype = "jsonl"
elif file.suffix == ".parquet":
filetype = "parquet"
else:
raise FileTypeError(
f"Unknown extension of file {file}. Only files with extensions .jsonl and .parquet are supported."
)
redirect_url, file_id = self.get_upload_url(url, file, purpose, filetype) # type: ignore
if file.suffix == ".jsonl":
filetype = "jsonl"
elif file.suffix == ".parquet":
filetype = "parquet"
else:
raise FileTypeError(
f"Unknown extension of file {file}. Only files with extensions .jsonl and .parquet are supported."
)
redirect_url, file_id = self.get_upload_url(url, file, purpose, filetype) # type: ignore

file_size = os.stat(file.as_posix()).st_size

Expand All @@ -289,33 +290,32 @@ def upload(
disable=bool(DISABLE_TQDM),
) as pbar:
with file.open("rb") as f:
wrapped_file = CallbackIOWrapper(pbar.update, f, "read")
wrapped_file = cast(IO[bytes], CallbackIOWrapper(pbar.update, f, "read"))

if redirect:
assert redirect_url is not None
callback_response = self._client.put(
cast_to=httpx.Response,
path=redirect_url,
body=wrapped_file,
)
else:
response = self._client.put(
cast_to=FileRetrieveResponse,
path=url,
body=wrapped_file,
)
assert redirect_url is not None
callback_response = self._client._client.put(
url=redirect_url,
content=wrapped_file.read(),
)
log.debug(
'HTTP Response: %s %s "%i %s" %s',
"put",
redirect_url,
callback_response.status_code,
callback_response.reason_phrase,
callback_response.headers,
)

if redirect:
assert isinstance(callback_response, httpx.Response) # type: ignore
assert isinstance(callback_response, httpx.Response) # type: ignore

if not callback_response.status_code == 200:
raise APIStatusError(
f"Error during file upload: {callback_response.content.decode()}, headers: {callback_response.headers}",
response=callback_response,
body=callback_response.content.decode(),
)
if not callback_response.status_code == 200:
raise APIStatusError(
f"Error during file upload: {callback_response.content.decode()}, headers: {callback_response.headers}",
response=callback_response,
body=callback_response.content.decode(),
)

response = self.callback(f"{url}/{file_id}/preprocess")
response = self.callback(f"{url}/{file_id}/preprocess")

assert isinstance(response, FileRetrieveResponse) # type: ignore

Expand Down Expand Up @@ -379,21 +379,19 @@ async def upload(
url: str,
file: Path,
purpose: FilePurpose,
redirect: bool = False,
) -> FileRetrieveResponse:
file_id = None

redirect_url = None
if redirect:
if file.suffix == ".jsonl":
filetype = "jsonl"
elif file.suffix == ".parquet":
filetype = "parquet"
else:
raise FileTypeError(
f"Unknown extension of file {file}. Only files with extensions .jsonl and .parquet are supported."
)
redirect_url, file_id = await self.get_upload_url(url, file, purpose, filetype) # type: ignore
if file.suffix == ".jsonl":
filetype = "jsonl"
elif file.suffix == ".parquet":
filetype = "parquet"
else:
raise FileTypeError(
f"Unknown extension of file {file}. Only files with extensions .jsonl and .parquet are supported."
)
redirect_url, file_id = await self.get_upload_url(url, file, purpose, filetype) # type: ignore

file_size = os.stat(file.as_posix()).st_size

Expand All @@ -405,33 +403,32 @@ async def upload(
disable=bool(DISABLE_TQDM),
) as pbar:
with file.open("rb") as f:
wrapped_file = CallbackIOWrapper(pbar.update, f, "read")
wrapped_file = cast(IO[bytes], CallbackIOWrapper(pbar.update, f, "read"))

if redirect:
assert redirect_url is not None
callback_response = self._client.put(
cast_to=httpx.Response,
path=redirect_url,
body=wrapped_file,
)
else:
response = self._client.put(
cast_to=FileRetrieveResponse,
path=url,
body=wrapped_file,
)
assert redirect_url is not None
callback_response = await self._client._client.put(
url=redirect_url,
content=wrapped_file.read(),
)
log.debug(
'HTTP Response: %s %s "%i %s" %s',
"put",
redirect_url,
callback_response.status_code,
callback_response.reason_phrase,
callback_response.headers,
)

if redirect:
assert isinstance(callback_response, httpx.Response) # type: ignore
assert isinstance(callback_response, httpx.Response) # type: ignore

if not callback_response.status_code == 200:
raise APIStatusError(
f"Error during file upload: {callback_response.content.decode()}, headers: {callback_response.headers}",
response=callback_response,
body=callback_response.content.decode(),
)
if not callback_response.status_code == 200:
raise APIStatusError(
f"Error during file upload: {callback_response.content.decode()}, headers: {callback_response.headers}",
response=callback_response,
body=callback_response.content.decode(),
)

response = self.callback(f"{url}/{file_id}/preprocess")
response = self.callback(f"{url}/{file_id}/preprocess")

assert isinstance(response, FileRetrieveResponse) # type: ignore

Expand Down
18 changes: 2 additions & 16 deletions src/together/lib/types/error.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
from typing import Any

from ..._exceptions import TogetherError


class DownloadError(TogetherError):
def __init__(
self,
message: str,
**kwargs: Any,
) -> None:
self.message = message
super().__init__(**kwargs)
pass


class FileTypeError(TogetherError):
def __init__(
self,
message: str,
**kwargs: Any,
) -> None:
self.message = message
super().__init__(**kwargs)
pass
8 changes: 4 additions & 4 deletions src/together/resources/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def delete(
cast_to=FileDeleteResponse,
)

def upload(
def upload_file(
self,
file: Path | str,
*,
Expand All @@ -162,7 +162,7 @@ def upload(

purpose = cast(FilePurpose, purpose)

return upload_manager.upload("files", file, purpose=purpose, redirect=True)
return upload_manager.upload("files", file, purpose=purpose)

def content(
self,
Expand Down Expand Up @@ -304,7 +304,7 @@ async def delete(
cast_to=FileDeleteResponse,
)

async def upload(
async def upload_file(
self,
file: Path | str,
*,
Expand All @@ -326,7 +326,7 @@ async def upload(

purpose = cast(FilePurpose, purpose)

return await upload_manager.upload("files", file, purpose=purpose, redirect=True)
return await upload_manager.upload("files/upload", file, purpose=purpose)

async def content(
self,
Expand Down
Loading
Loading