From 46018bd801daf67f471398f1719a9f3cb4a30489 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Wed, 17 Dec 2025 12:47:15 +0530 Subject: [PATCH] fix: TypeError: TypedDict does not support instance and class checks --- litellm/integrations/custom_guardrail.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/litellm/integrations/custom_guardrail.py b/litellm/integrations/custom_guardrail.py index 6892ba3426a..fe0ce208ee6 100644 --- a/litellm/integrations/custom_guardrail.py +++ b/litellm/integrations/custom_guardrail.py @@ -240,6 +240,28 @@ class CustomGuardrail(CustomLogger): return metadata["disable_global_guardrail"] return False + def _is_valid_response_type(self, result: Any) -> bool: + """ + Check if result is a valid LLMResponseTypes instance. + + Safely handles TypedDict types which don't support isinstance checks. + For non-LiteLLM responses (like passthrough httpx.Response), returns True + to allow them through. + """ + if result is None: + return False + + try: + # Try isinstance check on valid types that support it + response_types = get_args(LLMResponseTypes) + return isinstance(result, response_types) + except TypeError as e: + # TypedDict types don't support isinstance checks + # In this case, we can't validate the type, so we allow it through + if "TypedDict" in str(e): + return True + raise + def get_guardrail_from_metadata( self, data: dict ) -> Union[List[str], List[Dict[str, DynamicGuardrailParams]]]: @@ -342,7 +364,7 @@ class CustomGuardrail(CustomLogger): response=response, ) - if result is None or not isinstance(result, get_args(LLMResponseTypes)): + if not self._is_valid_response_type(result): return response return result