Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 11% (0.11x) speedup for can_modify_guardrails in litellm/proxy/guardrails/guardrail_helpers.py

⏱️ Runtime : 892 microseconds 801 microseconds (best of 206 runs)

📝 Explanation and details

The optimized code achieves an 11% speedup through three key optimizations that reduce redundant dictionary operations:

What was optimized:

  1. Eliminated redundant metadata access: Changed team_obj.metadata or {} to direct team_obj.metadata access with explicit null checking, avoiding unnecessary empty dict creation.
  2. Cached guardrails lookup: Stored team_metadata.get("guardrails") in a variable instead of calling .get() multiple times on the same key.
  3. Replaced nested .get() with direct key access: Changed team_metadata.get("guardrails", {}).get("modify_guardrails", None) to "modify_guardrails" in guardrails and guardrails["modify_guardrails"], eliminating the expensive nested dictionary lookup pattern.

Why this is faster:
The original code's main bottleneck (49.4% of total time) was the repeated .get("guardrails") calls. Dictionary .get() operations are slower than direct key access (in operator + dict lookup), and the original code was performing up to 3 separate .get() calls on the same "guardrails" key. The optimized version reduces this to a single lookup cached in a variable.

Test case performance:

  • Biggest gains (20-40% faster): Cases with None/empty metadata benefit from avoiding the or {} operation
  • Consistent improvements (5-15% faster): Cases with guardrails dictionaries benefit from cached lookups and direct key access
  • Minimal overhead: Simple cases like team_obj is None show negligible performance impact

The optimization is particularly effective for the common code paths involving guardrails validation while maintaining identical behavior.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 2054 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest
from litellm.proxy.guardrails.guardrail_helpers import can_modify_guardrails


# function to test
class LiteLLM_TeamTable:
    def __init__(self, metadata=None):
        self.metadata = metadata
from litellm.proxy.guardrails.guardrail_helpers import can_modify_guardrails

# unit tests

# --- Basic Test Cases ---

def test_none_team_obj_returns_true():
    # Should allow modification if team_obj is None
    codeflash_output = can_modify_guardrails(None) # 333ns -> 398ns (16.3% slower)

def test_team_obj_with_no_metadata_returns_true():
    # Should allow modification if metadata is None
    team = LiteLLM_TeamTable(metadata=None)
    codeflash_output = can_modify_guardrails(team) # 704ns -> 548ns (28.5% faster)

def test_team_obj_with_empty_metadata_returns_true():
    # Should allow modification if metadata is empty dict
    team = LiteLLM_TeamTable(metadata={})
    codeflash_output = can_modify_guardrails(team) # 730ns -> 515ns (41.7% faster)

def test_team_obj_with_guardrails_not_dict_returns_true():
    # Should allow modification if guardrails is not a dict
    team = LiteLLM_TeamTable(metadata={"guardrails": "not_a_dict"})
    codeflash_output = can_modify_guardrails(team) # 2.12μs -> 2.22μs (4.46% slower)

def test_team_obj_with_guardrails_dict_without_modify_guardrails_returns_true():
    # Should allow modification if guardrails is dict but no modify_guardrails key
    team = LiteLLM_TeamTable(metadata={"guardrails": {}})
    codeflash_output = can_modify_guardrails(team) # 2.25μs -> 2.06μs (9.20% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_true_returns_true():
    # Should allow modification if modify_guardrails is True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": True}})
    codeflash_output = can_modify_guardrails(team) # 2.18μs -> 2.17μs (0.600% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_false_returns_false():
    # Should NOT allow modification if modify_guardrails is False
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": False}})
    codeflash_output = can_modify_guardrails(team) # 2.11μs -> 2.01μs (5.03% faster)

# --- Edge Test Cases ---

def test_team_obj_with_guardrails_none_returns_true():
    # Should allow modification if guardrails is explicitly None
    team = LiteLLM_TeamTable(metadata={"guardrails": None})
    codeflash_output = can_modify_guardrails(team) # 669ns -> 626ns (6.87% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_none_returns_true():
    # Should allow modification if modify_guardrails is None
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": None}})
    codeflash_output = can_modify_guardrails(team) # 2.24μs -> 2.06μs (8.78% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_missing_returns_true():
    # Should allow modification if modify_guardrails key is missing
    team = LiteLLM_TeamTable(metadata={"guardrails": {}})
    codeflash_output = can_modify_guardrails(team) # 2.13μs -> 1.99μs (7.04% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_non_bool_returns_true():
    # Should allow modification if modify_guardrails is not False (e.g. string, int, etc.)
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": "no"}})
    codeflash_output = can_modify_guardrails(team) # 2.10μs -> 2.04μs (3.24% faster)
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": 0}})
    codeflash_output = can_modify_guardrails(team) # 988ns -> 934ns (5.78% faster)
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": []}})
    codeflash_output = can_modify_guardrails(team) # 640ns -> 600ns (6.67% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_false_and_other_keys_returns_false():
    # Should NOT allow modification if modify_guardrails is False, even with other keys present
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": False, "other": 123}})
    codeflash_output = can_modify_guardrails(team) # 1.93μs -> 1.88μs (2.82% faster)

def test_team_obj_with_metadata_extra_keys_returns_true():
    # Should allow modification if unrelated keys are present
    team = LiteLLM_TeamTable(metadata={"foo": "bar"})
    codeflash_output = can_modify_guardrails(team) # 608ns -> 652ns (6.75% slower)

def test_team_obj_with_guardrails_as_empty_list_returns_true():
    # Should allow modification if guardrails is an empty list (not dict)
    team = LiteLLM_TeamTable(metadata={"guardrails": []})
    codeflash_output = can_modify_guardrails(team) # 1.92μs -> 1.91μs (0.366% faster)

def test_team_obj_with_guardrails_as_int_returns_true():
    # Should allow modification if guardrails is an int (not dict)
    team = LiteLLM_TeamTable(metadata={"guardrails": 123})
    codeflash_output = can_modify_guardrails(team) # 1.88μs -> 1.87μs (0.267% faster)

# --- Large Scale Test Cases ---

def test_large_metadata_dict_with_guardrails_false_returns_false():
    # Large metadata dict with guardrails.modify_guardrails = False
    metadata = {f"key_{i}": i for i in range(500)}
    metadata["guardrails"] = {"modify_guardrails": False}
    team = LiteLLM_TeamTable(metadata=metadata)
    codeflash_output = can_modify_guardrails(team) # 2.22μs -> 2.15μs (3.40% faster)

def test_large_metadata_dict_with_guardrails_true_returns_true():
    # Large metadata dict with guardrails.modify_guardrails = True
    metadata = {f"key_{i}": i for i in range(500)}
    metadata["guardrails"] = {"modify_guardrails": True}
    team = LiteLLM_TeamTable(metadata=metadata)
    codeflash_output = can_modify_guardrails(team) # 2.32μs -> 2.09μs (10.8% faster)

def test_large_metadata_dict_with_guardrails_missing_returns_true():
    # Large metadata dict with no guardrails key
    metadata = {f"key_{i}": i for i in range(999)}
    team = LiteLLM_TeamTable(metadata=metadata)
    codeflash_output = can_modify_guardrails(team) # 693ns -> 703ns (1.42% slower)

def test_large_guardrails_dict_with_modify_guardrails_false_returns_false():
    # Large guardrails dict with modify_guardrails = False
    guardrails = {f"gkey_{i}": i for i in range(500)}
    guardrails["modify_guardrails"] = False
    team = LiteLLM_TeamTable(metadata={"guardrails": guardrails})
    codeflash_output = can_modify_guardrails(team) # 2.30μs -> 2.34μs (1.62% slower)

def test_large_guardrails_dict_with_modify_guardrails_true_returns_true():
    # Large guardrails dict with modify_guardrails = True
    guardrails = {f"gkey_{i}": i for i in range(500)}
    guardrails["modify_guardrails"] = True
    team = LiteLLM_TeamTable(metadata={"guardrails": guardrails})
    codeflash_output = can_modify_guardrails(team) # 2.25μs -> 2.20μs (2.13% faster)

def test_team_obj_with_large_metadata_and_non_dict_guardrails_returns_true():
    # Large metadata dict, guardrails is a string
    metadata = {f"key_{i}": i for i in range(999)}
    metadata["guardrails"] = "not_a_dict"
    team = LiteLLM_TeamTable(metadata=metadata)
    codeflash_output = can_modify_guardrails(team) # 2.06μs -> 1.97μs (4.67% faster)

# --- Mutation Detection / Negative Test ---

def test_mutation_detection_modify_guardrails_false_returns_false():
    # If implementation is changed so that modify_guardrails=False returns True, this test will fail.
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": False}})
    codeflash_output = can_modify_guardrails(team) # 2.15μs -> 1.96μs (10.1% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import pytest  # used for our unit tests
from litellm.proxy.guardrails.guardrail_helpers import can_modify_guardrails


# function to test
# (copied from above for completeness)
class LiteLLM_TeamTable:
    def __init__(self, metadata=None):
        self.metadata = metadata
from litellm.proxy.guardrails.guardrail_helpers import can_modify_guardrails

# unit tests

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

def test_none_team_obj_returns_true():
    # If team_obj is None, should always return True
    codeflash_output = can_modify_guardrails(None) # 333ns -> 388ns (14.2% slower)

def test_team_obj_with_no_metadata_returns_true():
    # If team_obj exists but metadata is None, should return True
    team = LiteLLM_TeamTable(metadata=None)
    codeflash_output = can_modify_guardrails(team) # 748ns -> 540ns (38.5% faster)

def test_team_obj_with_empty_metadata_returns_true():
    # If team_obj metadata is empty dict, should return True
    team = LiteLLM_TeamTable(metadata={})
    codeflash_output = can_modify_guardrails(team) # 752ns -> 560ns (34.3% faster)

def test_team_obj_with_guardrails_not_dict_returns_true():
    # If guardrails is present but not a dict, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": "not_a_dict"})
    codeflash_output = can_modify_guardrails(team) # 2.44μs -> 2.23μs (9.27% faster)

def test_team_obj_with_guardrails_dict_no_modify_guardrails_returns_true():
    # If guardrails is a dict but modify_guardrails key is missing, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {}})
    codeflash_output = can_modify_guardrails(team) # 2.22μs -> 2.10μs (6.06% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_true_returns_true():
    # If modify_guardrails is True, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": True}})
    codeflash_output = can_modify_guardrails(team) # 2.23μs -> 2.17μs (2.58% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_false_returns_false():
    # If modify_guardrails is False, should return False
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": False}})
    codeflash_output = can_modify_guardrails(team) # 2.08μs -> 2.09μs (0.478% slower)

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

def test_team_obj_with_guardrails_dict_modify_guardrails_none_returns_true():
    # If modify_guardrails is None, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": None}})
    codeflash_output = can_modify_guardrails(team) # 2.11μs -> 2.04μs (3.08% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_missing_returns_true():
    # If modify_guardrails key is missing, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"other_key": 123}})
    codeflash_output = can_modify_guardrails(team) # 1.97μs -> 1.90μs (3.79% faster)

def test_team_obj_with_guardrails_none_returns_true():
    # If guardrails is explicitly None, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": None})
    codeflash_output = can_modify_guardrails(team) # 654ns -> 673ns (2.82% slower)

def test_team_obj_with_guardrails_dict_modify_guardrails_false_and_other_keys():
    # If modify_guardrails is False but other keys exist, should still return False
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": False, "foo": "bar"}})
    codeflash_output = can_modify_guardrails(team) # 2.17μs -> 1.96μs (10.5% faster)


def test_team_obj_with_guardrails_dict_modify_guardrails_string_returns_true():
    # If modify_guardrails is a string, should return True (only False disables)
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": "False"}})
    codeflash_output = can_modify_guardrails(team) # 2.82μs -> 2.65μs (6.41% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_zero_returns_true():
    # If modify_guardrails is 0, should return True (only False disables)
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": 0}})
    codeflash_output = can_modify_guardrails(team) # 2.27μs -> 2.18μs (4.22% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_empty_list_returns_true():
    # If modify_guardrails is an empty list, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": []}})
    codeflash_output = can_modify_guardrails(team) # 2.20μs -> 2.13μs (3.33% faster)

def test_team_obj_with_guardrails_dict_modify_guardrails_false_case_sensitive():
    # If modify_guardrails is "false" (string), should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": "false"}})
    codeflash_output = can_modify_guardrails(team) # 2.09μs -> 2.07μs (0.820% faster)

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

def test_large_metadata_dict_returns_true():
    # Large metadata dict with no guardrails key
    large_metadata = {f"key_{i}": i for i in range(500)}
    team = LiteLLM_TeamTable(metadata=large_metadata)
    codeflash_output = can_modify_guardrails(team) # 626ns -> 732ns (14.5% slower)

def test_large_guardrails_dict_modify_guardrails_false_returns_false():
    # Large guardrails dict with modify_guardrails set to False
    large_guardrails = {f"key_{i}": i for i in range(500)}
    large_guardrails["modify_guardrails"] = False
    team = LiteLLM_TeamTable(metadata={"guardrails": large_guardrails})
    codeflash_output = can_modify_guardrails(team) # 2.36μs -> 2.34μs (0.512% faster)

def test_large_guardrails_dict_modify_guardrails_true_returns_true():
    # Large guardrails dict with modify_guardrails set to True
    large_guardrails = {f"key_{i}": i for i in range(500)}
    large_guardrails["modify_guardrails"] = True
    team = LiteLLM_TeamTable(metadata={"guardrails": large_guardrails})
    codeflash_output = can_modify_guardrails(team) # 2.28μs -> 2.21μs (2.89% faster)

def test_many_teams_with_various_settings():
    # Test 1000 teams, half with modify_guardrails False, half True
    teams = []
    for i in range(1000):
        if i % 2 == 0:
            guardrails = {"modify_guardrails": False}
            expected = False
        else:
            guardrails = {"modify_guardrails": True}
            expected = True
        team = LiteLLM_TeamTable(metadata={"guardrails": guardrails})
        teams.append((team, expected))
    # Check all teams
    for team, expected in teams:
        codeflash_output = can_modify_guardrails(team) # 617μs -> 555μs (11.2% faster)

def test_large_team_obj_list_none_and_empty_metadata():
    # Test a mix of None and empty metadata for 1000 teams
    teams = []
    for i in range(1000):
        if i % 2 == 0:
            team = None
        else:
            team = LiteLLM_TeamTable(metadata={})
        teams.append(team)
    for team in teams:
        codeflash_output = can_modify_guardrails(team) # 190μs -> 164μs (15.4% faster)

# ----------- Mutation-sensitive Test Cases -----------

def test_modify_guardrails_false_only_returns_false():
    # Only False disables modification, all other values must return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": False}})
    codeflash_output = can_modify_guardrails(team) # 2.33μs -> 2.29μs (1.92% faster)
    for value in [None, True, "False", 0, [], {}, "false", "TRUE"]:
        team = LiteLLM_TeamTable(metadata={"guardrails": {"modify_guardrails": value}})
        codeflash_output = can_modify_guardrails(team) # 5.07μs -> 4.75μs (6.76% faster)

def test_guardrails_key_missing_returns_true():
    # If guardrails key is missing, should return True
    team = LiteLLM_TeamTable(metadata={"other_key": 123})
    codeflash_output = can_modify_guardrails(team) # 614ns -> 657ns (6.54% slower)

def test_guardrails_is_empty_dict_returns_true():
    # If guardrails is an empty dict, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {}})
    codeflash_output = can_modify_guardrails(team) # 2.16μs -> 1.99μs (8.33% faster)

def test_guardrails_is_dict_with_unrelated_keys_returns_true():
    # If guardrails dict has unrelated keys, should return True
    team = LiteLLM_TeamTable(metadata={"guardrails": {"foo": "bar"}})
    codeflash_output = can_modify_guardrails(team) # 2.04μs -> 1.89μs (7.73% 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-can_modify_guardrails-mhbrwsuj and push.

Codeflash

The optimized code achieves an 11% speedup through three key optimizations that reduce redundant dictionary operations:

**What was optimized:**
1. **Eliminated redundant metadata access**: Changed `team_obj.metadata or {}` to direct `team_obj.metadata` access with explicit null checking, avoiding unnecessary empty dict creation.
2. **Cached guardrails lookup**: Stored `team_metadata.get("guardrails")` in a variable instead of calling `.get()` multiple times on the same key.
3. **Replaced nested .get() with direct key access**: Changed `team_metadata.get("guardrails", {}).get("modify_guardrails", None)` to `"modify_guardrails" in guardrails and guardrails["modify_guardrails"]`, eliminating the expensive nested dictionary lookup pattern.

**Why this is faster:**
The original code's main bottleneck (49.4% of total time) was the repeated `.get("guardrails")` calls. Dictionary `.get()` operations are slower than direct key access (`in` operator + dict lookup), and the original code was performing up to 3 separate `.get()` calls on the same "guardrails" key. The optimized version reduces this to a single lookup cached in a variable.

**Test case performance:**
- **Biggest gains** (20-40% faster): Cases with None/empty metadata benefit from avoiding the `or {}` operation
- **Consistent improvements** (5-15% faster): Cases with guardrails dictionaries benefit from cached lookups and direct key access
- **Minimal overhead**: Simple cases like `team_obj is None` show negligible performance impact

The optimization is particularly effective for the common code paths involving guardrails validation while maintaining identical behavior.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 29, 2025 09:08
@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