From d159762010c5a0af07409309d5705e0161a5b46b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 8 Mar 2026 02:05:30 +0000 Subject: [PATCH] fix: address review - document _parse_url safety, guard print_verbose 1. http_handler.py _parse_url: Add docstring documenting why the LRU cache is safe with query-string URLs. When params= is non-None, httpx replaces the query string entirely; when params= is None, the cached URL preserves the original query string. Both match pre-optimization behavior (verified with httpx.URL vs str tests). 2. prometheus.py print_verbose: Guard the call behind litellm.set_verbose check so the string formatting is truly lazy. The previous % formatting was eagerly evaluated (same cost as f-string) since print_verbose takes a pre-formatted string. Co-authored-by: Krish Dholakia --- litellm/integrations/prometheus.py | 8 ++++---- litellm/llms/custom_httpx/http_handler.py | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index bfb7ade372e..c8f5a303770 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -943,10 +943,10 @@ class PrometheusLogger(CustomLogger): else: _tags = [] - print_verbose( - "inside track_prometheus_metrics, model %s, response_cost %s, tokens_used %s" - % (model, response_cost, tokens_used) - ) + if litellm.set_verbose: + print_verbose( + f"inside track_prometheus_metrics, model {model}, response_cost {response_cost}, tokens_used {tokens_used}" + ) enum_values = UserAPIKeyLabelValues( end_user=end_user_id, diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index c6013c450b1..69dc0175585 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -56,7 +56,14 @@ except Exception: @functools.lru_cache(maxsize=64) def _parse_url(url: str) -> httpx.URL: """Pre-parse a URL string into an httpx.URL to avoid regex-heavy - parsing inside httpx._merge_url on every request (~7μs → ~0.4μs).""" + parsing inside httpx._merge_url on every request (~7μs → ~0.4μs). + + Safe to use with ``build_request(params=...)``: httpx replaces the + query string entirely when ``params`` is non-None, so any query + params baked into the cached URL are harmless in that case. When + ``params`` is None the cached URL preserves the original query + string, which is the correct behaviour. + """ return httpx.URL(url)