@@ -1113,7 +1113,14 @@ def _process_response(
1113
1113
) -> ResponseT :
1114
1114
origin = get_origin (cast_to ) or cast_to
1115
1115
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
+ ):
1117
1124
if not issubclass (origin , APIResponse ):
1118
1125
raise TypeError (f"API Response types must subclass { APIResponse } ; Received { origin } " )
1119
1126
@@ -1618,7 +1625,14 @@ async def _process_response(
1618
1625
) -> ResponseT :
1619
1626
origin = get_origin (cast_to ) or cast_to
1620
1627
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
+ ):
1622
1636
if not issubclass (origin , AsyncAPIResponse ):
1623
1637
raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
1624
1638
0 commit comments