# OpenAI Whisper- Send Bytes (python) instead of filename

**URL:** <https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786>\
**Category:** API\
**Tags:** whisper\
**Created:** [March 5, 2023, 3:28pm UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786 "2023-03-05T15:28:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/virajvaitha1995/32/33290_2.png) [@virajvaitha1995](https://community.openai.com/u/virajvaitha1995)\
**Post date:** [March 5, 2023, 3:28pm UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786/1 "2023-03-05T15:28:24Z")

</div>

Hi,

I hope you’re well. Really enjoying using the OpenAI api, recently had some challenges and was looking for some help.

I don’t want to save audio to disk and delete it with a background task.

My FastAPI application uses a an UploadFile (meaning users upload the file, and I then have access a SpooledTemporaryFile).

Previously using the free version of Whisper on Github, I was able to send the bytes to the model, whereas this API isn’t working this way.

Can anyone else advise on how they are transcribing audio in python without saving videos/audio to disk?

Thanks,

---

<div class="post-metadata">

**Author:** ![areebaseher04](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/areebaseher04/32/41025_2.png) [@areebaseher04](https://community.openai.com/u/areebaseher04)\
**Post date:** [March 8, 2023, 4:22pm UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786/2 "2023-03-08T16:22:03Z")

</div>

Hi, I am using the openai-whisper from github in my django app and I am sending bytes just like you but it is not working.

Can you please share your code?

---

<div class="post-metadata">

**Author:** ![tudor.chifor](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/tudor.chifor/32/34731_2.png) [@tudor.chifor](https://community.openai.com/u/tudor.chifor)\
**Post date:** [May 17, 2023, 3:38pm UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786/3 "2023-05-17T15:38:51Z")

</div>

Any updates on this issue ? [quote=“virajvaitha1995, post:1, topic:84786, full:true”]  
Hi,

I hope you’re well. Really enjoying using the OpenAI api, recently had some challenges and was looking for some help.

I don’t want to save audio to disk and delete it with a background task.

My FastAPI application uses a an UploadFile (meaning users upload the file, and I then have access a SpooledTemporaryFile).

Previously using the free version of Whisper on Github, I was able to send the bytes to the model, whereas this API isn’t working this way.

Can anyone else advise on how they are transcribing audio in python without saving videos/audio to disk?

Thanks,  
[/quote]

---

<div class="post-metadata">

**Author:** ![jayseaeff](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jayseaeff/32/161717_2.png) [@jayseaeff](https://community.openai.com/u/jayseaeff)\
**Post date:** [June 27, 2023, 2:40am UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786/4 "2023-06-27T02:40:20Z")

</div>

Hi All,

I also had this problem and managed to find a solution. I was using `pydub` to load and edit audio segments and then wanted to send a pydub audio segment directly to whisper without having to create a temporary file. The following approach worked: basically create BytesIO buffer, encode the audio into it in a supported format and then pass it to whisper:

```auto
import openai
from pydub import AudioSegment

fname = "file.mp3"
audio = AudioSegment.from_file(fname, format="mp3")
# only use first 5sec
audio = audio[:5000]

buffer = io.BytesIO()
# you need to set the name with the extension
buffer.name = fname
audio.export(buffer, format="mp3")

transcript = openai.Audio.transcribe("whisper-1", buffer)

```

---

<div class="post-metadata">

**Author:** ![axiomofjoy](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/axiomofjoy/32/147552_2.png) [@axiomofjoy](https://community.openai.com/u/axiomofjoy)\
**Post date:** [February 15, 2024, 6:52am UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786/5 "2024-02-15T06:52:25Z")

</div>

The solution from @jayseaeff worked for me, even without using `pydub`. The important part is to set the `.name` attribute on the buffer object. It appears that the Whisper API is inferring the file type from the extension on this attribute, rather than inspecting the raw bytes themselves. Here’s a snippet that worked for me (I’m using GraphQL with multipart file uploads).

```auto
@strawberry.type
class Mutation:
    @strawberry.mutation
    async def transcribe(self, audio_file: Upload) -> str:
        audio_data = await audio_file.read()
        buffer = io.BytesIO(audio_data)
        buffer.name = "file.mp3" # this is the important line
        transcription = await openai_client.audio.transcriptions.create(
            model="whisper-1",
            file=buffer,
        )
        return transcription.text

```

Looking at the types in the Python SDK, it looks as though as you can pass a bytes object to the `file` argument, but I haven’t gotten this to work.

---

<div class="post-metadata">

**Author:** ![jrapp](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jrapp/32/147383_2.png) [@jrapp](https://community.openai.com/u/jrapp)\
**Post date:** [February 20, 2024, 4:04pm UTC](https://community.openai.com/t/openai-whisper-send-bytes-python-instead-of-filename/84786/6 "2024-02-20T16:04:38Z")

</div>

Yeah in order to send bytes of a file you need to send more than just the buffer to the file parameter. You can see what types the `file` property accepts by stepping into the documentation with VS code or Intellij. Here’s an example of what is working for me:

```auto
return whisper_api_client.audio.transcriptions.create(
        model="whisper-1",
        file=("temp." + file_type, file_bytes, content_type),
    ).text

```

where `file_type="m4a"` and `content_type="audio/m4a"`
