@devries-cameron20 / composio-1 / commits / 0d28befb1
Fork 0

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

fix(sdk): map streamed file transport failures (#4321)

## Summary - map Python file-fetch failures that occur after response headers into the documented upload and download errors - map TypeScript RemoteFile connection and streamed-body failures into RemoteFileDownloadError while preserving blocked-URL errors - close or cancel response bodies on every exit and apply the shared 100 MiB response limit to TypeScript RemoteFile downloads This supersedes the Python-only proposal in #4305 and carries the same failure category across both SDKs. ## Independent reproduction A response double returned one chunk and then raised a connection-reset error. On current next: - Python _fetch_file_from_url leaked ConnectionError, although it did close the response - Python Tool Router URL fetch leaked ConnectionError and left the response open - TypeScript RemoteFile leaked the native fetch/body TypeError instead of RemoteFileDownloadError ## Verification - Python make chk - Python make tst: 1,490 passed - TypeScript core typecheck - TypeScript core tests: 1,245 passed, 2 expected failures - TypeScript package build: 19 packages - focused Python regression tests: 3 passed - focused TypeScript RemoteFile tests: 17 passed

Alberto Schiabel committed Sep 3, 2026 at 12:06 UTC 0d28befb1422cf68a8c46a5dacaf3e1e8d3e9a75
7 files changed +201 -68
.changeset/tidy-files-stream-errors.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Map file-download transport failures to the SDK error contract and bound streamed response bodies.
python/composio/core/models/_files.py
+5 -1
@@ -456,11 +456,15 @@ def _fetch_file_from_url(
456 if chunk:
457 total_bytes += len(chunk)
458 if total_bytes > max_size:
459 - response.close()
459 raise ResponseTooLargeError(
460 f"Response size exceeds maximum allowed size ({max_size} bytes)"
461 )
462 chunks.append(chunk)
463 + except requests.exceptions.RequestException as e:
464 + raise ErrorUploadingFile(
465 + f"Failed to fetch file from URL: {_sanitize_url_for_logging(url)}. "
466 + f"Error: {e}"
467 + ) from e
468 finally:
469 response.close()
470
python/composio/core/models/tool_router_session_files.py
+29 -28
@@ -99,38 +99,39 @@ def _fetch_url_bytes(url: str) -> t.Tuple[bytes, str]:
99 except requests.exceptions.RequestException as e:
100 raise _UrlFetchError(cause=e) from e
101
102 - if response.status_code in (301, 302, 303, 307, 308):
103 - response.close()
104 - raise _UrlFetchError(redirected=True)
105 -
106 - if not response.ok:
107 - response.close()
108 - raise _UrlFetchError(
109 - status_code=response.status_code, status_text=response.reason
110 - )
102 + try:
103 + if response.status_code in (301, 302, 303, 307, 308):
104 + raise _UrlFetchError(redirected=True)
105
112 - content_length = parse_content_length(response.headers.get("Content-Length"))
113 - if content_length is not None and content_length > _MAX_RESPONSE_SIZE:
114 - response.close()
115 - raise _UrlFetchError(
116 - size_detail=(
117 - f"File size ({content_length} bytes) exceeds maximum allowed "
118 - f"size ({_MAX_RESPONSE_SIZE} bytes)"
106 + if not response.ok:
107 + raise _UrlFetchError(
108 + status_code=response.status_code, status_text=response.reason
109 )
120 - )
110
122 - chunks: t.List[bytes] = []
123 - total_bytes = 0
124 - for chunk in response.iter_content(chunk_size=8192):
125 - if chunk:
126 - total_bytes += len(chunk)
127 - if total_bytes > _MAX_RESPONSE_SIZE:
128 - response.close()
129 - raise _UrlFetchError(
130 - size_detail="Response size exceeds maximum allowed size"
111 + content_length = parse_content_length(response.headers.get("Content-Length"))
112 + if content_length is not None and content_length > _MAX_RESPONSE_SIZE:
113 + raise _UrlFetchError(
114 + size_detail=(
115 + f"File size ({content_length} bytes) exceeds maximum allowed "
116 + f"size ({_MAX_RESPONSE_SIZE} bytes)"
117 )
132 - chunks.append(chunk)
133 - response.close()
118 + )
119 +
120 + chunks: t.List[bytes] = []
121 + total_bytes = 0
122 + try:
123 + for chunk in response.iter_content(chunk_size=8192):
124 + if chunk:
125 + total_bytes += len(chunk)
126 + if total_bytes > _MAX_RESPONSE_SIZE:
127 + raise _UrlFetchError(
128 + size_detail="Response size exceeds maximum allowed size"
129 + )
130 + chunks.append(chunk)
131 + except requests.exceptions.RequestException as e:
132 + raise _UrlFetchError(cause=e) from e
133 + finally:
134 + response.close()
135
136 mimetype = response.headers.get("content-type", "application/octet-stream")
137 mimetype = mimetype.split(";")[0].strip()
python/tests/test_files.py
+18
@@ -1906,6 +1906,24 @@ class TestFetchFileFromUrl:
1906 assert "Failed to fetch file from URL" in str(exc_info.value)
1907 assert "404" in str(exc_info.value)
1908
1909 + @patch("composio.core.models._files.safe_get")
1910 + def test_fetch_file_from_url_maps_midstream_failure(self, mock_get):
1911 + def failing_stream(chunk_size=None):
1912 + yield b"partial"
1913 + raise requests.exceptions.ConnectionError("peer reset mid-stream")
1914 +
1915 + mock_response = MagicMock()
1916 + mock_response.ok = True
1917 + mock_response.status_code = 200
1918 + mock_response.headers = {"content-type": "text/plain"}
1919 + mock_response.iter_content.side_effect = failing_stream
1920 + mock_get.return_value = mock_response
1921 +
1922 + with pytest.raises(ErrorUploadingFile, match="peer reset mid-stream"):
1923 + _fetch_file_from_url("https://example.com/file.txt")
1924 +
1925 + mock_response.close.assert_called_once()
1926 +
1927 @patch("composio.core.models._files.safe_get")
1928 def test_fetch_file_from_url_decodes_percent_encoded_filename(self, mock_get):
1929 """Test that percent-encoded characters in URL filenames are decoded."""
python/tests/test_tool_router_session_files.py
+32
@@ -408,6 +408,38 @@ class TestResponseDerivedUrlsAreGuarded:
408 with pytest.raises(RemoteFileDownloadError, match="exceeds maximum"):
409 rf.buffer()
410
411 + def test_buffer_maps_midstream_failure_and_closes_response(self):
412 + rf = self._remote_file("https://s3.example.com/download")
413 + response = mock_stream_response()
414 +
415 + def failing_stream(chunk_size=None):
416 + yield b"partial"
417 + raise requests.exceptions.ConnectionError("peer reset mid-stream")
418 +
419 + response.iter_content = failing_stream
420 +
421 + with patch(SAFE_GET, return_value=response):
422 + with pytest.raises(RemoteFileDownloadError) as exc_info:
423 + rf.buffer()
424 +
425 + assert isinstance(exc_info.value.__cause__, requests.exceptions.ConnectionError)
426 + response.close.assert_called_once()
427 +
428 + def test_url_upload_maps_midstream_failure_and_closes_response(self, files_mount):
429 + response = mock_stream_response()
430 +
431 + def failing_stream(chunk_size=None):
432 + yield b"partial"
433 + raise requests.exceptions.ConnectionError("peer reset mid-stream")
434 +
435 + response.iter_content = failing_stream
436 +
437 + with patch(SAFE_GET, return_value=response):
438 + with pytest.raises(ValidationError, match="peer reset mid-stream"):
439 + files_mount.upload("https://files.example/input.txt")
440 +
441 + response.close.assert_called_once()
442 +
443 def test_text_and_save_inherit_the_guard(self, tmp_path):
444 """`text()` and `save()` read through `buffer()`, so they are covered."""
445 rf = self._remote_file("http://127.0.0.1:9000/download")
ts/packages/core/src/models/RemoteFile.ts
+54 -36
@@ -8,7 +8,12 @@ function getParentDir(filePath: string): string {
8 return filePath.slice(0, lastSep);
9 }
10 import { RemoteFileData, RemoteFileDataSchema } from '../types/ToolRouterSessionFilesMount.types';
11 -import { RemoteFileDownloadError, ValidationError } from '../errors';
11 +import {
12 + ComposioBlockedInternalUrlError,
13 + RemoteFileDownloadError,
14 + ValidationError,
15 +} from '../errors';
16 +import { readResponseBodyWithLimit } from '../utils/readResponseBody';
17
18 /**
19 * Represents a file stored in a tool router session's file mount.
@@ -77,31 +82,58 @@ export class RemoteFile {
82 return platform.basename(this.mountRelativePath);
83 }
84
85 + private downloadError(message: string, cause: unknown, response?: Response) {
86 + return new RemoteFileDownloadError(message, {
87 + statusCode: response?.status,
88 + statusText: response?.statusText,
89 + downloadUrl: this.downloadUrl,
90 + mountRelativePath: this.mountRelativePath,
91 + filename: this.filename,
92 + cause,
93 + });
94 + }
95 +
96 + private async downloadBytes(): Promise<{ content: Uint8Array; contentType: string }> {
97 + let response: Response;
98 + try {
99 + response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
100 + } catch (error) {
101 + if (error instanceof ComposioBlockedInternalUrlError) {
102 + throw error;
103 + }
104 + throw this.downloadError('Failed to download file', error);
105 + }
106 +
107 + if (!response.ok) {
108 + await response.body?.cancel().catch(() => undefined);
109 + throw this.downloadError(
110 + `Failed to download file: ${response.status} ${response.statusText}`,
111 + new Error(`HTTP ${response.status}: ${response.statusText}`),
112 + response
113 + );
114 + }
115 +
116 + try {
117 + const content = response.body
118 + ? await readResponseBodyWithLimit(response)
119 + : new Uint8Array(await response.arrayBuffer());
120 + return {
121 + content,
122 + contentType: response.headers?.get?.('content-type') ?? '',
123 + };
124 + } catch (error) {
125 + await response.body?.cancel().catch(() => undefined);
126 + throw this.downloadError('Failed to read downloaded file', error, response);
127 + }
128 + }
129 +
130 /**
131 * Fetches the file content as a buffer.
132 * @returns The file content as a Uint8Array
133 * @throws RemoteFileDownloadError if the fetch fails
134 */
135 async buffer(): Promise<Uint8Array> {
86 - // SSRF guard: `downloadUrl` is set from an API response, so it is untrusted
87 - // input like every other response field, and its bytes are handed straight
88 - // back to the caller. See ssrfGuard.node.ts.
89 - const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
90 - if (!response.ok) {
91 - throw new RemoteFileDownloadError(
92 - `Failed to download file: ${response.status} ${response.statusText}`,
93 - {
94 - statusCode: response.status,
95 - statusText: response.statusText,
96 - downloadUrl: this.downloadUrl,
97 - mountRelativePath: this.mountRelativePath,
98 - filename: this.filename,
99 - cause: new Error(`HTTP ${response.status}: ${response.statusText}`),
100 - }
101 - );
102 - }
103 - const arrayBuffer = await response.arrayBuffer();
104 - return new Uint8Array(arrayBuffer);
136 + return (await this.downloadBytes()).content;
137 }
138
139 /**
@@ -120,22 +152,8 @@ export class RemoteFile {
152 * @throws RemoteFileDownloadError if the fetch fails
153 */
154 async blob(): Promise<Blob> {
123 - // SSRF guard: see `buffer()`.
124 - const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
125 - if (!response.ok) {
126 - throw new RemoteFileDownloadError(
127 - `Failed to download file: ${response.status} ${response.statusText}`,
128 - {
129 - statusCode: response.status,
130 - statusText: response.statusText,
131 - downloadUrl: this.downloadUrl,
132 - mountRelativePath: this.mountRelativePath,
133 - filename: this.filename,
134 - cause: new Error(`HTTP ${response.status}: ${response.statusText}`),
135 - }
136 - );
137 - }
138 - return response.blob();
155 + const { content, contentType } = await this.downloadBytes();
156 + return new Blob([content as BlobPart], { type: contentType });
157 }
158
159 /**
ts/packages/core/test/models/RemoteFile.test.ts
+58 -3
@@ -124,6 +124,57 @@ describe('RemoteFile', () => {
124 message: expect.stringContaining('404'),
125 });
126 });
127 +
128 + it('should map a mid-stream transport failure and retain its cause', async () => {
129 + const cause = new TypeError('peer reset mid-stream');
130 + let pulls = 0;
131 + globalThis.fetch = vi.fn().mockResolvedValue(
132 + new Response(
133 + new ReadableStream({
134 + pull(controller) {
135 + if (pulls++ === 0) {
136 + controller.enqueue(new Uint8Array([1, 2, 3]));
137 + } else {
138 + controller.error(cause);
139 + }
140 + },
141 + })
142 + )
143 + );
144 +
145 + const file = new RemoteFile(validCamelCaseData);
146 + await expect(file.buffer()).rejects.toMatchObject({
147 + name: 'RemoteFileDownloadError',
148 + cause,
149 + });
150 + });
151 +
152 + it('should map a transport failure before response headers arrive', async () => {
153 + const cause = new TypeError('connection refused');
154 + globalThis.fetch = vi.fn().mockRejectedValue(cause);
155 +
156 + const file = new RemoteFile(validCamelCaseData);
157 + await expect(file.buffer()).rejects.toMatchObject({
158 + name: 'RemoteFileDownloadError',
159 + cause,
160 + });
161 + });
162 +
163 + it('should reject a declared body larger than the shared download limit', async () => {
164 + globalThis.fetch = vi.fn().mockResolvedValue(
165 + new Response('ignored', {
166 + headers: { 'content-length': String(101 * 1024 * 1024) },
167 + })
168 + );
169 +
170 + const file = new RemoteFile(validCamelCaseData);
171 + await expect(file.buffer()).rejects.toMatchObject({
172 + name: 'RemoteFileDownloadError',
173 + cause: expect.objectContaining({
174 + message: expect.stringContaining('exceeds maximum allowed size'),
175 + }),
176 + });
177 + });
178 });
179
180 describe('text', () => {
@@ -143,16 +194,20 @@ describe('RemoteFile', () => {
194
195 describe('blob', () => {
196 it('should fetch and return file content as Blob', async () => {
146 - const blob = new Blob(['data']);
197 + const blob = new Blob(['data'], { type: 'text/plain' });
198 globalThis.fetch = vi.fn().mockResolvedValue({
199 ok: true,
149 - blob: () => Promise.resolve(blob),
200 + status: 200,
201 + statusText: 'OK',
202 + headers: new Headers({ 'content-type': blob.type }),
203 + body: blob.stream(),
204 });
205
206 const file = new RemoteFile(validCamelCaseData);
207 const result = await file.blob();
208
155 - expect(result).toBe(blob);
209 + expect(result.type).toBe(blob.type);
210 + expect(await result.text()).toBe(await blob.text());
211 });
212
213 it('should throw RemoteFileDownloadError when fetch fails', async () => {