Files
litellm/tests/code_coverage_tests/test_chat_completion_imports.py
T
Alexsander Hamir 59409429d4 fix: reduced __inits__ overhead in 7% (#14689)
* fix: avoid redundant __init__ calls on hot path

Previously, imports on the request hot path caused __init__ to run
excessively for every request. This change ensures initialization
happens once, reducing cpu overhead.

* fix: remove redundant __init__ import

The current implementation no longer requires an import at the top of the function.

* fix: placed on core utils for future reuse

* test: add coverage & remove inline import

A general import-checking tool across all endpoints would be a large PR.
This commit focuses on a smaller, targeted fix for the discussed case.

* added import check to CI
2025-09-18 17:18:05 -07:00

43 lines
1.6 KiB
Python

## Tests that chat_completion endpoint has no imports inside function bodies
## This is critical for performance optimization in the hot path
import ast
from pathlib import Path
def test_chat_completion_no_imports():
"""Test that chat_completion endpoint has no imports in function bodies."""
# Path to the proxy server file
proxy_server_path = Path(__file__).parent.parent.parent / "litellm" / "proxy" / "proxy_server.py"
with open(proxy_server_path, 'r') as f:
content = f.read()
# Parse the AST
tree = ast.parse(content)
# Find the chat_completion function
chat_completion_func = None
for node in ast.walk(tree):
if (isinstance(node, ast.AsyncFunctionDef) and node.name == "chat_completion"):
chat_completion_func = node
break
assert chat_completion_func is not None, "chat_completion function not found"
# Check for imports inside the function body
import_violations = []
for node in ast.walk(chat_completion_func):
if isinstance(node, (ast.Import, ast.ImportFrom)):
# Get line number
line_num = node.lineno
import_violations.append(line_num)
# Assert no import violations found
if import_violations:
print(f"Found {len(import_violations)} import violations in chat_completion endpoint:")
for line_num in import_violations:
print(f" - Line {line_num}: Import statement found")
print("\nchat_completion endpoint should not contain imports for optimal performance.")
raise Exception("Import violations found in chat_completion endpoint")