REFACTOR: Shared image fetch helper for multimodal seed datasets#1776
Merged
romanlutz merged 2 commits intoMay 22, 2026
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Follow-up to @jsong468's review comment on #1757: #1757 (comment) (
out of scope for this PR).Four multimodal seed-dataset loaders under
pyrit/datasets/seed_datasets/remote/duplicated the samedownload an image from a URL, cache it under seed-prompt-entries, return the local pathlogic. This PR extracts that logic into a single shared async helper.What changed
Added
pyrit/datasets/seed_datasets/remote/_image_cache.pywith:filenameso each loader keeps its existing on-disk cache filename pattern (no user-visible cache invalidation).image_bytespath supports loaders that have raw bytes in hand (e.g. PIL images from HuggingFace rows) without going through the network. This makes the helper a drop-in fit for the MSTS loader once FEAT: Add MSTS multimodal safety dataset loader #1757 lands.request_headers/request_timeout/follow_redirectscover VLSU's custom UA + 2 s timeout + redirect needs without leaking a generic**httpx_kwargsthrough the public API.str(Path(results_path) / sub_dir / filename)for cross-OS-stable cache paths so theskip download if file existsfast-path fires correctly on Windows.try / except Exception -> log + skipconvention used at every call site.RuntimeErrormessage (Serializer memory is not properly configured) so existingpytest.raises(match=...)assertions keep passing.Why a standalone module (rather than a base-class method or mixin)
_RemoteDatasetLoaderwould bloat the base._helpers.py/_common.pyinremote/, so the new module fits the prevailing one-helper-per-file pattern.Migrated loaders
_HarmBenchMultimodalDataset_fetch_and_save_image_asyncharmbench_{behavior_id}.png_VisualLeakBenchDataset_fetch_and_save_image_asyncvisual_leak_bench_{example_id}.png_VLSUMultimodalDataset_fetch_and_save_image_asyncml_vlsu_{group_id}.png(custom headers/timeout/redirects preserved)_ComicJailbreakDataset_fetch_template_asynccomic_jailbreak_{template_name}.png(template-name validation kept in the wrapper)Each loader's existing wrapper method survives as a 1-3 line delegating call, so every existing
patch.object(loader, "_fetch_and_save_image_async", ...)test mock keeps working untouched._VLGuardDatasetis not migrated - it downloads a zip from HuggingFace Hub instead of an individual image URL, so the pattern doesn't apply.Tests
tests/unit/datasets/test_image_cache.py(8 tests) directly exercising the helper: cache hit skips network, URL fetch path writes response bytes,image_bytespath skips network entirely,ValueErroron missing input,RuntimeErroron misconfigured memory, exception propagation, custom headers/timeout/redirects forwarded to the HTTP client, and graceful fallback when the cache-existence check itself fails.<loader_module>.data_serializer_factoryto_image_cache.data_serializer_factory(the loaders no longer import the factory directly).expected_pathassertions switched tostr(Path(...))for Windows/POSIX portability.All 439 unit tests under
tests/unit/datasetspass.Verification