@@ -1046,7 +1046,14 @@ def _process_response(
10461046 ) -> ResponseT :
10471047 origin = get_origin (cast_to ) or cast_to
10481048
1049- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1049+ if (
1050+ inspect .isclass (origin )
1051+ and issubclass (origin , BaseAPIResponse )
1052+ # we only want to actually return the custom BaseAPIResponse class if we're
1053+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1054+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1055+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1056+ ):
10501057 if not issubclass (origin , APIResponse ):
10511058 raise TypeError (f"API Response types must subclass { APIResponse } ; Received { origin } " )
10521059
@@ -1549,7 +1556,14 @@ async def _process_response(
15491556 ) -> ResponseT :
15501557 origin = get_origin (cast_to ) or cast_to
15511558
1552- if inspect .isclass (origin ) and issubclass (origin , BaseAPIResponse ):
1559+ if (
1560+ inspect .isclass (origin )
1561+ and issubclass (origin , BaseAPIResponse )
1562+ # we only want to actually return the custom BaseAPIResponse class if we're
1563+ # returning the raw response, or if we're not streaming SSE, as if we're streaming
1564+ # SSE then `cast_to` doesn't actively reflect the type we need to parse into
1565+ and (not stream or bool (response .request .headers .get (RAW_RESPONSE_HEADER )))
1566+ ):
15531567 if not issubclass (origin , AsyncAPIResponse ):
15541568 raise TypeError (f"API Response types must subclass { AsyncAPIResponse } ; Received { origin } " )
15551569
0 commit comments