fix(multimodal): raise explicit errors in Audio instead of assert#2361
Closed
IgnazioDS wants to merge 1 commit into
Closed
fix(multimodal): raise explicit errors in Audio instead of assert#2361IgnazioDS wants to merge 1 commit into
IgnazioDS wants to merge 1 commit into
Conversation
`Audio.from_url` and `Audio.from_path` validated inputs with bare `assert` statements. Asserts are silently stripped under `python -O` (PEP 230), so the checks vanish in optimized deployments, and when they do fire they raise `AssertionError` rather than the `ValueError` used everywhere else. This left `Audio` inconsistent with `Image` and `PDF`, which already raise `FileNotFoundError` / `ValueError` and reject empty files. Replace the asserts with explicit `FileNotFoundError` (missing file) and `ValueError` (empty file, unsupported format), mirroring the `Image`/`PDF` pattern. Add regression tests covering all four cases.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Collaborator
|
Consolidated this Audio validation fix into draft PR #2400. Closing this PR to keep the queue focused on the consolidated patch; the new PR carries the explicit |
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.
What & why
Audio.from_urlandAudio.from_pathvalidate inputs with bareassertstatements:Two problems:
assertis stripped underpython -O(PEP 230), so these validation checks silently vanish in optimized deployments — invalid audio then flows downstream instead of being rejected at the boundary.AssertionErrorrather than theValueErrorcallers expect.This also leaves
Audioinconsistent with its sibling classes:Image.from_pathandPDF.from_pathalready raiseFileNotFoundErrorfor a missing file,ValueError("... file is empty")for an empty file, andValueError("Unsupported ... format: ...")for an unsupported type.Change
Replace the asserts in
Audiowith the same explicit errorsImage/PDFuse:FileNotFoundErrorValueError("Audio file is empty")(new check, matchingImage/PDF)ValueError("Unsupported audio format: ...")No behavior change for valid inputs.
Tests
Adds 4 regression tests in
tests/v2/test_core_multimodal_runtime.py— each fails onmainand passes here:from_urlwith an unsupported content-type →ValueErrorfrom_pathwith an unsupported format →ValueErrorfrom_pathwith an empty file →ValueErrorfrom_pathwith a missing file →FileNotFoundErrorVerification:
pytest tests/v2/test_core_multimodal_runtime.py→ 18 passed;tests/multimodal/test_multimodal.py(non-LLM) → 57 passed;ruff check/ruff format --check/ty checkclean.CHANGELOG.mdupdated under[Unreleased].