Repository navigation
fix(proxy): attribute provider and model_info on pre_call_hook rejections #41077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yucheng-berri
merged 9 commits into
main
from
litellm_pre_call_reject_provider_attribution
Sep 22, 2026
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39b4baf
fix(proxy): attribute provider and model info on pre-call rejected re…
devin-ai-integration[bot] 0860660
fix(proxy): keep pre-call rejections out of deployment cooldown and p…
yucheng-berri 196c5df
Merge remote-tracking branch 'origin/main' into litellm_pre_call_reje…
yucheng-berri 1648e90
fix(proxy): skip deployment attribution when the rejected body's mode…
yucheng-berri cadb93e
fix(prometheus): bucket non-string request models as other instead of…
yucheng-berri fc4f4cd
fix(proxy): resolve team deployments and treat guardrail rejects as p…
yucheng-berri 25bafc2
fix(prometheus): flag pre-routing rejects instead of matching excepti…
yucheng-berri 5e7b073
fix(proxy): key pre-routing reject flag off provider handoff, not cal…
yucheng-berri d6bc323
test(e2e): poll for both served and rejected spend rows before assert…
yucheng-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/test_litellm/integrations/test_prometheus_deployment_state_proxy_rejects.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """ | ||
| LIT-7701 attributes a pre_call_hook rejection's failure log to the model | ||
| group's single deployment (``model_id`` and ``custom_llm_provider``) and flags | ||
| it with ``PROXY_REJECTED_BEFORE_ROUTING_KEY``. The deployment health metrics | ||
| must keep treating such rejects as "no deployment picked": a key rate limit or | ||
| guardrail block never reached the deployment, so it must not flip | ||
| ``litellm_deployment_state`` to partial outage or count as a deployment failure | ||
| response. A failure raised after the router picked a deployment (a post-call | ||
| guardrail block, a provider error) carries no flag and keeps its deployment labels. | ||
| """ | ||
|
|
||
| import pytest | ||
| from fastapi import HTTPException | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| from prometheus_client import REGISTRY | ||
|
|
||
| from litellm.constants import PROXY_REJECTED_BEFORE_ROUTING_KEY | ||
| from litellm.exceptions import GuardrailRaisedException | ||
| from litellm.integrations.prometheus import PrometheusLogger | ||
| from litellm.proxy._types import ProxyException | ||
| from litellm.proxy.common_utils.proxy_rate_limit_error import ProxyRateLimitError | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def cleanup_prometheus_registry(): | ||
| for collector in list(REGISTRY._collector_to_names.keys()): | ||
| try: | ||
| REGISTRY.unregister(collector) | ||
| except Exception: | ||
| pass | ||
|
|
||
| yield | ||
|
|
||
| for collector in list(REGISTRY._collector_to_names.keys()): | ||
| try: | ||
| REGISTRY.unregister(collector) | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| def _attributed_failure_kwargs(exception: Exception, rejected_before_routing: bool) -> dict: | ||
| return { | ||
| "model": "openai/gpt-4.1", | ||
| "litellm_params": { | ||
| "custom_llm_provider": "openai", | ||
| "metadata": {"model_info": {"id": "dep-1"}, "model_group": "internal-model"}, | ||
| **({PROXY_REJECTED_BEFORE_ROUTING_KEY: True} if rejected_before_routing else {}), | ||
| }, | ||
| "standard_logging_object": { | ||
| "model_id": "dep-1", | ||
| "model_group": "internal-model", | ||
| "api_base": "https://api.openai.com", | ||
| "metadata": {}, | ||
| }, | ||
| "exception": exception, | ||
| } | ||
|
|
||
|
|
||
| def _model_id_values(metric) -> set[str]: | ||
| index = metric._labelnames.index("model_id") | ||
| return {sample_key[index] for sample_key in metric._metrics} | ||
|
|
||
|
|
||
| class _ProviderError(Exception): | ||
| status_code = 500 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "rejection", | ||
| [ | ||
| HTTPException(status_code=403, detail="guardrail blocked"), | ||
| ProxyException(message="budget exceeded", type="budget_exceeded", param=None, code=400), | ||
| ProxyRateLimitError(detail={"error": "key rpm limit"}), | ||
| GuardrailRaisedException(guardrail_name="pii", message="blocked", status_code=403), | ||
| ], | ||
| ids=["http_exception", "proxy_exception", "proxy_rate_limit", "guardrail_raised"], | ||
| ) | ||
| def test_attributed_proxy_reject_leaves_deployment_healthy(rejection: Exception): | ||
| logger = PrometheusLogger() | ||
|
|
||
| logger.set_llm_deployment_failure_metrics(_attributed_failure_kwargs(rejection, rejected_before_routing=True)) | ||
|
|
||
| assert logger.litellm_deployment_state._metrics == {} | ||
| assert _model_id_values(logger.litellm_deployment_failure_responses) == {""} | ||
| assert _model_id_values(logger.litellm_deployment_total_requests) == {""} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "failure", | ||
| [ | ||
| _ProviderError("upstream 500"), | ||
| GuardrailRaisedException(guardrail_name="pii", message="response blocked", status_code=400), | ||
| ], | ||
| ids=["provider_error", "post_call_guardrail"], | ||
| ) | ||
| def test_failure_after_routing_still_marks_deployment_partial_outage(failure: Exception): | ||
| logger = PrometheusLogger() | ||
|
|
||
| logger.set_llm_deployment_failure_metrics(_attributed_failure_kwargs(failure, rejected_before_routing=False)) | ||
|
|
||
| assert _model_id_values(logger.litellm_deployment_state) == {"dep-1"} | ||
| assert _model_id_values(logger.litellm_deployment_failure_responses) == {"dep-1"} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.