perf: replace enum construction with frozenset lookup#20302
Merged
AlexsanderHamir merged 1 commit intoBerriAI:mainfrom Feb 7, 2026
Merged
perf: replace enum construction with frozenset lookup#20302AlexsanderHamir merged 1 commit intoBerriAI:mainfrom
AlexsanderHamir merged 1 commit intoBerriAI:mainfrom
Conversation
…g_request CallTypes(call_type) was constructing an enum from string on every call, taking ~4.6µs/call (69.6% of function time). Replace with a frozenset membership test for ~0.8µs/call (8.3x faster).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Greptile OverviewGreptile SummaryThis PR optimizes the Key changes:
Performance impact:The function is called frequently in the request processing path (4 call sites in Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/utils.py | Replaced enum construction with frozenset lookup for 8.3x performance improvement in _is_streaming_request function |
| tests/test_litellm/test_utils.py | Added comprehensive test coverage for _is_streaming_request with 10 test cases covering string/enum inputs and edge cases |
Sequence Diagram
sequenceDiagram
participant Caller
participant _is_streaming_request
participant _STREAMING_CALL_TYPES as frozenset
Note over Caller,_STREAMING_CALL_TYPES: Before: Enum Construction (4.6µs)
Caller->>_is_streaming_request: kwargs, call_type (str or enum)
_is_streaming_request->>_is_streaming_request: Check if stream=True in kwargs
alt stream=True
_is_streaming_request-->>Caller: True
else stream not True
_is_streaming_request->>_is_streaming_request: OLD: Try CallTypes(call_type) construction
_is_streaming_request->>_is_streaming_request: OLD: Compare with enum members
_is_streaming_request-->>Caller: True/False (slow)
end
Note over Caller,_STREAMING_CALL_TYPES: After: Frozenset Lookup (0.8µs)
Caller->>_is_streaming_request: kwargs, call_type (str or enum)
_is_streaming_request->>_is_streaming_request: Check if stream=True in kwargs
alt stream=True
_is_streaming_request-->>Caller: True
else stream not True
_is_streaming_request->>_STREAMING_CALL_TYPES: NEW: call_type in frozenset?
_STREAMING_CALL_TYPES-->>_is_streaming_request: True/False (fast O(1) lookup)
_is_streaming_request-->>Caller: True/False
end
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.
CallTypes(call_type) was constructing an enum from string on every call, taking ~4.6µs/call (69.6% of total function time). Replace with a frozenset membership test for ~0.8µs/call (8.3x faster).
Before
After
Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitCI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
Performance
🧹 Refactoring
Changes