Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/xai_sdk/aio/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async def upload(
res = await self._stub.UploadFile(chunks)
if not should_disable_sensitive_attributes():
span.set_attribute("file.id", res.id)
span.set_attribute("file.filename", res.filename)
span.set_attribute("file.name", res.filename)
return res

async def batch_upload(
Expand Down Expand Up @@ -209,6 +209,8 @@ async def batch_upload(
"""
if len(files) == 0:
raise ValueError("files cannot be empty - please provide at least one file to upload")
if batch_size < 1:
raise ValueError("batch_size must be at least 1")

semaphore = Semaphore(batch_size)
results: dict[int, Union[files_pb2.File, BaseException]] = {}
Expand Down
2 changes: 2 additions & 0 deletions src/xai_sdk/sync/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def batch_upload(
"""
if len(files) == 0:
raise ValueError("files cannot be empty - please provide at least one file to upload")
if batch_size < 1:
raise ValueError("batch_size must be at least 1")

results: dict[int, Union[files_pb2.File, BaseException]] = {}

Expand Down
12 changes: 11 additions & 1 deletion tests/aio/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,16 @@ async def test_batch_upload_empty_list(client_with_mock_stub: AsyncClient):
await client_with_mock_stub.files.batch_upload([])


@pytest.mark.asyncio
async def test_batch_upload_invalid_batch_size(client_with_mock_stub: AsyncClient):
"""Test batch upload rejects non-positive batch sizes."""
with pytest.raises(ValueError, match="batch_size must be at least 1"):
await client_with_mock_stub.files.batch_upload(["file.txt"], batch_size=0)

with pytest.raises(ValueError, match="batch_size must be at least 1"):
await client_with_mock_stub.files.batch_upload(["file.txt"], batch_size=-1)


@mock.patch("xai_sdk.aio.files.tracer")
@pytest.mark.asyncio
async def test_upload_creates_span_with_correct_attributes(
Expand Down Expand Up @@ -745,7 +755,7 @@ async def async_return():
)

mock_span.set_attribute.assert_any_call("file.id", result.id)
mock_span.set_attribute.assert_any_call("file.filename", result.filename)
mock_span.set_attribute.assert_any_call("file.name", result.filename)


@mock.patch("xai_sdk.aio.files.tracer")
Expand Down
9 changes: 9 additions & 0 deletions tests/sync/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ def test_batch_upload_empty_list(client_with_mock_stub: Client):
client_with_mock_stub.files.batch_upload([])


def test_batch_upload_invalid_batch_size(client_with_mock_stub: Client):
"""Test batch upload rejects non-positive batch sizes."""
with pytest.raises(ValueError, match="batch_size must be at least 1"):
client_with_mock_stub.files.batch_upload(["file.txt"], batch_size=0)

with pytest.raises(ValueError, match="batch_size must be at least 1"):
client_with_mock_stub.files.batch_upload(["file.txt"], batch_size=-1)


@mock.patch("xai_sdk.sync.files.tracer")
def test_upload_creates_span_with_correct_attributes(
mock_tracer: mock.MagicMock, client_with_mock_stub: Client, mock_stub
Expand Down
Loading