Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 29, 2025

📄 62% (0.62x) speedup for get_error_message_str in litellm/proxy/utils.py

⏱️ Runtime : 524 microseconds 324 microseconds (best of 218 runs)

📝 Explanation and details

The optimized code achieves a 61% speedup through two key optimizations:

1. Early Returns Eliminate Redundant Operations
The original code assigns values to error_message and then returns it at the end, requiring the function to continue executing through all remaining lines. The optimized version uses direct return statements, immediately exiting the function once a condition is met. This eliminates unnecessary variable assignments and subsequent line executions.

2. Cached Attribute Access
The original code accesses e.detail multiple times in the isinstance checks (isinstance(e.detail, str), isinstance(e.detail, dict)). The optimized version caches this with detail = e.detail and reuses the cached value, reducing attribute lookup overhead.

Performance Impact by Test Case:

  • Large dict serialization cases: 68-75% faster - Early returns prevent unnecessary operations after json.dumps() completes
  • Simple string cases: 25-45% faster - Direct returns avoid variable assignment overhead
  • Edge cases with fallbacks: 20-55% faster - Early exits prevent evaluating remaining conditions

The optimizations are particularly effective for the most common paths (string details, dict details) and scale well with data size, as seen in the large-scale test cases where the benefits of early returns are most pronounced.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 47 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 66.7%
🌀 Generated Regression Tests and Runtime
import json

# imports
import pytest  # used for our unit tests
from fastapi import HTTPException
from litellm.proxy.utils import get_error_message_str

# unit tests

# ------------------------
# Basic Test Cases
# ------------------------

def test_http_exception_with_str_detail():
    # Test HTTPException with string detail
    exc = HTTPException(status_code=400, detail="Bad request error")
    codeflash_output = get_error_message_str(exc) # 935ns -> 685ns (36.5% faster)

def test_http_exception_with_dict_detail():
    # Test HTTPException with dict detail
    detail = {"error": "Not found", "code": 404}
    exc = HTTPException(status_code=404, detail=detail)
    codeflash_output = get_error_message_str(exc) # 12.7μs -> 10.1μs (25.5% faster)

def test_regular_exception_with_str_message():
    # Test standard Exception with string message
    exc = Exception("A generic error occurred")
    codeflash_output = get_error_message_str(exc) # 1.36μs -> 1.09μs (24.5% faster)

def test_regular_exception_with_int_message():
    # Test Exception with non-string message (int)
    exc = Exception(12345)
    codeflash_output = get_error_message_str(exc) # 1.46μs -> 1.11μs (31.2% faster)

def test_regular_exception_with_dict_message():
    # Test Exception with dict message
    exc = Exception({"foo": "bar"})
    codeflash_output = get_error_message_str(exc) # 3.35μs -> 2.44μs (37.5% faster)

# ------------------------
# Edge Test Cases
# ------------------------

def test_http_exception_with_none_detail():
    # Test HTTPException with None detail
    exc = HTTPException(status_code=500, detail=None)
    codeflash_output = get_error_message_str(exc) # 893ns -> 695ns (28.5% faster)

def test_http_exception_with_list_detail():
    # Test HTTPException with list detail (unsupported type)
    exc = HTTPException(status_code=500, detail=[1, 2, 3])
    # Should fallback to str(e)
    codeflash_output = get_error_message_str(exc) # 3.88μs -> 2.85μs (35.9% faster)

def test_http_exception_with_custom_message_str():
    # HTTPException with .message attribute (string)
    class MyHTTPException(HTTPException):
        def __init__(self, detail, message):
            super().__init__(status_code=400, detail=detail)
            self.message = message

    exc = MyHTTPException(detail=None, message="Custom message here")
    codeflash_output = get_error_message_str(exc) # 1.03μs -> 766ns (34.2% faster)

def test_http_exception_with_custom_message_dict():
    # HTTPException with .message attribute (dict)
    class MyHTTPException(HTTPException):
        def __init__(self, detail, message):
            super().__init__(status_code=400, detail=detail)
            self.message = message

    msg = {"msg": "dict message", "type": "error"}
    exc = MyHTTPException(detail=None, message=msg)
    codeflash_output = get_error_message_str(exc) # 1.03μs -> 725ns (41.8% faster)

def test_http_exception_with_custom_message_none():
    # HTTPException with .message attribute None
    class MyHTTPException(HTTPException):
        def __init__(self, detail, message):
            super().__init__(status_code=400, detail=detail)
            self.message = message

    exc = MyHTTPException(detail=None, message=None)
    # Should fallback to str(e)
    codeflash_output = get_error_message_str(exc) # 987ns -> 776ns (27.2% faster)

def test_regular_exception_with_empty_message():
    # Exception with empty string message
    exc = Exception("")
    codeflash_output = get_error_message_str(exc) # 1.34μs -> 1.04μs (29.0% faster)

def test_regular_exception_with_none_message():
    # Exception with None message
    exc = Exception(None)
    codeflash_output = get_error_message_str(exc) # 1.54μs -> 1.11μs (39.0% faster)

def test_regular_exception_with_unicode_message():
    # Exception with unicode message
    exc = Exception("错误信息")
    codeflash_output = get_error_message_str(exc) # 1.32μs -> 954ns (38.5% faster)

def test_regular_exception_with_object_message():
    # Exception with object message
    class Dummy:
        def __str__(self):
            return "DummyObject"
    exc = Exception(Dummy())
    codeflash_output = get_error_message_str(exc) # 1.96μs -> 1.32μs (48.2% faster)

# ------------------------
# Large Scale Test Cases
# ------------------------

def test_http_exception_with_large_dict_detail():
    # HTTPException with large dict detail
    detail = {f"key{i}": f"value{i}" for i in range(1000)}
    exc = HTTPException(status_code=500, detail=detail)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 191μs -> 113μs (68.0% faster)

def test_http_exception_with_large_str_detail():
    # HTTPException with large string detail
    large_str = "A" * 1000
    exc = HTTPException(status_code=500, detail=large_str)
    codeflash_output = get_error_message_str(exc) # 979ns -> 747ns (31.1% faster)

def test_regular_exception_with_large_str_message():
    # Exception with large string message
    large_str = "Z" * 1000
    exc = Exception(large_str)
    codeflash_output = get_error_message_str(exc) # 1.48μs -> 1.06μs (40.0% faster)

def test_http_exception_with_large_custom_message_dict():
    # HTTPException with large .message dict
    class MyHTTPException(HTTPException):
        def __init__(self, detail, message):
            super().__init__(status_code=400, detail=detail)
            self.message = message

    msg = {f"field{i}": f"error{i}" for i in range(1000)}
    exc = MyHTTPException(detail=None, message=msg)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 1.06μs -> 771ns (37.5% faster)

def test_http_exception_with_large_custom_message_str():
    # HTTPException with large .message string
    class MyHTTPException(HTTPException):
        def __init__(self, detail, message):
            super().__init__(status_code=400, detail=detail)
            self.message = message

    large_msg = "X" * 1000
    exc = MyHTTPException(detail=None, message=large_msg)
    codeflash_output = get_error_message_str(exc) # 975ns -> 763ns (27.8% faster)

# ------------------------
# Additional Edge Cases
# ------------------------

def test_http_exception_with_detail_as_bool():
    # HTTPException with detail as boolean
    exc = HTTPException(status_code=400, detail=True)
    # Should fallback to str(e)
    codeflash_output = get_error_message_str(exc) # 3.20μs -> 2.64μs (21.2% faster)

def test_http_exception_with_detail_as_float():
    # HTTPException with detail as float
    exc = HTTPException(status_code=400, detail=3.14159)
    # Should fallback to str(e)
    codeflash_output = get_error_message_str(exc) # 4.75μs -> 3.46μs (37.2% faster)

def test_regular_exception_with_tuple_message():
    # Exception with tuple message
    exc = Exception((1, 2, 3))
    codeflash_output = get_error_message_str(exc) # 2.70μs -> 1.95μs (38.3% faster)

def test_regular_exception_with_list_message():
    # Exception with list message
    exc = Exception([1, 2, 3])
    codeflash_output = get_error_message_str(exc) # 2.47μs -> 1.73μs (42.6% faster)

def test_regular_exception_with_set_message():
    # Exception with set message
    exc = Exception({1, 2, 3})
    # Set string representation is unordered; just check type
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 3.47μs -> 2.48μs (39.6% faster)

def test_http_exception_with_detail_as_empty_dict():
    # HTTPException with detail as empty dict
    exc = HTTPException(status_code=400, detail={})
    codeflash_output = get_error_message_str(exc) # 10.2μs -> 7.84μs (30.8% faster)

def test_http_exception_with_detail_as_empty_str():
    # HTTPException with detail as empty string
    exc = HTTPException(status_code=400, detail="")
    codeflash_output = get_error_message_str(exc) # 965ns -> 671ns (43.8% faster)

def test_http_exception_with_detail_as_nested_dict():
    # HTTPException with detail as nested dict
    detail = {"outer": {"inner": "value"}}
    exc = HTTPException(status_code=400, detail=detail)
    codeflash_output = get_error_message_str(exc) # 12.5μs -> 8.87μs (40.8% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import json

# imports
import pytest  # used for our unit tests
from fastapi import HTTPException
from litellm.proxy.utils import get_error_message_str

# ---------------------------
# Basic Test Cases
# ---------------------------

def test_http_exception_with_string_detail():
    # HTTPException with a string detail
    exc = HTTPException(status_code=400, detail="Bad request error")
    codeflash_output = get_error_message_str(exc) # 928ns -> 702ns (32.2% faster)

def test_http_exception_with_dict_detail():
    # HTTPException with a dict detail
    detail = {"error": "Invalid input", "code": 123}
    exc = HTTPException(status_code=422, detail=detail)
    codeflash_output = get_error_message_str(exc) # 12.0μs -> 8.11μs (48.6% faster)

def test_http_exception_with_int_detail():
    # HTTPException with a non-str, non-dict detail (should fallback to str(e))
    exc = HTTPException(status_code=404, detail=404)
    codeflash_output = get_error_message_str(exc) # 2.40μs -> 1.69μs (41.9% faster)

def test_regular_exception_str():
    # Regular Exception with a string message
    exc = Exception("Something went wrong")
    codeflash_output = get_error_message_str(exc) # 1.39μs -> 1.08μs (28.8% faster)

def test_regular_exception_int():
    # Regular Exception with a non-string message
    exc = Exception(42)
    codeflash_output = get_error_message_str(exc) # 1.35μs -> 966ns (40.1% faster)

# ---------------------------
# Edge Test Cases
# ---------------------------

def test_http_exception_with_none_detail():
    # HTTPException with None as detail (should fallback to str(e))
    exc = HTTPException(status_code=500, detail=None)
    codeflash_output = get_error_message_str(exc) # 918ns -> 675ns (36.0% faster)

def test_http_exception_with_message_attribute_str():
    # HTTPException with a 'message' attribute (not standard, but possible)
    class CustomHTTPException(HTTPException):
        def __init__(self, status_code, detail, message):
            super().__init__(status_code=status_code, detail=detail)
            self.message = message
    exc = CustomHTTPException(status_code=401, detail=None, message="Unauthorized access")
    codeflash_output = get_error_message_str(exc) # 966ns -> 727ns (32.9% faster)

def test_http_exception_with_message_attribute_dict():
    # HTTPException with a 'message' attribute that's a dict
    class CustomHTTPException(HTTPException):
        def __init__(self, status_code, detail, message):
            super().__init__(status_code=status_code, detail=detail)
            self.message = message
    message = {"error": "Forbidden", "code": 403}
    exc = CustomHTTPException(status_code=403, detail=None, message=message)
    codeflash_output = get_error_message_str(exc) # 999ns -> 750ns (33.2% faster)

def test_http_exception_with_message_attribute_none():
    # HTTPException with a 'message' attribute that's None (should fallback to str(e))
    class CustomHTTPException(HTTPException):
        def __init__(self, status_code, detail, message):
            super().__init__(status_code=status_code, detail=detail)
            self.message = message
    exc = CustomHTTPException(status_code=403, detail=None, message=None)
    codeflash_output = get_error_message_str(exc) # 1.00μs -> 702ns (43.2% faster)

def test_http_exception_with_unusual_detail_type():
    # HTTPException with a detail that's a list (should fallback to str(e))
    exc = HTTPException(status_code=400, detail=["error", "bad"])
    codeflash_output = get_error_message_str(exc) # 4.34μs -> 3.23μs (34.7% faster)

def test_regular_exception_with_empty_message():
    # Regular Exception with an empty string message
    exc = Exception("")
    codeflash_output = get_error_message_str(exc) # 1.44μs -> 1.02μs (41.7% faster)

def test_regular_exception_with_none_message():
    # Regular Exception with None as message
    exc = Exception(None)
    codeflash_output = get_error_message_str(exc) # 1.48μs -> 1.00μs (47.2% faster)

def test_regular_exception_with_object_message():
    # Regular Exception with an object as message
    class Dummy:
        def __str__(self):
            return "Dummy object"
    exc = Exception(Dummy())
    codeflash_output = get_error_message_str(exc) # 1.89μs -> 1.24μs (51.8% faster)

def test_http_exception_with_detail_set_to_false():
    # HTTPException with detail set to False (should fallback to str(e))
    exc = HTTPException(status_code=400, detail=False)
    codeflash_output = get_error_message_str(exc) # 3.22μs -> 2.45μs (31.1% faster)

def test_http_exception_with_detail_set_to_true():
    # HTTPException with detail set to True (should fallback to str(e))
    exc = HTTPException(status_code=400, detail=True)
    codeflash_output = get_error_message_str(exc) # 2.84μs -> 1.85μs (53.5% faster)

# ---------------------------
# Large Scale Test Cases
# ---------------------------

def test_http_exception_with_large_dict_detail():
    # HTTPException with a large dict detail
    large_detail = {f"key_{i}": f"value_{i}" for i in range(1000)}
    exc = HTTPException(status_code=500, detail=large_detail)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 212μs -> 121μs (74.6% faster)

def test_http_exception_with_large_string_detail():
    # HTTPException with a large string detail
    large_str = "A" * 1000
    exc = HTTPException(status_code=413, detail=large_str)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 959ns -> 687ns (39.6% faster)

def test_regular_exception_with_large_message():
    # Regular Exception with a large string message
    large_str = "B" * 1000
    exc = Exception(large_str)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 1.35μs -> 1.07μs (26.6% faster)

def test_http_exception_with_large_message_attribute_dict():
    # HTTPException with a large dict as 'message' attribute
    class CustomHTTPException(HTTPException):
        def __init__(self, status_code, detail, message):
            super().__init__(status_code=status_code, detail=detail)
            self.message = message
    large_message = {f"field_{i}": i for i in range(1000)}
    exc = CustomHTTPException(status_code=500, detail=None, message=large_message)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 1.09μs -> 705ns (55.3% faster)

def test_http_exception_with_large_message_attribute_str():
    # HTTPException with a large string as 'message' attribute
    class CustomHTTPException(HTTPException):
        def __init__(self, status_code, detail, message):
            super().__init__(status_code=status_code, detail=detail)
            self.message = message
    large_message = "X" * 1000
    exc = CustomHTTPException(status_code=500, detail=None, message=large_message)
    codeflash_output = get_error_message_str(exc); result = codeflash_output # 991ns -> 762ns (30.1% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-get_error_message_str-mhbo48eo and push.

Codeflash

The optimized code achieves a **61% speedup** through two key optimizations:

**1. Early Returns Eliminate Redundant Operations**
The original code assigns values to `error_message` and then returns it at the end, requiring the function to continue executing through all remaining lines. The optimized version uses direct `return` statements, immediately exiting the function once a condition is met. This eliminates unnecessary variable assignments and subsequent line executions.

**2. Cached Attribute Access**
The original code accesses `e.detail` multiple times in the isinstance checks (`isinstance(e.detail, str)`, `isinstance(e.detail, dict)`). The optimized version caches this with `detail = e.detail` and reuses the cached value, reducing attribute lookup overhead.

**Performance Impact by Test Case:**
- **Large dict serialization cases**: 68-75% faster - Early returns prevent unnecessary operations after `json.dumps()` completes
- **Simple string cases**: 25-45% faster - Direct returns avoid variable assignment overhead
- **Edge cases with fallbacks**: 20-55% faster - Early exits prevent evaluating remaining conditions

The optimizations are particularly effective for the most common paths (string details, dict details) and scale well with data size, as seen in the large-scale test cases where the benefits of early returns are most pronounced.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 29, 2025 07:22
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant