method in python 'Stream_to_file' not working
Asked Answered
H

4

8

Working with OpenAI API and below is my simple code, but getting deprecation warning for 'steam_to_file' method.

Code -

from openai import OpenAI
from pathlib import Path

client = OpenAI(
  api_key=os.getenv("OPENAI_API_KEY"),
 )

speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
  model="tts-1",
  voice="alloy",
  input= '''I see skies of blue and clouds of white
            The bright blessed days, the dark sacred nights
            And I think to myself
            What a wonderful world
         '''
)
response.stream_to_file(speech_file_path)

IDE - Visual Studio code

Warning as below -

** DeprecationWarning: Due to a bug, this method doesn't actually stream the response content, .with_streaming_response.method() should be used instead response.stream_to_file("song.mp3")**

Can anyone please help?

I tried to check through different forums but could not find error related to stream_to_file.

I am using Python 3.12

Hypocrite answered 7/2 at 5:47 Comment(0)
K
5

This seems to be the recommended approach, at least it uses the methods that the error message suggests:

from openai import OpenAI

client = OpenAI()

with client.audio.speech.with_streaming_response.create(
    model="tts-1",
    voice="alloy",
    input="""I see skies of blue and clouds of white
             The bright blessed days, the dark sacred nights
             And I think to myself
             What a wonderful world""",
) as response:
    response.stream_to_file("speech.mp3")

However, it seems like this still doesn't actually stream the response. If I repeatedly check the "last changed" time on the speech.mp3 file, I can see that it is created immediately, but it isn't changed again until the entire generation completes.

I'm still trying to figure out how to do proper streaming of TTS, but at least this is a way to use the API without a DeprecationError.

Using openai 1.12.0

Kamasutra answered 22/2 at 21:21 Comment(0)
N
2

The easy way is just to ignore this warning. Import the warnings library, then warnings.filterwarnings() It worked for me.

from openai import OpenAI
from pathlib import Path
import warnings

# Ignore DeprecationWarning
warnings.filterwarnings("ignore", category=DeprecationWarning)
  
client = OpenAI(
  api_key=os.getenv("OPENAI_API_KEY"),
)

speech_file_path = Path(__file__).parent / "speech.mp3"

response = client.audio.speech.create(
  model="tts-1",
  voice="alloy",
  input= '''I see skies of blue and clouds of white
            The bright blessed days, the dark sacred nights
            And I think to myself
            What a wonderful world
         '''
)

response.stream_to_file(speech_file_path)
Nucleolus answered 6/3 at 20:5 Comment(0)
J
0

Agree, it's irrespective of if you are using Path or not. It creates the file. But with this tweak proposed, it worked without errors.

with client.audio.speech.with_streaming_response.create(
    model="tts-1",
    voice="alloy",
    input=file.read()
    ) as  response:
        response.stream_to_file(created_audio_file_path)
Jolty answered 4/6 at 6:54 Comment(0)
K
-3

I had the same issue but this fixed it. Delete line
speech_file_path = Path(__file__).parent / "speech.mp3" and write file_name = "speech.mp3".

response = openai.audio.speech.create(
  model="tts-1",
  voice=shimmer,
  input="The quick brown fox jumped over the lazy dog."
)

file_name = "speech.mp3"
response.stream_to_file(file_name)
Kleinstein answered 18/2 at 4:49 Comment(5)
This is absolutely NOT "the same issue".Tunicle
uh yeah it is... I had the same depreciation warning and searching it is what led me to their post in the first place. I'm simply showing what worked. @TimRobertsKleinstein
Nope. You changed something else, like maybe upgrading openai_python. This change would have absolutely no effect on that error. Every call to stream_to_file would get it.Tunicle
The only change I made was the one I originally commented. Why don’t you just try it yourself? @TimRoberts.Kleinstein
Using Path or not does not have any effect on the stream_to_file method.Ultan

© 2022 - 2024 — McMap. All rights reserved.