Skip to content

fix(sync): accept file objects that have no path for a name - #268

Open
RaphaelFakhri wants to merge 1 commit into
AssemblyAI:masterfrom
RaphaelFakhri:fix/audio-file-object-int-name
Open

RaphaelFakhri wants to merge 1 commit into
AssemblyAI:masterfrom
RaphaelFakhri:fix/audio-file-object-int-name

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Summary

SyncTranscriber.transcribe() and AsyncSyncTranscriber.transcribe() read the audio part's filename from the file object's name attribute and pass it to os.path.basename(). Anonymous files, such as the result of tempfile.TemporaryFile() on POSIX, report a file descriptor (an int) as their name, so the call raises TypeError: expected str, bytes or os.PathLike object, not int. The streaming path, stream_filename() and the Dictation resolve_source() already use the name only when it is a str.

Changes

  • assemblyai/_audio.py: use the file object's name only when it is a non-empty str. Otherwise the audio is sent under the default filename and Content-Type.
  • tests/unit/test_sync.py: add a test that transcribes a tempfile.TemporaryFile() and checks the request body.

Testing

python -m pytest tests/unit/test_sync.py tests/unit/test_sync_async.py tests/unit/test_sync_live.py tests/unit/test_dictation.py tests/unit/test_audio_helpers.py tests/unit/test_sync_backwards_compat.py

Without the source change the new test fails with the TypeError. With the change, all 186 tests in these files pass.

SyncTranscriber.transcribe() read the audio part's filename from the
file object's name attribute and passed it to os.path.basename().
Anonymous files such as tempfile.TemporaryFile() report a file
descriptor (an int) as their name, so the call raised TypeError. Use
the name only when it is a str, as the streaming path already does.
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.

1 participant