⚡️ Speed up method LiteLLMResponsesTransformationHandler.convert_chat_completion_messages_to_responses_api by 23%
#168
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.
📄 23% (0.23x) speedup for
LiteLLMResponsesTransformationHandler.convert_chat_completion_messages_to_responses_apiinlitellm/completion_extras/litellm_responses_transformation/transformation.py⏱️ Runtime :
7.62 milliseconds→6.19 milliseconds(best of255runs)📝 Explanation and details
The optimized code achieves a 23% speedup through several key performance optimizations:
Main Optimizations:
Method lookup caching: The most impactful change is caching
self._convert_content_to_responses_formatas a local variable_convert_content_to_responses_format. This eliminates repeated attribute lookups in the hot loop that processes each message, saving substantial time when processing many messages.Reduced dictionary key checks: In the tool_calls processing section, instead of using
"name" in functionand"arguments" in functionchecks followed by separate dictionary access, the code now usesfunction.get("name")andfunction.get("arguments")with None checks. This halves the dictionary lookups from 4 to 2 per tool call.List method binding: For list processing,
result.appendandself._convert_content_str_to_input_textare bound to local variables (append_result,convert_str) to avoid repeated method lookups during iteration.Tuple membership testing: Changed the list of accepted content types to a tuple, which is faster for
inoperator checks due to better hash table optimization.Performance Impact by Test Type:
The optimizations maintain identical behavior and error handling while focusing on the most frequently executed code paths during message transformation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-LiteLLMResponsesTransformationHandler.convert_chat_completion_messages_to_responses_api-mhdlm11land push.