@@ -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
@@ -1588,7 +1595,14 @@ async def _process_response(
15881595 ) -> ResponseT :
15891596 origin = get_origin (cast_to ) or cast_to
15901597
1591- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1598+ if (
1599+ inspect .isclass (origin )
1600+ and issubclass (origin , BaseAPIResponse )
1601+ # we only want to actually return the custom BaseAPIResponse class if we're
1602+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1603+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1604+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1605+ ):
15921606 if not issubclass (origin , AsyncAPIResponse ):
15931607 raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
15941608
0 commit comments