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 <krrishdholakia@gmail.com>
This commit is contained in:
Cursor Agent 2026-03-08 02:05:30 +00:00
parent 02e6b59abd
commit d159762010
2 changed files with 12 additions and 5 deletions

View file

@ -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,

View file

@ -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)