diff --git a/vocode/streaming/synthesizer/play_ht_synthesizer.py b/vocode/streaming/synthesizer/play_ht_synthesizer.py index 80baae42a..f80f2fcd6 100644 --- a/vocode/streaming/synthesizer/play_ht_synthesizer.py +++ b/vocode/streaming/synthesizer/play_ht_synthesizer.py @@ -82,27 +82,21 @@ async def create_speech_uncached( timeout=ClientTimeout(total=15), ) if not response.ok: + response_json = await response.json() + if response_json and "error_message" in response_json: + message = response_json["error_message"] + else: + message = f"Request to Play.HT failed with status code {str(response.status)}" + raise PlayHTV1APIError( f"Play.ht API error status code {response.status}", - (await response.json()).get( - "error_message", - f"Request to Play.HT failed with status code {str(response.status)}", - ), + message, ) if response.status == 429 and attempt < max_backoff_retries - 1: await asyncio.sleep(backoff_retry_delay) backoff_retry_delay *= 2 # Exponentially increase delay continue - if not response.ok: - raise PlayHTV1APIError( - f"Play.ht API error status code {response.status}", - (await response.json()).get( - "error_message", - f"Request to Play.HT failed with status code {str(response.status)}", - ), - ) - if self.experimental_streaming: return SynthesisResult( self.experimental_mp3_streaming_output_generator(