diff --git a/litellm/_logging.py b/litellm/_logging.py index 6a579d91c53..a7eadd41f93 100644 --- a/litellm/_logging.py +++ b/litellm/_logging.py @@ -113,7 +113,7 @@ class HealthCheckAccessLogFilter(logging.Filter): path = full_path.split("?", 1)[0] if path in _DISABLED_ACCESS_LOG_PATHS: return False - except Exception: + except Exception: # noqa: BLE001 return True return True diff --git a/litellm/integrations/asqav/asqav.py b/litellm/integrations/asqav/asqav.py index 975976efd39..d6680c4b547 100644 --- a/litellm/integrations/asqav/asqav.py +++ b/litellm/integrations/asqav/asqav.py @@ -63,7 +63,7 @@ def _read_tail(fh: BinaryIO, size: int) -> bytes: chunk_size *= 2 -def _content_digest(value: Any) -> Optional[str]: +def _content_digest(value: Any) -> Optional[str]: # noqa: ANN401 """Return a SHA-256 hex digest of a content value, or None if empty.""" if value is None: return None @@ -73,9 +73,9 @@ def _content_digest(value: Any) -> Optional[str]: def _extract_loggable( kwargs: dict[str, Any], - response_obj: Any, - start_time: Any, - end_time: Any, + response_obj: Any, # noqa: ANN401 + start_time: Any, # noqa: ANN401 + end_time: Any, # noqa: ANN401 status: str, ) -> dict[str, Any]: """Pull metadata + digests out of a callback invocation. @@ -121,7 +121,7 @@ def _extract_loggable( # litellm_params.metadata keys to avoid unexpected bleed. if k in _PROXY_IDENTITY_KEYS: metadata.setdefault(k, v) - except Exception: + except Exception: # noqa: BLE001 pass # Timing @@ -129,7 +129,7 @@ def _extract_loggable( try: if start_time is not None and end_time is not None: latency_ms = int((end_time - start_time).total_seconds() * 1000) - except Exception: + except Exception: # noqa: BLE001 pass # Usage @@ -149,7 +149,7 @@ def _extract_loggable( provider_request_id = response_obj._hidden_params.get( "x-request-id" ) or response_obj._hidden_params.get("cf-ray") - except Exception: + except Exception: # noqa: BLE001 pass # Content digests (not content itself) @@ -160,7 +160,7 @@ def _extract_loggable( if hasattr(response_obj, "choices") and response_obj.choices: content = response_obj.choices[0].message.content response_content_digest = _content_digest(content) - except Exception: + except Exception: # noqa: BLE001 pass # Standard logging payload may carry call_id / litellm_call_id @@ -169,7 +169,7 @@ def _extract_loggable( slp: Any = kwargs.get("standard_logging_object") if slp and isinstance(slp, dict): call_id = slp.get("id") or slp.get("litellm_call_id") - except Exception: + except Exception: # noqa: BLE001 pass if not call_id: call_id = kwargs.get("litellm_call_id") or kwargs.get( @@ -264,7 +264,7 @@ class AsqavLogger(CustomLogger): last_record = json.loads(lines[-1].decode("utf-8")) self._prev_hash = last_record.get("record_hash", _GENESIS_HASH) self._call_count = last_record.get("seq", -1) + 1 - except Exception: + except Exception: # noqa: BLE001 verbose_logger.debug( f"[AsqavLogger] Could not load chain tail: {traceback.format_exc()}" ) @@ -276,9 +276,9 @@ class AsqavLogger(CustomLogger): def _build_and_append( self, kwargs: dict[str, Any], - response_obj: Any, - start_time: Any, - end_time: Any, + response_obj: Any, # noqa: ANN401 + start_time: Any, # noqa: ANN401 + end_time: Any, # noqa: ANN401 status: str, ) -> None: """Build one audit record and append it to the JSONL log. @@ -299,7 +299,7 @@ class AsqavLogger(CustomLogger): loggable["response_content"] = response_obj.choices[ 0 ].message.content - except Exception: + except Exception: # noqa: BLE001 pass # The file write happens under the same lock that assigns seq and @@ -326,7 +326,7 @@ class AsqavLogger(CustomLogger): self._prev_hash = record_hash self._call_count += 1 - except Exception: + except Exception: # noqa: BLE001 verbose_logger.debug( f"[AsqavLogger] Unhandled error in _build_and_append: {traceback.format_exc()}" ) @@ -350,7 +350,7 @@ class AsqavLogger(CustomLogger): try: with os.fdopen(fd, "a", encoding="utf-8", closefd=True) as fh: fh.write(json.dumps(record, separators=(",", ":")) + "\n") - except Exception: + except Exception: # noqa: BLE001 # fdopen owns fd; if it raises before returning the context # manager the fd may still be open - close defensively. try: @@ -359,7 +359,7 @@ class AsqavLogger(CustomLogger): pass raise return True - except Exception: + except Exception: # noqa: BLE001 verbose_logger.warning( f"[AsqavLogger] Failed to write audit record: {traceback.format_exc()}" ) @@ -370,17 +370,17 @@ class AsqavLogger(CustomLogger): # ------------------------------------------------------------------ def log_success_event( - self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any + self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any # noqa: ANN401 ) -> None: self._build_and_append(kwargs, response_obj, start_time, end_time, "success") def log_failure_event( - self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any + self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any # noqa: ANN401 ) -> None: self._build_and_append(kwargs, response_obj, start_time, end_time, "failure") async def async_log_success_event( - self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any + self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any # noqa: ANN401 ) -> None: await asyncio.to_thread( self._build_and_append, @@ -392,7 +392,7 @@ class AsqavLogger(CustomLogger): ) async def async_log_failure_event( - self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any + self, kwargs: dict[str, Any], response_obj: Any, start_time: Any, end_time: Any # noqa: ANN401 ) -> None: await asyncio.to_thread( self._build_and_append, @@ -454,5 +454,5 @@ class AsqavLogger(CustomLogger): return True, "ok" except FileNotFoundError: return False, f"log file not found: {path}" - except Exception as exc: + except Exception as exc: # noqa: BLE001 return False, f"verification error: {exc}" diff --git a/litellm/integrations/opentelemetry.py b/litellm/integrations/opentelemetry.py index 1aaf9d8286e..e3baaee2d71 100644 --- a/litellm/integrations/opentelemetry.py +++ b/litellm/integrations/opentelemetry.py @@ -2125,7 +2125,7 @@ class OpenTelemetry(OTELGenAISemconvMixin, CustomLogger): if hasattr(response_obj, "model_dump"): try: response_obj = response_obj.model_dump() - except Exception: + except Exception: # noqa: BLE001 response_obj = None else: response_obj = None diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py index 1dc3c0819ce..482c3dc8aef 100644 --- a/litellm/proxy/common_request_processing.py +++ b/litellm/proxy/common_request_processing.py @@ -2437,7 +2437,7 @@ class ProxyBaseLLMRequestProcessing: @staticmethod async def _bill_partial_stream_on_disconnect( - response: Any, request_data: dict + response: Any, request_data: dict # noqa: ANN401 ) -> None: """Record SpendLogs for tokens already produced when a stream is cut off. @@ -2465,7 +2465,7 @@ class ProxyBaseLLMRequestProcessing: messages=getattr(response, "messages", None), logging_obj=logging_obj, ) - except Exception: + except Exception: # noqa: BLE001 verbose_proxy_logger.exception( "Failed to assemble partial streaming usage on client disconnect" ) @@ -2494,7 +2494,7 @@ class ProxyBaseLLMRequestProcessing: cache_hit=False, prefer_async_handlers=True, ) - except Exception: + except Exception: # noqa: BLE001 verbose_proxy_logger.exception( "Failed to record partial streaming usage on client disconnect" ) diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index 535ee8af7b1..2329a301c4a 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -184,7 +184,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): guardrailIdentifier: Optional[str] = None, guardrailVersion: Optional[str] = None, disable_exception_on_block: Optional[bool] = False, - checks: Any | None = None, + checks: Any | None = None, # noqa: ANN401 content_filter_threshold: float | None = 0.5, prompt_attack_threshold: float | None = 0.5, pii_confidence_threshold: float | None = 0.5, @@ -253,7 +253,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): ) @staticmethod - def _normalize_checks(checks: Any | None) -> dict[str, Any] | None: + def _normalize_checks(checks: Any | None) -> dict[str, Any] | None: # noqa: ANN401 """Normalize the configured `checks` into a plain dict for the API body. Accepts a pydantic ``BedrockChecksConfigModel`` or a raw dict; drops empty / @@ -729,7 +729,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self, source: Literal["INPUT", "OUTPUT"], messages: list[AllMessageValues] | None = None, - response: Any | litellm.ModelResponse | None = None, + response: Any | litellm.ModelResponse | None = None, # noqa: ANN401 request_data: dict | None = None, logging_event_type: GuardrailEventHooks | None = None, ) -> BedrockGuardrailResponse: @@ -760,7 +760,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self, source: Literal["INPUT", "OUTPUT"], messages: list[AllMessageValues] | None = None, - response: Any | litellm.ModelResponse | None = None, + response: Any | litellm.ModelResponse | None = None, # noqa: ANN401 request_data: dict | None = None, logging_event_type: GuardrailEventHooks | None = None, ) -> BedrockGuardrailResponse: @@ -828,8 +828,8 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): request_data=request_data or {}, guardrail_status=self._get_bedrock_guardrail_response_status(response=httpx_response), start_time=start_time.timestamp(), - end_time=datetime.now().timestamp(), - duration=(datetime.now() - start_time).total_seconds(), + end_time=datetime.now().timestamp(), # noqa: DTZ005 + duration=(datetime.now() - start_time).total_seconds(), # noqa: DTZ005 event_type=event_type, tracing_detail=tracing_detail or None, ) @@ -856,7 +856,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): async def _sign_and_post( self, - prepared_request: Any, + prepared_request: Any, # noqa: ANN401 request_data: dict | None, event_type: GuardrailEventHooks, start_time: "datetime", @@ -945,7 +945,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self, source: Literal["INPUT", "OUTPUT"], messages: list[AllMessageValues] | None = None, - response: Any | litellm.ModelResponse | None = None, + response: Any | litellm.ModelResponse | None = None, # noqa: ANN401 ) -> list[BedrockChecksMessage]: """Build the role-tagged `messages` array for InvokeGuardrailChecks. @@ -985,7 +985,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self, source: Literal["INPUT", "OUTPUT"], messages: list[AllMessageValues] | None = None, - response: Any | litellm.ModelResponse | None = None, + response: Any | litellm.ModelResponse | None = None, # noqa: ANN401 request_data: dict | None = None, logging_event_type: GuardrailEventHooks | None = None, ) -> BedrockGuardrailResponse: @@ -997,7 +997,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): """ from datetime import datetime - start_time = datetime.now() + start_time = datetime.now() # noqa: DTZ005 checks_messages = self._build_invoke_guardrail_checks_messages( source=source, messages=messages, response=response @@ -1050,8 +1050,8 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): request_data=request_data or {}, guardrail_status="guardrail_failed_to_respond", start_time=start_time.timestamp(), - end_time=datetime.now().timestamp(), - duration=(datetime.now() - start_time).total_seconds(), + end_time=datetime.now().timestamp(), # noqa: DTZ005 + duration=(datetime.now() - start_time).total_seconds(), # noqa: DTZ005 event_type=event_type, ) raise HTTPException(status_code=status_code, detail=detail_message) @@ -1071,8 +1071,8 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): request_data=request_data or {}, guardrail_status=self._get_invoke_checks_status(bool(violations)), start_time=start_time.timestamp(), - end_time=datetime.now().timestamp(), - duration=(datetime.now() - start_time).total_seconds(), + end_time=datetime.now().timestamp(), # noqa: DTZ005 + duration=(datetime.now() - start_time).total_seconds(), # noqa: DTZ005 event_type=event_type, tracing_detail=self._build_invoke_checks_tracing_detail(violations) or None, ) diff --git a/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/bias_hallucination_estimator.py b/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/bias_hallucination_estimator.py index 5c8e6975186..878f52495d8 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/bias_hallucination_estimator.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/bias_hallucination_estimator.py @@ -150,7 +150,7 @@ class BiasHallucinationEstimatorGuardrail(CustomGuardrail): if not self._should_check(input_type): return inputs - start_time = datetime.now() + start_time = datetime.now() # noqa: DTZ005 texts = self._extract_texts(inputs) if not texts: return inputs @@ -274,7 +274,7 @@ class BiasHallucinationEstimatorGuardrail(CustomGuardrail): request_data=request_data, guardrail_status=status, start_time=start_time.timestamp(), - end_time=datetime.now().timestamp(), + end_time=datetime.now().timestamp(), # noqa: DTZ005 duration=(datetime.now() - start_time).total_seconds(), tracing_detail=tracing_detail, ) diff --git a/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/data_sources.py b/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/data_sources.py index a0582fc607c..73ea2135529 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/data_sources.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/data_sources.py @@ -132,7 +132,7 @@ class FileDataSource(DataSource): list[str | dict[str, Any]], [{"text": line.strip()} for line in f if line.strip()], ) - except Exception: + except Exception: # noqa: BLE001 pass return [] @@ -187,7 +187,7 @@ class URLDataSource(DataSource): if response.status == 200: content = await response.text() return self._parse_content(content) - except Exception: + except Exception: # noqa: BLE001 pass return [] @@ -300,7 +300,7 @@ class VectorStoreDataSource(DataSource): ) for doc in docs ] - except Exception: + except Exception: # noqa: BLE001 pass return [] @@ -356,7 +356,7 @@ class KnowledgeGraphDataSource(DataSource): for text in (self._extract_text(binding),) if text ] - except Exception: + except Exception: # noqa: BLE001 pass return [] diff --git a/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/grounding_checker.py b/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/grounding_checker.py index 9539f002cff..3fab770ee06 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/grounding_checker.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bias_hallucination_estimator/grounding_checker.py @@ -103,7 +103,7 @@ class GroundingChecker: f"Timeout searching {source.name} for claim: {claim}" ) return [] - except Exception as e: + except Exception as e: # noqa: BLE001 verbose_logger.warning(f"Error searching {source.name}: {e}") return [] diff --git a/litellm/proxy/guardrails/usage_endpoints.py b/litellm/proxy/guardrails/usage_endpoints.py index be6c141f920..209e6c7a5d3 100644 --- a/litellm/proxy/guardrails/usage_endpoints.py +++ b/litellm/proxy/guardrails/usage_endpoints.py @@ -144,13 +144,13 @@ def _get_guardrail_attrs(g: Any) -> tuple[Any, str]: return gid, (name or gid or "") -def _get_guardrail_dict_field(g: Any, field_name: str) -> Any: +def _get_guardrail_dict_field(g: Any, field_name: str) -> Any: # noqa: ANN401 if isinstance(g, dict): return g.get(field_name) return getattr(g, field_name, None) -def _get_guardrail_litellm_params(g: Any) -> dict[str, Any]: +def _get_guardrail_litellm_params(g: Any) -> dict[str, Any]: # noqa: ANN401 litellm_params = _get_guardrail_dict_field(g, "litellm_params") if isinstance(litellm_params, dict): return litellm_params @@ -159,7 +159,7 @@ def _get_guardrail_litellm_params(g: Any) -> dict[str, Any]: return {} -def _get_guardrail_info(g: Any) -> dict[str, Any]: +def _get_guardrail_info(g: Any) -> dict[str, Any]: # noqa: ANN401 guardrail_info = _get_guardrail_dict_field(g, "guardrail_info") return guardrail_info if isinstance(guardrail_info, dict) else {} @@ -175,7 +175,7 @@ def _get_config_loaded_guardrails() -> list[Any]: ] -def _find_config_loaded_guardrail(guardrail_id_or_name: str) -> Optional[Any]: +def _find_config_loaded_guardrail(guardrail_id_or_name: str) -> Optional[Any]: # noqa: ANN401 for guardrail in _get_config_loaded_guardrails(): gid, display_name = _get_guardrail_attrs(guardrail) if guardrail_id_or_name in (gid, display_name): @@ -183,7 +183,7 @@ def _find_config_loaded_guardrail(guardrail_id_or_name: str) -> Optional[Any]: return None -def _merge_config_loaded_guardrails(db_guardrails: Any) -> list[Any]: +def _merge_config_loaded_guardrails(db_guardrails: Any) -> list[Any]: # noqa: ANN401 guardrails = list(db_guardrails) seen_keys: set[str] = set() for guardrail in guardrails: diff --git a/litellm/rag/ingestion/milvus_ingestion.py b/litellm/rag/ingestion/milvus_ingestion.py index f29afe56593..67cc3b79e80 100644 --- a/litellm/rag/ingestion/milvus_ingestion.py +++ b/litellm/rag/ingestion/milvus_ingestion.py @@ -188,7 +188,7 @@ class MilvusRAGIngestion(BaseRAGIngestion): ) inner = data.get("data") return bool(inner.get("has")) if isinstance(inner, dict) else False - except Exception as e: + except Exception as e: # noqa: BLE001 verbose_logger.debug(f"Milvus collection 'has' check failed: {e}") return False