@@ -1071,7 +1071,14 @@ def _process_response(
10711071 ) -> ResponseT :
10721072 origin = get_origin (cast_to ) or cast_to
10731073
1074- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1074+ if (
1075+ inspect .isclass (origin )
1076+ and issubclass (origin , BaseAPIResponse )
1077+ # we only want to actually return the custom BaseAPIResponse class if we're
1078+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1079+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1080+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1081+ ):
10751082 if not issubclass (origin , APIResponse ):
10761083 raise TypeError (f"API Response types must subclass { APIResponse } ; Received { origin } " )
10771084
@@ -1574,7 +1581,14 @@ async def _process_response(
15741581 ) -> ResponseT :
15751582 origin = get_origin (cast_to ) or cast_to
15761583
1577- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1584+ if (
1585+ inspect .isclass (origin )
1586+ and issubclass (origin , BaseAPIResponse )
1587+ # we only want to actually return the custom BaseAPIResponse class if we're
1588+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1589+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1590+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1591+ ):
15781592 if not issubclass (origin , AsyncAPIResponse ):
15791593 raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
15801594
0 commit comments