microsoft/semantic-kernel · error · ServiceResponseException
{type(self)} service failed to generate audio
Error message
{type(self)} service failed to generate audio What it means
Raised as ServiceResponseException in _send_text_to_audio_request when any exception occurs during client.audio.speech.create. This is the text-to-speech (TTS) path; failures include invalid model/voice parameters, text exceeding limits, content-filter rejections, or network/quota errors.
Source
Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_handler.py:190
except Exception as ex:
raise ServiceResponseException(
f"{type(self)} service failed to transcribe audio",
ex,
) from ex
async def _send_text_to_audio_request(
self, settings: OpenAITextToAudioExecutionSettings
) -> _legacy_response.HttpxBinaryResponseContent:
"""Send a request to the OpenAI text to audio endpoint.
The OpenAI API returns the content of the generated audio file.
"""
try:
return await self.client.audio.speech.create(
**settings.prepare_settings_dict(),
)
except Exception as ex:
raise ServiceResponseException(
f"{type(self)} service failed to generate audio",
ex,
) from ex
def _handle_structured_output(
self, request_settings: OpenAIChatPromptExecutionSettings, settings: dict[str, Any]
) -> None:
response_format = getattr(request_settings, "response_format", None)
if getattr(request_settings, "structured_json_response", False) and response_format:
# Case 1: response_format is a type and subclass of BaseModel
if isinstance(response_format, type) and issubclass(response_format, BaseModel):
settings["response_format"] = type_to_response_format_param(response_format)
# Case 2: response_format is a type but not a subclass of BaseModel
elif isinstance(response_format, type):
generated_schema = KernelJsonSchemaBuilder.build(parameter_type=response_format, structured_output=True)
assert generated_schema is not None # nosec
settings["response_format"] = generate_structured_output_response_format_schema(
name=response_format.__name__, schema=generated_schemaView on GitHub (pinned to c028a0c7dc)
Solutions
- Inspect ex.__cause__ for the specific API error
- Verify the model id is 'tts-1' or 'tts-1-hd' and the voice is one of the supported names
- Ensure input text is within the 4096-character limit
- For content-filter rejections, sanitize or shorten the input text
Example fix
# before settings = OpenAITextToAudioExecutionSettings(ai_model_id='tts-1', voice='custom_voice') # after — use a supported voice settings = OpenAITextToAudioExecutionSettings(ai_model_id='tts-1', voice='alloy')
Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED_VOICES = {'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'}
SUPPORTED_TTS_MODELS = {'tts-1', 'tts-1-hd'}
if settings.ai_model_id not in SUPPORTED_TTS_MODELS:
raise ValueError(f'Unsupported TTS model: {settings.ai_model_id}')
if settings.voice and settings.voice not in SUPPORTED_VOICES:
raise ValueError(f'Unsupported voice: {settings.voice}')
if len(settings.input_text) > 4096:
raise ValueError('TTS input exceeds 4096 character limit') Try / catch
from semantic_kernel.exceptions import ServiceResponseException
try:
audio = await service._send_text_to_audio_request(settings)
except ServiceResponseException as e:
logger.error('TTS generation failed: %s', e)
raise Prevention
- Validate voice, model, and text length against the OpenAI TTS spec before calling
- Cache generated audio to reduce API calls for repeated phrases
When it happens
Trigger: Calling text-to-speech with unsupported voice names, invalid model id (e.g., not 'tts-1' or 'tts-1-hd'), text that triggers content filtering, response_format not supported, or network/rate-limit failures during client.audio.speech.create.
Common situations: Using a voice name not in OpenAI's supported set (alloy, echo, fable, onyx, nova, shimmer); very long input text exceeding the 4096-character limit; content filter on the TTS input; rate limit from high-frequency speech generation.
Related errors
- The voice '{voice}' is not supported.
- The format '{format}' is not supported.
- {type(self)} service failed to generate embeddings
- Failed to generate image: {ex}
- Failed to edit image: {ex}
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/ca659a6523ea43c7.
Report an issue: GitHub.