fix(proxy): keep exception text out of pricing and extension warnings

Review follow-ups:
- _warn_pricing_once now logs the model and exception type at WARNING; the
  exception itself goes to DEBUG (pricing errors can echo client model strings).
  Its dedupe set is capped at 1024 and a key is marked only if WARNING is on.
- Extension enumeration failure logs the exception type; traceback is DEBUG.
- A valid HEADROOM_OPENAI_TOOL_SEARCH_MODELS pattern re-arms the bad-regex
  warning, so bad A, valid B, bad A warns twice.
- Tests: re-arm case, an import-time knob warning (reload, valid case silent),
  and assertions that exception text stays out of the WARNING lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 4% strict / 4% weighted
This commit is contained in:
Tejas Chopra
2026-10-09 18:30:09 -07:00
co-authored by Claude Opus 5.5
parent 8ef70d2649
commit d83e7d5c3e
8 changed files with 93 additions and 20 deletions
+24 -10
View File
@@ -58,16 +58,29 @@ logger = logging.getLogger("headroom.proxy")
# unresolvable model (a custom / OpenAI-compatible name LiteLLM can't price,
# e.g. glm-5.2) floods proxy.log with an identical WARNING every single request
# (#2504). Track which models have already been warned so each fires once per
# process; the set is tiny and bounded by the number of distinct models seen.
# process. Model names come from clients, so the set is capped: when it fills it
# is cleared, which at worst repeats a warning instead of growing without bound.
_warned_pricing_models: set[str] = set()
_WARNED_PRICING_MODELS_MAX = 1024
def _warn_pricing_once(model: str, message: str) -> None:
"""Emit ``message`` at WARNING only the first time ``model`` fails pricing."""
if model in _warned_pricing_models:
def _warn_pricing_once(model: str, message: str, exc: BaseException | None = None) -> None:
"""Emit ``message`` at WARNING only the first time ``model`` fails pricing.
With ``exc``, the WARNING names only the exception type: pricing errors can
echo the client's model string or provider internals. The full exception
goes to DEBUG.
"""
if model in _warned_pricing_models or not logger.isEnabledFor(logging.WARNING):
return
if len(_warned_pricing_models) >= _WARNED_PRICING_MODELS_MAX:
_warned_pricing_models.clear()
_warned_pricing_models.add(model)
logger.warning(message)
if exc is None:
logger.warning(message)
return
logger.warning("%s (%s)", message, type(exc).__name__)
logger.debug("%s", message, exc_info=exc)
# A route whose responses never carry a usage breakdown hits the estimated-basis
@@ -278,7 +291,8 @@ def build_prefix_cache_stats(
except Exception as e:
_warn_pricing_once(
f"__cache_rates__:{model_name}",
f"Failed to resolve cache rates for model {model_name}: {e}",
f"Failed to resolve cache rates for model {model_name}",
e,
)
cache_prices = None
best_tokens = tokens_sent
@@ -1000,7 +1014,7 @@ class CostTracker:
return float(total_cost) if total_cost > 0 else None
except Exception as e:
_warn_pricing_once(model, f"Failed to get pricing for model {model}: {e}")
_warn_pricing_once(model, f"Failed to get pricing for model {model}", e)
return None
def _prune_old_costs(self):
@@ -1375,7 +1389,7 @@ class CostTracker:
return cost_per_token * 1_000_000 if cost_per_token else None
except Exception as e:
_warn_pricing_once(
f"__list_price__:{model}", f"Failed to get list price for model {model}: {e}"
f"__list_price__:{model}", f"Failed to get list price for model {model}", e
)
return None
@@ -1399,7 +1413,7 @@ class CostTracker:
return base or None
except Exception as e:
_warn_pricing_once(
f"__output_price__:{model}", f"Failed to get output price for model {model}: {e}"
f"__output_price__:{model}", f"Failed to get output price for model {model}", e
)
return None
@@ -1456,7 +1470,7 @@ class CostTracker:
rates = resolve_rates(model, long_context=long_context, for_billing=for_billing)
except Exception as e:
_warn_pricing_once(
f"__cache_rates__:{model}", f"Failed to resolve cache rates for model {model}: {e}"
f"__cache_rates__:{model}", f"Failed to resolve cache rates for model {model}", e
)
return None
if rates is None or not rates.uncached:
+4 -4
View File
@@ -124,11 +124,11 @@ def discover() -> Iterator[tuple[str, ProxyExtension]]:
entries = importlib.metadata.entry_points(group=ENTRY_POINT_GROUP)
except Exception as exc: # noqa: BLE001 — importlib.metadata can raise varied types
log.warning(
"proxy extensions: entry-point enumeration failed, so no proxy extensions "
"will be installed: %s",
exc,
exc_info=True,
"proxy extensions: entry-point enumeration failed (%s), so no proxy "
"extensions will be installed; enable debug logging for the traceback",
type(exc).__name__,
)
log.debug("proxy extensions: entry-point enumeration failure detail", exc_info=True)
return
for entry in entries:
try:
+6 -2
View File
@@ -4662,10 +4662,10 @@ def _model_supports_openai_tool_search(model: str | None) -> bool:
override = os.environ.get("HEADROOM_OPENAI_TOOL_SEARCH_MODELS", "").strip()
if override:
try:
return re.search(override, model) is not None
matched = re.search(override, model) is not None
except re.error as exc:
# Malformed override → fall back to the version gate.
if override != _openai_tool_search_bad_pattern:
if override != _openai_tool_search_bad_pattern and logger.isEnabledFor(logging.WARNING):
_openai_tool_search_bad_pattern = override
logger.warning(
"Ignoring invalid HEADROOM_OPENAI_TOOL_SEARCH_MODELS regex %r (%s); "
@@ -4673,6 +4673,10 @@ def _model_supports_openai_tool_search(model: str | None) -> bool:
override,
exc,
)
else:
# A valid pattern re-arms the warning for the next bad one.
_openai_tool_search_bad_pattern = None
return matched
match = re.match(r"gpt-(\d+)(?:\.(\d+))?", model.strip().lower())
if not match:
return False
+2 -1
View File
@@ -396,4 +396,5 @@ def test_cache_rate_failure_warns_once_per_model(monkeypatch: pytest.MonkeyPatch
warnings = [r.getMessage() for r in capture.records if r.levelno == logging.WARNING]
assert len(warnings) == 1
assert "for model m:" in warnings[0] and "pricing table corrupt" in warnings[0]
assert "for model m" in warnings[0] and "RuntimeError" in warnings[0]
assert "pricing table corrupt" not in warnings[0] # exception text stays at DEBUG
+2 -2
View File
@@ -46,8 +46,8 @@ def test_distinct_models_each_warn_once(cost_tracker, caplog):
cost_tracker.estimate_cost("mystery-model", 10, 5)
msgs = [r.getMessage() for r in caplog.records if "Failed to get pricing" in r.getMessage()]
assert sum("for model glm-5.2:" in m for m in msgs) == 1
assert sum("for model mystery-model:" in m for m in msgs) == 1
assert sum("for model glm-5.2 (" in m for m in msgs) == 1
assert sum("for model mystery-model (" in m for m in msgs) == 1
def test_litellm_unavailable_warns_once_per_model(monkeypatch, caplog):
+18
View File
@@ -89,6 +89,24 @@ def test_malformed_override_warns_once_not_per_request(monkeypatch):
assert "[unclosed" in warnings[0].getMessage()
def test_valid_override_rearms_the_malformed_warning(monkeypatch):
"""Bad A, valid B, bad A again: the second A is reported too."""
from headroom.proxy import helpers
monkeypatch.setattr(helpers, "_openai_tool_search_bad_pattern", None)
capture = _Capture()
proxy_logger = logging.getLogger("headroom.proxy")
proxy_logger.addHandler(capture)
try:
for pattern in ("[unclosed", r"^gpt-5\.4", "[unclosed"):
monkeypatch.setenv("HEADROOM_OPENAI_TOOL_SEARCH_MODELS", pattern)
assert _model_supports_openai_tool_search("gpt-5.4") is True
finally:
proxy_logger.removeHandler(capture)
assert [r.levelno for r in capture.records] == [logging.WARNING, logging.WARNING]
# --- deferral behavior -------------------------------------------------------
@@ -1,6 +1,7 @@
from __future__ import annotations
import importlib
import logging
import os
import pytest
@@ -39,3 +40,37 @@ def test_bad_env_falls_back_to_default(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_COMPRESSION_TIMEOUT_SECONDS", "not-a-number")
importlib.reload(helpers)
assert helpers.COMPRESSION_TIMEOUT_SECONDS == 30.0
class _Capture(logging.Handler):
def __init__(self) -> None:
super().__init__(logging.DEBUG)
self.messages: list[str] = []
def emit(self, record: logging.LogRecord) -> None:
if record.levelno == logging.WARNING:
self.messages.append(record.getMessage())
def _reload_warnings() -> list[str]:
capture = _Capture()
proxy_logger = logging.getLogger("headroom.proxy")
proxy_logger.addHandler(capture)
try:
importlib.reload(helpers)
finally:
proxy_logger.removeHandler(capture)
return [m for m in capture.messages if "HEADROOM_COMPRESSION_TIMEOUT_SECONDS" in m]
def test_bad_env_says_it_was_ignored(monkeypatch: pytest.MonkeyPatch) -> None:
"""A typo in the knob used to revert to 30s without a word."""
monkeypatch.setenv("HEADROOM_COMPRESSION_TIMEOUT_SECONDS", "not-a-number")
assert _reload_warnings() == [
"Ignoring invalid HEADROOM_COMPRESSION_TIMEOUT_SECONDS='not-a-number'; using 30.0"
]
def test_valid_env_is_silent(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_COMPRESSION_TIMEOUT_SECONDS", "75")
assert _reload_warnings() == []
+2 -1
View File
@@ -76,7 +76,8 @@ def test_discover_warns_when_entry_point_enumeration_fails(monkeypatch) -> None:
assert len(warnings) == 1
assert "no proxy extensions will be installed" in warnings[0]
assert "corrupt dist-info" in warnings[0]
assert "RuntimeError" in warnings[0]
assert "corrupt dist-info" not in warnings[0] # detail stays at DEBUG
def test_discover_is_quiet_when_nothing_is_installed(monkeypatch) -> None: