Skip to content

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
huggingface:mainfrom
theguysudo:fix/fal-wavespeed-input-types
Open

theguysudo wants to merge 2 commits into
huggingface:mainfrom
theguysudo:fix/fal-wavespeed-input-types

Conversation

@theguysudo

@theguysudo theguysudo commented Nov 22, 2026 •

Copy link
Copy Markdown

Summary

InferenceClient documents that ASR audio accepts str | Path | bytes | BinaryIO and that image tasks accept any ContentT (bytes, file object, path, URL, PIL image). Two provider helpers did not honor that contract:

  • FalAIAutomaticSpeechRecognitionTask (inference/_providers/fal_ai.py)
  • WavespeedAIImageToImageTask and WavespeedAIImageToVideoTask (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 into base64.b64encode, which raised TypeError: 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 InferenceClient documents (Path, opened file, PIL Image) were silently unsupported.

Fix

Replace the hand-written base64 encoding with the existing _as_url helper from inference/_common.py, which already handles every input type (URL, data: URL, bytes, bytearray/memoryview, binary file object, str/Path file 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).

  • fal-ai ASR: 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-coding audio/mpeg.
  • WaveSpeed image-to-image (and image-to-video through inheritance): image = _as_url(inputs, default_mime_type="image/jpeg") — removes the now-unused import 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: added Path and 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] @ ."

$ pytest tests/test_inference_providers.py -k "fal or wavespeed" -q
38 passed

$ pytest tests/test_inference_providers.py -q
179 passed

$ pytest tests/test_inference_client.py -k "as_url or open_as_mime or b64_encode" -q
14 passed

$ ruff check <changed files> && ruff format --check <changed files>
All checks passed! / 3 files already formatted

The issue's own offline reproduction (client-level, HTTP mocked) also confirms the behavior:

  • Before this patch: TypeError: a bytes-like object is required, not 'PosixPath' / '_io.BufferedReader' / 'PngImageFile' on fal-ai ASR with Path/file input and WaveSpeed image-to-image with Path/file/PIL input.
  • After this patch: all 7 input combinations (bytes, Path, opened file, PIL image) build the expected data: 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 with TypeError before a request is sent.

Both providers replace duplicated base64/URL branching with the shared _as_url helper (same pattern as other fal-ai image tasks). Fal ASR uses default_mime_type="audio/mpeg" with MIME inferred from filenames when possible; WaveSpeed image tasks use default_mime_type="image/jpeg" and drop the local base64 usage.

Tests in test_inference_providers.py cover 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.

theguysudo and others added 2 commits November 22, 2026 20:58
…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

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.80%. Comparing base (1daa48b) to head (b649173).
⚠️ Report is 894 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bot-ci-comment

Copy link
Copy Markdown

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fal-ai ASR and WaveSpeed image tasks fail on Path, file object and PIL inputs

2 participants