@@ -1071,7 +1071,14 @@ def _process_response(
1071
1071
) -> ResponseT :
1072
1072
origin = get_origin (cast_to ) or cast_to
1073
1073
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
+ ):
1075
1082
if not issubclass (origin , APIResponse ):
1076
1083
raise TypeError (f"API Response types must subclass { APIResponse } ; Received { origin } " )
1077
1084
@@ -1574,7 +1581,14 @@ async def _process_response(
1574
1581
) -> ResponseT :
1575
1582
origin = get_origin (cast_to ) or cast_to
1576
1583
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
+ ):
1578
1592
if not issubclass (origin , AsyncAPIResponse ):
1579
1593
raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
1580
1594
0 commit comments