@@ -78,7 +78,7 @@ def parse_stream_helper(line: bytes) -> str | None:
7878 line = line [len (b"data: " ) :]
7979 else :
8080 line = line [len (b"data:" ) :]
81- if line .strip () == b"[DONE]" :
81+ if line .strip (). upper () == b"[DONE]" :
8282 # return here will cause GeneratorExit exception in urllib3
8383 # and it will close http connection with TCP Reset
8484 return None
@@ -620,17 +620,22 @@ def _interpret_response(
620620 self , result : requests .Response , stream : bool
621621 ) -> Tuple [TogetherResponse | Iterator [TogetherResponse ], bool ]:
622622 """Returns the response(s) and a bool indicating whether it is a stream."""
623- if stream and "text/event-stream" in result .headers .get ("Content-Type" , "" ):
623+ content_type = result .headers .get ("Content-Type" , "" )
624+ if stream and "text/event-stream" in content_type :
624625 return (
625626 self ._interpret_response_line (
626627 line , result .status_code , result .headers , stream = True
627628 )
628629 for line in parse_stream (result .iter_lines ())
629630 ), True
630631 else :
632+ if content_type in ["application/octet-stream" , "audio/wav" , "audio/mpeg" ]:
633+ content = result .content
634+ else :
635+ content = result .content .decode ("utf-8" )
631636 return (
632637 self ._interpret_response_line (
633- result . content . decode ( "utf-8" ) ,
638+ content ,
634639 result .status_code ,
635640 result .headers ,
636641 stream = False ,
@@ -670,7 +675,7 @@ async def _interpret_async_response(
670675 )
671676
672677 def _interpret_response_line (
673- self , rbody : str , rcode : int , rheaders : Any , stream : bool
678+ self , rbody : str | bytes , rcode : int , rheaders : Any , stream : bool
674679 ) -> TogetherResponse :
675680 # HTTP 204 response code does not have any content in the body.
676681 if rcode == 204 :
@@ -684,8 +689,11 @@ def _interpret_response_line(
684689 )
685690
686691 try :
687- if "text/plain" in rheaders .get ("Content-Type" , "" ):
692+ content_type = rheaders .get ("Content-Type" , "" )
693+ if "text/plain" in content_type :
688694 data : Dict [str , Any ] = {"message" : rbody }
695+ elif content_type in ["application/octet-stream" , "audio/wav" , "audio/mpeg" ]:
696+ data = rbody
689697 else :
690698 data = json .loads (rbody )
691699 except (JSONDecodeError , UnicodeDecodeError ) as e :
0 commit comments