Repository navigation
fix(inference): support Path, file object and PIL inputs in fal-ai ASR and WaveSpeed image tasks - #5420
Open
theguysudo wants to merge 2 commits into
Open
fix(inference): support Path, file object and PIL inputs in fal-ai ASR and WaveSpeed image tasks#5420theguysudo wants to merge 2 commits into
theguysudo wants to merge 2 commits into
Conversation
…R and WaveSpeed image tasks `FalAIAutomaticSpeechRecognitionTask` and `WavespeedAIImageToImageTask` (inherited by `WavespeedAIImageToVideoTask`) built data URLs by hand and only handled URL strings, plain str paths and raw bytes. A `Path`, an opened file object or a PIL Image went straight into `base64.b64encode`, raising `TypeError: a bytes-like object is required, not 'PosixPath'/'_io.BufferedReader'/'PngImageFile'` before any request was sent. Replace the hand-written encoding with the shared `_as_url` helper from `inference/_common.py` (already used by fal-ai image-to-image, Replicate ASR, Together, ...), which handles every input type documented by `InferenceClient` and guesses the MIME type from the file name. Fixes huggingface#5419
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5420 +/- ##
==========================================
+ Coverage 75.00% 77.80% +2.80%
==========================================
Files 145 178 +33
Lines 13978 25504 +11526
==========================================
+ Hits 10484 19844 +9360
- Misses 3494 5660 +2166 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This branch has not been deployed
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
InferenceClientdocuments that ASRaudioacceptsstr | Path | bytes | BinaryIOand that image tasks accept anyContentT(bytes, file object, path, URL, PIL image). Two provider helpers did not honor that contract:FalAIAutomaticSpeechRecognitionTask(inference/_providers/fal_ai.py)WavespeedAIImageToImageTaskandWavespeedAIImageToVideoTask(inherits from it) (inference/_providers/wavespeed.py)They built the data URL by hand and only handled a URL string, a path given as
str, or raw bytes. Anything else went straight intobase64.b64encode, which raisedTypeError: a bytes-like object is required, not 'PosixPath'(or'_io.BufferedReader','PngImageFile') before any request was sent. See #5419.Root cause
Both helpers duplicated URL/path/bytes handling locally instead of using the shared encoding helper, so the input types
InferenceClientdocuments (Path, opened file, PIL Image) were silently unsupported.Fix
Replace the hand-written base64 encoding with the existing
_as_urlhelper frominference/_common.py, which already handles every input type (URL,data:URL, bytes, bytearray/memoryview, binary file object,str/Pathfile path, PIL Image) and guesses the MIME type from the file name. This is the same pattern the sibling providers already use (fal-ai image-to-image/image-to-video/image-segmentation, Replicate ASR, Together).audio_url = _as_url(inputs, default_mime_type="audio/mpeg")— also preserves URL passthrough and now guesses the mime type from the file name instead of always hard-codingaudio/mpeg.image = _as_url(inputs, default_mime_type="image/jpeg")— removes the now-unusedimport base64.Net change in
src/: -19 lines.Tests
Extended
tests/test_inference_providers.py(all offline, mocked HTTP, same style as the existing provider tests):TestFalAIProvider::test_automatic_speech_recognition_payload: addedPathand opened-file input cases.TestWavespeedAIProvider::test_prepare_image_to_image_payload_from_path_and_file_object(new)TestWavespeedAIProvider::test_prepare_image_to_image_payload_from_pil_image(new)TestWavespeedAIProvider::test_prepare_image_to_video_payload_from_path(new, covers the inherited behavior)Verification
Environment: Python 3.12,
uv venv+uv pip install "huggingface_hub[dev] @ ."The issue's own offline reproduction (client-level, HTTP mocked) also confirms the behavior:
TypeError: a bytes-like object is required, not 'PosixPath'/'_io.BufferedReader'/'PngImageFile'on fal-ai ASR withPath/file input and WaveSpeed image-to-image withPath/file/PIL input.Path, opened file, PIL image) build the expecteddata:URL payload and complete the mocked request.Fixes #5419
Transparency note: developed with AI assistance (an AI coding agent driven by the repo owner), with local reproduction and tests run before submission.
Note
Low Risk
Localized inference payload encoding change with expanded test coverage; no auth or data-model changes.
Overview
Fixes fal-ai ASR and WaveSpeed image-to-image / image-to-video so they accept the same input types documented for
InferenceClient(Path, opened files, PIL images, URLs, bytes), instead of failing withTypeErrorbefore a request is sent.Both providers replace duplicated base64/URL branching with the shared
_as_urlhelper (same pattern as other fal-ai image tasks). Fal ASR usesdefault_mime_type="audio/mpeg"with MIME inferred from filenames when possible; WaveSpeed image tasks usedefault_mime_type="image/jpeg"and drop the localbase64usage.Tests in
test_inference_providers.pycover Path, file-handle, and PIL inputs for these payloads.Reviewed by Cursor Bugbot for commit b649173. Bugbot is set up for automated code reviews on this repo. Configure here.