@@ -59,7 +59,7 @@ class AsyncTTSClient:
5959 max_concurrent: Maximum concurrent requests
6060 """
6161
62- def __init__ (
62+ def __init__ ( # type: ignore[no-untyped-def]
6363 self ,
6464 base_url : str = "https://www.openai.fm" ,
6565 api_key : Optional [str ] = None ,
@@ -102,16 +102,16 @@ def __init__(
102102
103103 logger .info (f"Initialized async TTS client with base URL: { self .base_url } " )
104104
105- async def __aenter__ (self ):
105+ async def __aenter__ (self ): # type: ignore[no-untyped-def]
106106 """Async context manager entry."""
107107 await self ._ensure_session ()
108108 return self
109109
110- async def __aexit__ (self , exc_type , exc_val , exc_tb ):
110+ async def __aexit__ (self , exc_type , exc_val , exc_tb ) -> None : # type: ignore[no-untyped-def]
111111 """Async context manager exit."""
112112 await self .close ()
113113
114- async def _ensure_session (self ):
114+ async def _ensure_session (self ) -> None :
115115 """Ensure HTTP session is created."""
116116 if self ._session is None or self ._session .closed :
117117 # Setup headers
@@ -134,7 +134,7 @@ async def _ensure_session(self):
134134 connector = connector
135135 )
136136
137- async def generate_speech (
137+ async def generate_speech ( # type: ignore[no-untyped-def]
138138 self ,
139139 text : str ,
140140 voice : Union [Voice , str ] = Voice .ALLOY ,
@@ -176,7 +176,7 @@ async def generate_speech(
176176
177177 return await self ._make_request (request )
178178
179- async def generate_speech_long_text (
179+ async def generate_speech_long_text ( # type: ignore[no-untyped-def]
180180 self ,
181181 text : str ,
182182 voice : Union [Voice , str ] = Voice .ALLOY ,
@@ -274,7 +274,7 @@ def _resolve_long_text_format(
274274
275275 return response_format
276276
277- async def generate_speech_from_long_text (
277+ async def generate_speech_from_long_text ( # type: ignore[no-untyped-def]
278278 self ,
279279 text : str ,
280280 voice : Union [Voice , str ] = Voice .ALLOY ,
@@ -382,15 +382,18 @@ async def _make_request(self, request: TTSRequest) -> TTSResponse:
382382 url = build_url (self .base_url , "api/generate" )
383383
384384 # Prepare form data for openai.fm API
385+ voice_value = request .voice .value if isinstance (request .voice , Voice ) else str (request .voice )
386+ format_value = (
387+ request .response_format .value
388+ if isinstance (request .response_format , AudioFormat )
389+ else str (request .response_format )
390+ )
391+
385392 form_data = {
386393 'input' : request .input ,
387- 'voice' : request . voice . value ,
394+ 'voice' : voice_value ,
388395 'generation' : str (uuid .uuid4 ()),
389- 'response_format' : (
390- request .response_format .value
391- if hasattr (request .response_format , 'value' )
392- else str (request .response_format )
393- )
396+ 'response_format' : format_value
394397 }
395398
396399 # Add prompt/instructions if provided
@@ -438,14 +441,17 @@ async def _make_request(self, request: TTSRequest) -> TTSResponse:
438441
439442 target_format = get_supported_format (requested_format )
440443 payload ['response_format' ] = target_format .value
441- async with self ._session .post (url , data = payload ) as response :
442- # Handle different response types
443- if response .status == 200 :
444- return await self ._process_openai_fm_response (response , request )
445- else :
446- # Try to parse error response
447- try :
448- error_data = await response .json ()
444+ if self ._session is None :
445+ await self ._ensure_session ()
446+ if self ._session is not None :
447+ async with self ._session .post (url , data = payload ) as response :
448+ # Handle different response types
449+ if response .status == 200 :
450+ return await self ._process_openai_fm_response (response , request )
451+ else :
452+ # Try to parse error response
453+ try :
454+ error_data = await response .json ()
449455 except (json .JSONDecodeError , ValueError ):
450456 text = await response .text ()
451457 error_data = {"error" : {"message" : text or "Unknown error" }}
@@ -569,30 +575,31 @@ async def _process_openai_fm_response(
569575 "status_code" : response .status ,
570576 "url" : str (response .url ),
571577 "service" : "openai.fm" ,
572- "voice" : request . voice . value ,
578+ "voice" : voice_value ,
573579 "original_text" : (
574580 request .input [:100 ] + "..."
575581 if len (request .input ) > 100
576582 else request .input
577583 ),
578- "requested_format" : requested_format .value ,
584+ "requested_format" : requested_format .value if isinstance ( requested_format , AudioFormat ) else str ( requested_format ) ,
579585 "effective_requested_format" : get_supported_format (
580586 requested_format
581- ).value ,
582- "actual_format" : actual_format .value
587+ ).value if isinstance ( get_supported_format ( requested_format ), AudioFormat ) else str ( get_supported_format ( requested_format )) ,
588+ "actual_format" : actual_format .value if isinstance ( actual_format , AudioFormat ) else str ( actual_format )
583589 }
584590 )
585591
592+ actual_format_str = actual_format .value if isinstance (actual_format , AudioFormat ) else str (actual_format )
586593 logger .info (
587594 "Successfully generated %s of %s audio from openai.fm using voice %s" ,
588595 format_file_size (len (audio_data )),
589- actual_format . value .upper (),
590- request . voice . value ,
596+ actual_format_str .upper (),
597+ voice_value ,
591598 )
592599
593600 return tts_response
594601
595- async def close (self ):
602+ async def close (self ) -> None :
596603 """Close the HTTP session."""
597604 if self ._session and not self ._session .closed :
598605 await self ._session .close ()
0 commit comments