@@ -1086,7 +1086,14 @@ def _process_response(
1086
1086
1087
1087
origin = get_origin (cast_to ) or cast_to
1088
1088
1089
- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1089
+ if (
1090
+ inspect .isclass (origin )
1091
+ and issubclass (origin , BaseAPIResponse )
1092
+ # we only want to actually return the custom BaseAPIResponse class if we're
1093
+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1094
+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1095
+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1096
+ ):
1090
1097
if not issubclass (origin , APIResponse ):
1091
1098
raise TypeError (f"API Response types must subclass { APIResponse } ; Received { origin } " )
1092
1099
@@ -1603,7 +1610,14 @@ async def _process_response(
1603
1610
1604
1611
origin = get_origin (cast_to ) or cast_to
1605
1612
1606
- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1613
+ if (
1614
+ inspect .isclass (origin )
1615
+ and issubclass (origin , BaseAPIResponse )
1616
+ # we only want to actually return the custom BaseAPIResponse class if we're
1617
+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1618
+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1619
+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1620
+ ):
1607
1621
if not issubclass (origin , AsyncAPIResponse ):
1608
1622
raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
1609
1623
0 commit comments