Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

refactor(python): collapse redundant download error handlers, cover both

jkomyno28bcb190d991
diff --git a/python/composio/core/models/_files.py b/python/composio/core/models/_files.py
index 4f9400c46..fa82209d3 100644
--- a/python/composio/core/models/_files.py
+++ b/python/composio/core/models/_files.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import contextlib
import functools
import hashlib
import os
@@ -663,6 +664,17 @@ class FileUploadable(BaseModel):
return cls(name=file.name, mimetype=mimetype, s3key=s3meta.key)
+def _discard_partial_download(outfile: Path) -> None:
+ """Remove a half-written download so it is never mistaken for the file.
+
+ Cleanup failures are swallowed on purpose: the error that triggered the
+ cleanup is what the caller needs to see, and an ``OSError`` raised from
+ here would replace it.
+ """
+ with contextlib.suppress(OSError):
+ outfile.unlink(missing_ok=True)
+
+
class FileDownloadable(BaseModel):
model_config = ConfigDict(json_schema_extra={"file_downloadable": True})
@@ -747,18 +759,14 @@ class FileDownloadable(BaseModel):
except ResponseTooLargeError:
# Propagates uncaught — callers must see the limit hit — but the
# truncated file must not be left behind as if it were the download.
- outfile.unlink(missing_ok=True)
+ _discard_partial_download(outfile)
raise
- except requests.exceptions.RequestException as e:
- outfile.unlink(missing_ok=True)
- raise ErrorDownloadingFile(
- "Error downloading file: "
- f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
- ) from e
except OSError as e:
- # A failing `fd.write` (disk full, permissions) would otherwise
- # escape the `ErrorDownloadingFile` contract this method documents.
- outfile.unlink(missing_ok=True)
+ # `requests.exceptions.RequestException` subclasses `OSError`, so a
+ # mid-stream transport failure and a failing `fd.write`/`mkdir`
+ # (disk full, permissions) both land here — and both owe the caller
+ # the `ErrorDownloadingFile` this method documents.
+ _discard_partial_download(outfile)
raise ErrorDownloadingFile(
"Error downloading file: "
f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
diff --git a/python/tests/test_files.py b/python/tests/test_files.py
index 8f9731929..aafdcd007 100644
--- a/python/tests/test_files.py
+++ b/python/tests/test_files.py
@@ -2700,6 +2700,50 @@ class TestDownloadSizeLimit:
assert outfile.exists()
assert outfile.read_bytes() == b"x" * 256 + b"y" * 256
+ @patch("composio.core.models._files.safe_get")
+ def test_download_wraps_stream_failure_and_removes_partial_file(
+ self, mock_get, tmp_path
+ ):
+ """A transport failure mid-stream keeps the documented error contract."""
+
+ def failing_stream(chunk_size=None):
+ yield b"x" * 256
+ raise requests.exceptions.ConnectionError("connection reset")
+
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {}
+ mock_response.iter_content.side_effect = failing_stream
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ErrorDownloadingFile):
+ self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
+
+ assert list(tmp_path.iterdir()) == []
+
+ @patch("composio.core.models._files.safe_get")
+ def test_download_wraps_write_failure_and_removes_partial_file(
+ self, mock_get, tmp_path
+ ):
+ """A disk failure while writing is an `ErrorDownloadingFile`, not a raw OSError."""
+
+ def failing_stream(chunk_size=None):
+ yield b"x" * 256
+ raise OSError(28, "No space left on device")
+
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {}
+ mock_response.iter_content.side_effect = failing_stream
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ErrorDownloadingFile):
+ self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
+
+ assert list(tmp_path.iterdir()) == []
+
class TestRedirectHandling:
"""Test redirect handling (redirects should be rejected)."""