Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

refactor(sdk-python): spell out the download stream's exception cases

Maohaof93c046703a8
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/download_response.py b/sdks/sandbox/python/src/opensandbox/adapters/download_response.py
index 7432fd2a4..ee2edb751 100644
--- a/sdks/sandbox/python/src/opensandbox/adapters/download_response.py
+++ b/sdks/sandbox/python/src/opensandbox/adapters/download_response.py
@@ -94,11 +94,15 @@ class _AsyncResponseByteStream:
raise StopAsyncIteration
try:
return await self._iterator.__anext__()
- except BaseException as e:
+ except StopAsyncIteration:
+ await self.aclose()
+ raise
+ except Exception as e:
+ await self.aclose()
+ # Same mapping as the request itself, e.g. a reset mid-body.
+ raise ExceptionConverter.to_sandbox_exception(e) from e
+ except BaseException:
await self.aclose()
- if isinstance(e, Exception) and not isinstance(e, StopAsyncIteration):
- # Same mapping as the request itself, e.g. a reset mid-body.
- raise ExceptionConverter.to_sandbox_exception(e) from e
raise
async def aclose(self) -> None:
@@ -131,11 +135,15 @@ class _ResponseByteStream:
raise StopIteration
try:
return next(self._iterator)
- except BaseException as e:
+ except StopIteration:
+ self.close()
+ raise
+ except Exception as e:
+ self.close()
+ # Same mapping as the request itself, e.g. a reset mid-body.
+ raise ExceptionConverter.to_sandbox_exception(e) from e
+ except BaseException:
self.close()
- if isinstance(e, Exception) and not isinstance(e, StopIteration):
- # Same mapping as the request itself, e.g. a reset mid-body.
- raise ExceptionConverter.to_sandbox_exception(e) from e
raise
def close(self) -> None: