Search code examples
pythonopenai-api

AttributeError: 'HttpxBinaryResponseContent' object has no attribute 'with_streaming_response'


I am using openai to convert text into audio , how ever this error always pops up : AttributeError: 'HttpxBinaryResponseContent' object has no attribute 'with_streaming_response' I have tried the stream_to_field() function but it neither works

def audio(prompt):

speech_file_path=Path('C:\\Users\\beni7\\Downloads\\proyectos_programacion\\proyecto_shorts\\audio').parent / "speech.mp3"
  response = openai.audio.speech.create(
  model="tts-1",
  voice="alloy",
  input=prompt
 )
 response.with_streaming_response.method(speech_file_path)

Solution

  • .with_streaming_response is a method on speech. You can try the following:

    def audio(prompt):
        speech_file_path="<file path>"
        with client.audio.speech.with_streaming_response.create(
            model="tts-1",
            voice="alloy",
            input=prompt,
        ) as response:
            response.stream_to_file(speech_file_path)