fix(prometheus): default to status_code=500 for exceptions without status code#24264
Open
sourrris wants to merge 3 commits intoBerriAI:mainfrom
Open
fix(prometheus): default to status_code=500 for exceptions without status code#24264sourrris wants to merge 3 commits intoBerriAI:mainfrom
sourrris wants to merge 3 commits intoBerriAI:mainfrom
Conversation
…24224) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…atus code _extract_status_code() returned None when an exception lacked status_code/code attributes. str(None) became the literal 'None' in Prometheus labels, causing litellm_proxy_total_requests_metric 4xx/5xx aggregations to not match litellm_proxy_failed_requests_metric. Fixes BerriAI#24224 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Contributor
Greptile SummaryThis PR fixes a bug where Key changes:
The fix is minimal, well-scoped, and correctly avoids touching the non-exception ( Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/integrations/prometheus.py | Adds a fallback in _extract_status_code() to return 500 when an exception is present but carries no extractable status code, preventing "None" Prometheus labels. Also includes two purely cosmetic formatting fixes to ternary expressions for stream labels. |
| tests/test_litellm/integrations/test_prometheus_status_code_none.py | New regression test file with 5 mock-only unit tests covering: exception with status_code, exception with code, bare exception (→500), no exception (→None), and bare exception via kwargs. All tests are mock-only with no real network calls. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[_extract_status_code called] --> B{enum_values has\nstatus_code?}
B -- Yes --> C[status_code = int\nenum_values.status_code]
B -- No --> D{exception\nprovided?}
C --> D
D -- Yes --> E{exception has\nstatus_code or code?}
E -- Yes --> F[status_code = int\nexception attr]
E -- No --> G{kwargs has\nexception?}
F --> G
D -- No --> G
G -- Yes --> H{kwargs exception has\nstatus_code or code?}
H -- Yes --> I[status_code = int\nkwargs exception attr]
H -- No --> J{status_code is None AND\nexception present?}
I --> J
G -- No --> J
J -- Yes --> K[return 500\nnew fallback]
J -- No --> L[return status_code\nor None]
style K fill:#f96,stroke:#c00,color:#fff
Last reviewed commit: "style(prometheus): a..."
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
edd2dd7 to
9d88a63
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
_extract_status_code()returnedNonewhen an exception lackedstatus_code/codeattributesstr(None)became the literal"None"in Prometheus labels, causinglitellm_proxy_total_requests_metric4xx/5xx aggregations to not matchlitellm_proxy_failed_requests_metric500(unclassified server error) when an exception is present but carries no extractable status code — covers both directexceptionparam andkwargs["exception"]pathsTest plan
tests/test_litellm/integrations/test_prometheus_status_code_none.pytest_prometheus_invalid_key_filtering.py— 2 pre-existing async failures unrelated to this change)Fixes #24224
🤖 Generated with Claude Code