Skip to content

Fix undefined kwargs in InFlightRequestsMiddleware - #22523

Merged
jquinter merged 1 commit into
mainfrom
fix/lint-undefined-kwargs
Mar 1, 2026
Merged

jquinter merged 1 commit into
mainfrom
fix/lint-undefined-kwargs

Conversation

@jquinter

@jquinter jquinter commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • kwargs was referenced on lines 67 and 71 of in_flight_requests_middleware.py but never initialized, causing F821 Undefined name lint errors
  • Initialize kwargs as an empty dict before the conditional block

Note: The router.py:1721 PLR0915 Too many statements error is a pre-existing style warning and is not addressed here to keep this PR scoped.

Test plan

  • Verify make lint-ruff passes for this file

🤖 Generated with Claude Code

kwargs dict was used but never initialized, causing F821
(undefined name) lint errors.

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

vercel Bot commented Mar 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Building Building Preview, Comment Mar 1, 2026 8:23pm

Request Review

@greptile-apps

greptile-apps Bot commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a NameError bug in InFlightRequestsMiddleware._get_gauge() where kwargs was referenced but never initialized. This would cause the Prometheus gauge to fail to initialize on every first request, falling into the except Exception handler and leaving _gauge as None permanently — meaning no Prometheus metrics would ever be emitted for in-flight requests.

  • Initializes kwargs as an empty dict[str, Any] before the conditional block that optionally sets multiprocess_mode
  • Removes the # type: ignore[arg-type] comment that is no longer needed since kwargs is now properly typed

Confidence Score: 5/5

  • This PR is safe to merge — it's a minimal, clearly correct bug fix with no behavioral side effects.
  • The change is a one-line initialization of a variable that was previously undefined, fixing a clear runtime NameError. The fix is minimal, scoped, and correct. The code path before this fix would always throw an exception and silently disable Prometheus metrics.
  • No files require special attention.

Important Files Changed

Filename Overview
litellm/proxy/middleware/in_flight_requests_middleware.py Initializes kwargs as an empty dict before the conditional block, fixing a NameError that would occur at runtime when _get_gauge() is called. Also removes the now-unnecessary # type: ignore[arg-type] comment.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["_get_gauge() called on first HTTP request"] --> B{"Already attempted init?"}
    B -- Yes --> C["Return cached _gauge"]
    B -- No --> D["Mark init attempted"]
    D --> E["Import prometheus_client.Gauge"]
    E --> F["Initialize kwargs = {}"]
    F --> G{"PROMETHEUS_MULTIPROC_DIR set?"}
    G -- Yes --> H["kwargs['multiprocess_mode'] = 'livesum'"]
    H --> I["Create Gauge(**kwargs)"]
    G -- No --> I
    I --> J["Return _gauge"]
    E -- ImportError --> K["_gauge = None"]
    K --> J
Loading

Last reviewed commit: 22140b0

@jquinter
jquinter merged commit 00affc0 into main Mar 1, 2026
33 of 40 checks passed
@ishaan-berri
ishaan-berri deleted the fix/lint-undefined-kwargs branch March 26, 2026 22:29
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
Fix undefined kwargs in InFlightRequestsMiddleware

This branch had an error being deployed

1 failed deployment
Preview — 22140b06 Deployed Mar 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant