Text-to-Speech API Error - Empty Input String Issue

I’m building an application that processes data and converts it to speech audio files using OpenAI’s TTS API. However, I keep getting a BadRequestError saying the input string is too short.

Here’s my voice generator module:

import openai

def process_content(text_data):
    results = []
    segments = text_data.split("\n")
    for segment in segments:
        if segment.startswith('Scene: '):
            scene_desc = segment.replace("Scene:", "Scene: ")
            results.append({
                "category": "visual",
                "info": scene_desc
            })
        elif segment.startswith("Voice:"):
            speech_text = segment.replace("Voice:", "Voice:")
            results.append({
                "category": "audio",
                "text": speech_text
            })
    return results

def generate_audio(content_list, file_path="final_audio.mp3"):
    speech_content = ""
    for item in content_list:
        if item["category"] != 'audio':
            continue
        speech_content += item["text"] + "\n\n"
    
    voice_output = openai.audio.speech.create(
        model="tts-1",
        input=speech_content,
        voice="nova"
    )
    voice_output.stream_to_file(file_path)

And my main script:

with open("input_data.txt", "r") as file:
    raw_data = file.read()

ai_response = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "system",
            "content": """Generate content for a short presentation..."""
        },
        {
            "role": "user", 
            "content": f"Process this data: {raw_data}"
        }
    ]
)

processed_data = voice_gen.process_content(ai_response.choices[0].message.content)
voice_gen.generate_audio(processed_data, "presentation.mp3")

The error shows that the input parameter is empty when calling the speech API. I think my text parsing logic might be filtering out all the content, leaving nothing for the TTS function. Has anyone encountered this before?

Had the same issue with OpenAI’s TTS API last month. Your text extraction logic is the problem - you’re replacing “Voice:” with “Voice:” which doesn’t actually remove anything. Try speech_text = segment.replace("Voice:", "").strip() to strip out the prefix and get clean text. I’d add a quick validation like if not speech_content.strip(): before hitting the API to catch empty inputs. Also heads up - GPT responses don’t always match your expected format exactly, so maybe use partial string matching instead of looking for exact prefixes.

your parsing logic’s not quite right - in segment.replace("Voice:", "Voice:"), you’re not removing anything. try changing it to speech_text = segment.replace("Voice:", "").strip(). also, add some debug prints to see if speech_content is empty before calling the API.

yeah, that’s your problem - you’re replacing “Voice:” with “Voice:” so nothing changes. also, check if speech_content is empty before hitting openAI. try adding print(repr(speech_content)) to see what you’re actually getting.