Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

fix(studio): reject ambiguous S3 audio references (#16529)

Max-Reisingerd454907d960b
diff --git a/studio/backend/core/training/s3_dataset.py b/studio/backend/core/training/s3_dataset.py
index 3051e5a7b01..c2de23c3fad 100644
--- a/studio/backend/core/training/s3_dataset.py
+++ b/studio/backend/core/training/s3_dataset.py
@@ -189,13 +189,12 @@ def _key_relative_to_prefix(key: str, prefix: Optional[str]) -> str:
return key.lstrip("/")
-def _download_structured(
- client, bucket: str, prefix: Optional[str], keys: list[str], target_dir: str, cancel_callback
+def _structured_local_paths(
+ prefix: Optional[str], keys: list[str], target_dir: str
) -> dict[str, str]:
- """Download ``keys`` mirroring their prefix-relative layout. Returns key -> local path."""
+ """Map ``keys`` to local paths mirroring their prefix-relative layout."""
local_by_key: dict[str, str] = {}
for key in keys:
- _raise_if_cancelled(cancel_callback)
relative = _key_relative_to_prefix(key, prefix)
parts = [part for part in relative.split("/") if part not in ("", ".")]
# ".." is a legal S3 key segment: refuse rather than write above target_dir.
@@ -204,21 +203,26 @@ def _download_structured(
f"S3 key {key!r} contains '..' or empty path segments and cannot "
"be downloaded into the dataset directory."
)
- local_path = _contained_local_path(target_dir, parts)
- os.makedirs(os.path.dirname(local_path) or target_dir, exist_ok = True)
- _download_one(client, bucket, key, local_path, cancel_callback)
- local_by_key[key] = local_path
+ local_by_key[key] = _contained_local_path(target_dir, parts)
return local_by_key
+def _download_structured(client, bucket: str, local_by_key: dict[str, str], cancel_callback):
+ for key, local_path in local_by_key.items():
+ _raise_if_cancelled(cancel_callback)
+ os.makedirs(os.path.dirname(local_path), exist_ok = True)
+ _download_one(client, bucket, key, local_path, cancel_callback)
+
+
def _rewrite_audio_references(
manifest_local_by_key: dict[str, str],
audio_local_by_key: dict[str, str],
bucket: str,
prefix: Optional[str],
) -> None:
- """Rewrite audio values naming a downloaded key (prefix-relative, ``s3://`` URI, or
- manifest-relative) to its local path; anything else is left as written."""
+ """Rewrite audio values naming an audio key (prefix-relative, ``s3://`` URI, or
+ manifest-relative) to its local path; anything else is left as written. A value whose
+ prefix- and manifest-relative readings name different keys is refused."""
lookup: dict[str, str] = {}
for key, local_path in audio_local_by_key.items():
lookup[_key_relative_to_prefix(key, prefix)] = local_path
@@ -233,10 +237,15 @@ def _rewrite_audio_references(
if not isinstance(value, str) or not value.lower().endswith(_AUDIO_EXTENSIONS):
return None
direct = lookup.get(value)
- if direct is not None:
- return direct
relative_to_manifest = os.path.normpath(os.path.join(manifest_dir, value))
- return lookup.get(relative_to_manifest.replace(os.sep, "/"))
+ relative = lookup.get(relative_to_manifest.replace(os.sep, "/"))
+ if direct is not None and relative is not None and direct != relative:
+ raise ValueError(
+ f"Ambiguous audio reference {value!r} in S3 manifest {manifest_key!r}: "
+ "it matches different prefix-relative and manifest-relative files. "
+ "Use './' for a manifest-relative path or a full 's3://' URI."
+ )
+ return direct if direct is not None else relative
if manifest_path.lower().endswith(".csv"):
_rewrite_csv_manifest(manifest_path, resolve)
@@ -357,14 +366,13 @@ def prepare_s3_dataset_download(
local_files: list[str] = []
if audio_keys:
- manifest_local_by_key = _download_structured(
- client, bucket, prefix, keys, target_dir, cancel_callback
- )
- audio_local_by_key = _download_structured(
- client, bucket, prefix, audio_keys, target_dir, cancel_callback
- )
- local_files = list(manifest_local_by_key.values())
+ manifest_local_by_key = _structured_local_paths(prefix, keys, target_dir)
+ audio_local_by_key = _structured_local_paths(prefix, audio_keys, target_dir)
+ _download_structured(client, bucket, manifest_local_by_key, cancel_callback)
+ # Rewrite before the audio download so a bad manifest fails without fetching it.
_rewrite_audio_references(manifest_local_by_key, audio_local_by_key, bucket, prefix)
+ _download_structured(client, bucket, audio_local_by_key, cancel_callback)
+ local_files = list(manifest_local_by_key.values())
else:
used_paths: set[str] = set()
for key in keys:
diff --git a/studio/backend/tests/test_s3_audio_dataset.py b/studio/backend/tests/test_s3_audio_dataset.py
index 95578829e03..25eb96fb76c 100644
--- a/studio/backend/tests/test_s3_audio_dataset.py
+++ b/studio/backend/tests/test_s3_audio_dataset.py
@@ -197,6 +197,67 @@ def test_manifest_relative_references_resolve_against_the_manifest_dir(monkeypat
assert rows[0]["audio"] == str(tmp_path / "train" / "clips" / "a.wav")
[email protected]("suffix", [".jsonl", ".json", ".csv"])
+ "reference,root_audio,nested_audio,expected",
+ [
+ ("clips/a.wav", True, True, None),
+ ("./clips/a.wav", True, True, "train/clips/a.wav"),
+ ("s3://my-bucket/datasets/train/clips/a.wav", True, True, "train/clips/a.wav"),
+ ("s3://my-bucket/datasets/clips/a.wav", True, True, "clips/a.wav"),
+ ("clips/a.wav", False, True, "train/clips/a.wav"),
+ ("clips/a.wav", True, False, "clips/a.wav"),
+ ("missing.wav", True, True, "missing.wav"),
+ ],
+)
+def test_audio_reference_ambiguity(
+ monkeypatch, tmp_path, suffix, reference, root_audio, nested_audio, expected
+):
+ manifest_key = "datasets/train/rows" + suffix
+ row = {"audio": reference, "text": "transcript for selected clip"}
+ if suffix == ".csv":
+ content = f"audio,text\n{reference},{row['text']}\n"
+ else:
+ content = json.dumps([row] if suffix == ".json" else row) + "\n"
+ keys = [manifest_key]
+ if root_audio:
+ keys.append("datasets/clips/a.wav")
+ if nested_audio:
+ keys.append("datasets/train/clips/a.wav")
+ client = _install(monkeypatch, keys, contents = {manifest_key: content})
+
+ if expected is None:
+ with pytest.raises(ValueError, match = "Ambiguous audio reference") as error:
+ s3_dataset.download_s3_dataset(_cfg(), dest_dir = str(tmp_path))
+ assert reference in str(error.value)
+ assert manifest_key in str(error.value)
+ assert "./" in str(error.value) and "s3://" in str(error.value)
+ assert (tmp_path / "train" / ("rows" + suffix)).read_text() == content
+ assert [key for _, key, _ in client.downloaded] == [manifest_key]
+ return
+
+ files = s3_dataset.download_s3_dataset(_cfg(), dest_dir = str(tmp_path))
+ with open(files[0], encoding = "utf-8") as handle:
+ if suffix == ".csv":
+ result = next(csv.DictReader(handle))
+ else:
+ result = json.load(handle)
+ if suffix == ".json":
+ result = result[0]
+ expected_path = expected if expected == "missing.wav" else str(tmp_path / expected)
+ assert result == {"audio": expected_path, "text": row["text"]}
+
+
+def test_prefix_and_manifest_relative_candidates_can_be_the_same_file(monkeypatch, tmp_path):
+ _install(
+ monkeypatch,
+ ["datasets/metadata.jsonl", "datasets/clips/a.wav"],
+ contents = {"datasets/metadata.jsonl": '{"audio": "clips/a.wav", "text": "hi"}\n'},
+ )
+ files = s3_dataset.download_s3_dataset(_cfg(), dest_dir = str(tmp_path))
+ assert _rows(files[0])[0]["audio"] == str(tmp_path / "clips/a.wav")
+
+
def test_csv_audio_references_are_rewritten_to_local_paths(monkeypatch, tmp_path):
_install(
monkeypatch,