Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assemblyai/_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def _resolve_audio(
elif hasattr(data, "read"):
audio = data.read()
name = getattr(data, "name", None)
if name:
# `name` is a file descriptor (an int) for anonymous files such as
# `tempfile.TemporaryFile()`, so only a str is a usable path.
if isinstance(name, str) and name:
filename = os.path.basename(name)
suffix = os.path.splitext(name)[1].lower()
else:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import tempfile

import httpx
import pytest
from pytest_httpx import HTTPXMock
Expand Down Expand Up @@ -316,6 +318,24 @@ def test_transcribe_path_input(httpx_mock: HTTPXMock, tmp_path):
assert b'filename="call.wav"' in body


def test_transcribe_file_object_without_a_path_name(httpx_mock: HTTPXMock):
# Given an anonymous temporary file, whose `name` is a file descriptor
# rather than a path
_mock_ok(httpx_mock)
with tempfile.TemporaryFile() as audio_file:
audio_file.write(b"RIFFfake-wav-bytes")
audio_file.seek(0)

# When transcribing the file object
result = aai.SyncTranscriber().transcribe(audio_file)

# Then it succeeds and ships the audio under the default name
assert result.text == "hello world"
body = httpx_mock.get_requests()[0].read()
assert b'filename="audio.wav"' in body
assert b"RIFFfake-wav-bytes" in body


def test_keyterms_prompt_too_long_raises():
# Given a keyterms_prompt exceeding the character cap
# When building the config, Then validation fails immediately
Expand Down