Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: TypeError: TypedDict does not support instance and class checks

Sameer Kankute46018bd801da
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