@@ -1113,7 +1113,14 @@ def _process_response(
11131113 ) -> ResponseT :
11141114 origin = get_origin (cast_to ) or cast_to
11151115
1116- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1116+ if (
1117+ inspect .isclass (origin )
1118+ and issubclass (origin , BaseAPIResponse )
1119+ # we only want to actually return the custom BaseAPIResponse class if we're
1120+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1121+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1122+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1123+ ):
11171124 if not issubclass (origin , APIResponse ):
11181125 raise TypeError (f"API Response types must subclass { APIResponse } ; Received { origin } " )
11191126
@@ -1618,7 +1625,14 @@ async def _process_response(
16181625 ) -> ResponseT :
16191626 origin = get_origin (cast_to ) or cast_to
16201627
1621- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1628+ if (
1629+ inspect .isclass (origin )
1630+ and issubclass (origin , BaseAPIResponse )
1631+ # we only want to actually return the custom BaseAPIResponse class if we're
1632+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1633+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1634+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1635+ ):
16221636 if not issubclass (origin , AsyncAPIResponse ):
16231637 raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
16241638
0 commit comments