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