You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(buckets): open a bucket file as a streamed, seekable file - #2036
Adds open() to BucketsService (sdk.buckets). It returns a read-only binary file over a file in a bucket, like a file opened with open(path, "rb"). The file is downloaded as it is read, so it can be passed to anything that takes a file without saving it to disk or holding it in memory first.
The use case is a coded agent that sends large bucket files (50 MB and more) to Document Understanding. Today it has to download each file to disk, or into memory, before uploading it. With open the bytes go from the bucket to DU in 64 KiB chunks. No change to sdk.documents is needed: file= already accepts a binary file, and httpx sends this one like any other.
How it works
open: finds the bucket and returns an io.BufferedReader over a new private BucketFileReader (io.RawIOBase). The public return type is BinaryIO.
Read URL: each download looks up a fresh read URL, the first one included. On Orchestrator storage a URL is valid for about an hour, and a new URL also misses responses cached for an older one.
Read: a streamed GET, read one chunk at a time. The GET goes through the authenticated client, with the current token, when RequiresAuth is set, and through custom_client otherwise, as in download.
Size: from the download's Content-Length, or from Content-Range after a range request. If the storage reports neither, the size is known once a download reaches the end of the file. Asking for the size starts the download that the first read then continues, so seek(0, SEEK_END) followed by a full read is one GET.
Seek: moves the position only. The next read continues the open download when it is already at that position. Otherwise it starts a new download from there, with a Range header, and skips ahead when the storage ignores Range. Reading past the end returns end of file.
Rewind and retry: before each send, httpx rewinds a file passed to files=, so a retried upload sends the whole file again, from a new download.
Changed files: every download must return the same version of the file, by ETag and size. If the file was replaced in the bucket, the read raises the new BucketFileChangedError instead of cutting the file off or mixing bytes of two versions. Only an ETag tells two versions of the same size apart. Native Orchestrator storage can stream with no ETag or size, so there:
a new download from the start of the file, as a retried upload makes, is allowed: it has the whole file, so it can't mix versions. Its length must match the size already known, from its headers or, when it reports none, as it is read;
a new download from the middle of the file raises BucketFileChangedError.
Failures: the reader does not retry, and neither does its read-URL lookup. The code that reads the file retries, such as the sdk.documents upload's request retry, so a failure is retried at one layer.
An error status raises EnrichedException, as other SDK calls do.
A dropped connection, or a download that the storage ends before the end of the file, raises the new BucketFileReadError. The platform retry treats it as transient: the request sending the file was cut short, so it is sent again with the whole file. Read errors from the service itself keep the existing policy.
The next read starts a new download.
Signed URLs: the query string of a read URL is its signature.
A storage error carries the read URL without it. The status and headers stay, for the retry.
The download runs under suppress_http_instrumentation(), so HTTP client spans (httpx instrumentation, as in the agents runtime) don't record its URL. OTel redacts sig and Signature but not, for example, X-Amz-Signature.
Encoding: the GET asks for Accept-Encoding: identity, so the bytes read are the stored bytes and match the reported size.
download:download and download_async now use the shared read-URL lookup. Their behaviour is unchanged.
Async code
There is no open_async. Reading the file waits on the storage, as reading any file waits on the disk, and httpx reads files= uploads synchronously even in AsyncClient. So from async code, run open and the call that reads the file in a worker thread:
With a 100 MiB file from storage that pauses for 300 ms once, that kept the longest event-loop stall at 12 ms. Passing the file to start_ixp_extraction_async stalled it for 301 ms. Making the documents async upload read files off the event loop, local files included, is a follow-up in documents.
Testing
Unit tests:TestOpen in tests/services/test_buckets_service.py:
reading, getting the size without a second download, and rewinding to a new download from a new URL;
range requests, including when the storage ignores them, and reading past the end;
the URL looked up when the download starts;
the platform credentials and current token for storage that requires auth;
failures: no retry in the reader or its URL lookup, the next read starting again, and error responses closed;
a platform upload that retries a dropped download with the whole file, and a URL lookup failure retried five times in all, not five per attempt;
a download that ends before the end of the file;
the read URL's signature kept out of the error, with the status and Retry-After kept, and out of httpx instrumentation spans, on success and on an error status (opentelemetry-instrumentation-httpx is a new dev dependency for this test);
storage without an ETag or size: a rewind downloads the whole file again, a replacement of another length fails, a download from the middle is refused, and a platform upload retries a dropped download, chunked;
an httpx files= upload with an exact Content-Length that resends the whole file on a retry;
a file replaced in the bucket, by ETag and by size;
the size from a later 206 when the first download had none.
Full suite: the uipath-platform suite passes. ruff and mypy are clean on the changed files.
Live test on alpha (Orchestrator bucket storage, served through Cloudflare, RequiresAuth: false):
a 20 MiB file read whole and from the middle (206);
a file replaced in the bucket and read again, which raised BucketFileChangedError;
sdk.documents.start_ixp_extraction(file=<opened bucket file>), with the IXP extraction finishing with the expected values;
the same with storage without an ETag or size (headers removed from the real responses) and the first download dropped halfway: the upload was retried, chunked, from a new download, and the extraction finished.
Storage behaviour seen on alpha:ETag, Content-Length and Accept-Ranges: bytes are sent. If-Match is ignored. Read URLs expire after about an hour. Responses are cached per URL for 4 hours (cache-control: public, max-age=14400).
Notes
Resume triggers: with DocumentExtraction(file=...), the extraction starts after the step pauses, and by then a with block has closed the file. That case should keep the file open, or use file_path.
Follow-ups:
documents' async upload reading files off the event loop;
download streaming to disk with the same reader;
download and upload to external storage not checking the response status;
httpx logs every request URL at INFO level, so signed read URLs reach logs wherever INFO is on, for download too;
download and upload sending signed URLs with HTTP instrumentation on, as open did before this change.
`sdk.buckets.open()` and `open_async()` return a read-only binary file over
a file in a bucket. The file is downloaded as it is read, so it can be
handed to anything that takes a file, such as `file=` of
`sdk.documents.start_ixp_extraction`, without saving it to disk or holding
it in memory first.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
From review of the bucket file reader:
- A download that fails is dropped, so the next read starts a new one.
Before, a rewind after a failure before the first chunk read as the end
of the file, so a retried upload sent no file bytes.
- When the storage reports no size, the size is known once a download
reaches the end of the file, so reading again at the end returns b"".
- A seek past the end before any read returns end of file: a 416 response
gives the size instead of raising.
- A download that needs platform credentials sends the current token,
which may have been renewed since the client was created.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The reader ignores size information in later responses after an initial unknown size, and failed error-body reads can leave response streams open during retries.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Discover size from later responses when initially unknown
If the initial download omits Content-Length, _size_checked becomes true while _size remains unknown. A later seek can receive a valid 206 with Content-Range, but this guard ignores its total, so seek(0, SEEK_END) still raises UnsupportedOperation even though storage has now reported the size. Please keep checking new responses until _size is known.
Close error responses when reading their bodies fails
If reading an error response body raises (for example, a timeout while receiving a 503 body), response.close() is skipped. The reader has not received this response yet, so it cannot clean it up either, leaving an open stream behind on each retry. Close the response in a finally block so failed error-body reads also release the connection.
- Remove open_async. Reading the file waits on the storage, like reading
any file waits on the disk, so an async method would suggest otherwise.
From async code, read the file in a worker thread.
- Do not retry downloads in the reader. The code that reads the file
retries, such as the sdk.documents upload, so a storage failure is
retried once instead of up to 25 times.
- Raise BucketFileChangedError when a later download returns another
version of the file, by ETag or size, instead of cutting it off or
mixing two versions. Take the size from the first response reporting it.
- Look up a new read URL before every download, the first one included:
the URL expires, and a new one misses responses cached for an older one.
- Close an error response even when reading its body fails.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Unresolved issues allow replaced files to be silently truncated or accepted at the wrong length and cause storage connection resets to abort uploads instead of triggering retries.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Validate replacement download length against cached size
When storage omits ETag and size headers, rewinding after a replacement does not validate its actual length against the cached size. A larger replacement can be silently cut off at the old size, while a shorter replacement reaches EOF at lines 92–94 without raising BucketFileChangedError. Validate the observed download length against the cached size, rejecting both premature EOF and extra bytes rather than treating the cached size alone as proof of EOF.
Make storage read errors eligible for upload retries
A storage connection reset raises httpx.ReadError, which is re-raised unchanged here, but the documents upload's retry predicate in common/retry.py:74–81 does not accept it. Consequently, start_ixp_extraction(file=...) aborts instead of rewinding and retrying the download. Make storage-body failures recognizable to the upload retry policy while keeping retries out of the reader itself.
The reason will be displayed to describe this comment to others. Learn more.
native Orchestrator can stream without ETag or Content-Length. after a rewind, the cached size can truncate a larger replacement or accept a shorter one, and matching sizes can still mix versions. let's require a reliable storage version or fail a reopen we can't validate. earlier version-check thread
… errors
- A download that fails, or that the storage ends before the end of the
file, raises BucketFileReadError. The platform retry treats it as
transient: the request sending the file was cut short, so a documents
upload sends the whole file again from a new download. Errors from the
service itself keep the existing retry policy.
- The read-URL lookup of an opened file makes a single attempt, like the
download: whoever reads the file retries, at one layer.
- A storage error carries the read URL without its query string, which is
its signature; the status and headers stay for the retry.
- A download after the first needs the first one's ETag. A storage that
reports none can't show a replacement of the same size, so such a file
isn't downloaded a second time (BucketFileChangedError, verified=False).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Native Orchestrator storage can stream a file with no ETag or size. The
previous rule refused any second download there, so a retried upload
failed instead of sending the file again.
A download from the start of the file has the whole file, so it can't
mix two versions: without an ETag it is now allowed, and checked by its
length against the size already known, from its headers or, when it
reports none, as it is read (no extra bytes, no early end). A download
from the middle still needs the ETag. Reads also stop at the known size.
Checked live on alpha with the ETag and size headers removed and the
first download dropped halfway: the documents upload retried, chunked,
and the extraction finished.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The read URL's query string is its signature, and httpx instrumentation,
as the agents runtime sets it up, records each request's URL. It redacts
`sig` and `Signature`, not every storage's signature, such as
`X-Amz-Signature`. The download now runs under
`suppress_http_instrumentation()`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
open()toBucketsService(sdk.buckets). It returns a read-only binary file over a file in a bucket, like a file opened withopen(path, "rb"). The file is downloaded as it is read, so it can be passed to anything that takes a file without saving it to disk or holding it in memory first.The use case is a coded agent that sends large bucket files (50 MB and more) to Document Understanding. Today it has to
downloadeach file to disk, or into memory, before uploading it. Withopenthe bytes go from the bucket to DU in 64 KiB chunks. No change tosdk.documentsis needed:file=already accepts a binary file, and httpx sends this one like any other.How it works
open: finds the bucket and returns anio.BufferedReaderover a new privateBucketFileReader(io.RawIOBase). The public return type isBinaryIO.RequiresAuthis set, and throughcustom_clientotherwise, as indownload.Content-Length, or fromContent-Rangeafter a range request. If the storage reports neither, the size is known once a download reaches the end of the file. Asking for the size starts the download that the first read then continues, soseek(0, SEEK_END)followed by a full read is one GET.Rangeheader, and skips ahead when the storage ignoresRange. Reading past the end returns end of file.files=, so a retried upload sends the whole file again, from a new download.ETagand size. If the file was replaced in the bucket, the read raises the newBucketFileChangedErrorinstead of cutting the file off or mixing bytes of two versions. Only anETagtells two versions of the same size apart. Native Orchestrator storage can stream with noETagor size, so there:BucketFileChangedError.sdk.documentsupload's request retry, so a failure is retried at one layer.EnrichedException, as other SDK calls do.BucketFileReadError. The platform retry treats it as transient: the request sending the file was cut short, so it is sent again with the whole file. Read errors from the service itself keep the existing policy.suppress_http_instrumentation(), so HTTP client spans (httpx instrumentation, as in the agents runtime) don't record its URL. OTel redactssigandSignaturebut not, for example,X-Amz-Signature.Accept-Encoding: identity, so the bytes read are the stored bytes and match the reported size.download:downloadanddownload_asyncnow use the shared read-URL lookup. Their behaviour is unchanged.Async code
There is no
open_async. Reading the file waits on the storage, as reading any file waits on the disk, and httpx readsfiles=uploads synchronously even inAsyncClient. So from async code, runopenand the call that reads the file in a worker thread:With a 100 MiB file from storage that pauses for 300 ms once, that kept the longest event-loop stall at 12 ms. Passing the file to
start_ixp_extraction_asyncstalled it for 301 ms. Making the documents async upload read files off the event loop, local files included, is a follow-up in documents.Testing
TestOpenintests/services/test_buckets_service.py:Retry-Afterkept, and out of httpx instrumentation spans, on success and on an error status (opentelemetry-instrumentation-httpxis a new dev dependency for this test);ETagor size: a rewind downloads the whole file again, a replacement of another length fails, a download from the middle is refused, and a platform upload retries a dropped download, chunked;files=upload with an exactContent-Lengththat resends the whole file on a retry;ETagand by size;206when the first download had none.uipath-platformsuite passes. ruff and mypy are clean on the changed files.RequiresAuth: false):206);BucketFileChangedError;sdk.documents.start_ixp_extraction(file=<opened bucket file>), with the IXP extraction finishing with the expected values;ETagor size (headers removed from the real responses) and the first download dropped halfway: the upload was retried, chunked, from a new download, and the extraction finished.ETag,Content-LengthandAccept-Ranges: bytesare sent.If-Matchis ignored. Read URLs expire after about an hour. Responses are cached per URL for 4 hours (cache-control: public, max-age=14400).Notes
DocumentExtraction(file=...), the extraction starts after the step pauses, and by then awithblock has closed the file. That case should keep the file open, or usefile_path.downloadstreaming to disk with the same reader;downloadanduploadto external storage not checking the response status;downloadtoo;downloadanduploadsending signed URLs with HTTP instrumentation on, asopendid before this change.🤖 Generated with Claude Code