Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/hooks/cache_control_check.py: 50%

26 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 12:01 +0000

1# What this does? 

2## Checks if key is allowed to use the cache controls passed in to the completion() call 

3 

4 

5from typing import Final 

6 

7from fastapi import HTTPException 

8 

9from litellm import verbose_logger 

10from litellm._logging import verbose_proxy_logger 

11from litellm.caching.caching import DualCache 

12from litellm.integrations.custom_logger import CustomLogger 

13from litellm.proxy._types import UserAPIKeyAuth 

14 

15 

16class _PROXY_CacheControlCheck(CustomLogger): 

17 # Class variables or attributes 

18 def __init__(self): 

19 pass 

20 

21 async def async_pre_call_hook( 

22 self, 

23 user_api_key_dict: UserAPIKeyAuth, 

24 cache: DualCache, 

25 data: dict, 

26 call_type: str, 

27 ): 

28 try: 

29 verbose_proxy_logger.debug("Inside Cache Control Check Pre-Call Hook") 

30 allowed_cache_controls: Final = user_api_key_dict.allowed_cache_controls 

31 

32 if data.get("cache", None) is None: 32 ↛ 35line 32 didn't jump to line 35 because the condition on line 32 was always true

33 return 

34 

35 cache_args: Final = data.get("cache", None) 

36 if isinstance(cache_args, dict): 

37 for k, v in cache_args.items(): 

38 if ( 

39 (allowed_cache_controls is not None) 

40 and (isinstance(allowed_cache_controls, list)) 

41 and ( 

42 len(allowed_cache_controls) > 0 

43 ) # assume empty list to be nullable - https://github.com/prisma/prisma/issues/847#issuecomment-546895663 

44 and k not in allowed_cache_controls 

45 ): 

46 raise HTTPException( 

47 status_code=403, 

48 detail=f"Not allowed to set {k} as a cache control. Contact admin to change permissions.", 

49 ) 

50 else: # invalid cache 

51 return 

52 

53 except HTTPException as e: 

54 raise e 

55 except Exception as e: 

56 verbose_logger.exception( 

57 "litellm.proxy.hooks.cache_control_check.py::async_pre_call_hook(): Exception occured - %s", e 

58 )