Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): register SkillsInjectionHook at proxy startup instead of import time

mateo-berrif0a2a2312704
diff --git a/litellm/proxy/hooks/litellm_skills/__init__.py b/litellm/proxy/hooks/litellm_skills/__init__.py
index 751122ac51c..d24cdd37161 100644
--- a/litellm/proxy/hooks/litellm_skills/__init__.py
+++ b/litellm/proxy/hooks/litellm_skills/__init__.py
@@ -21,10 +21,7 @@ from litellm.llms.litellm_proxy.skills import (
code_execution_handler,
get_litellm_code_execution_tool,
)
-from litellm.proxy.hooks.litellm_skills.main import (
- SkillsInjectionHook,
- skills_injection_hook,
-)
+from litellm.proxy.hooks.litellm_skills.main import SkillsInjectionHook
__all__ = [
"LITELLM_CODE_EXECUTION_TOOL",
@@ -35,5 +32,4 @@ __all__ = [
"SkillsSandboxExecutor",
"code_execution_handler",
"get_litellm_code_execution_tool",
- "skills_injection_hook",
]
diff --git a/litellm/proxy/hooks/litellm_skills/main.py b/litellm/proxy/hooks/litellm_skills/main.py
index 569ec32c1a0..c5e8f03f792 100644
--- a/litellm/proxy/hooks/litellm_skills/main.py
+++ b/litellm/proxy/hooks/litellm_skills/main.py
@@ -29,6 +29,7 @@ import json
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any, Final, Protocol
+import litellm
from litellm._logging import verbose_proxy_logger
from litellm.caching.caching import DualCache
from litellm.integrations.custom_logger import CustomLogger
@@ -475,7 +476,6 @@ class SkillsInjectionHook(CustomLogger):
Returns the final response with generated files inline.
"""
- import litellm
from litellm.llms.litellm_proxy.skills.code_execution import (
LiteLLMInternalTools,
)
@@ -705,7 +705,6 @@ print('No executable skill module found')
Returns the final response with generated files inline.
"""
- import litellm
from litellm.llms.litellm_proxy.skills.code_execution import (
LiteLLMInternalTools,
)
@@ -894,11 +893,3 @@ print('No executable skill module found')
verbose_proxy_logger.debug("SkillsInjectionHook: Attached %s files to response", len(generated_files))
return response
-
-
-# Global instance for registration
-skills_injection_hook: Final = SkillsInjectionHook()
-
-import litellm
-
-litellm.logging_callback_manager.add_litellm_callback(skills_injection_hook)
diff --git a/tests/test_litellm/proxy/hooks/litellm_skills/test_main.py b/tests/test_litellm/proxy/hooks/litellm_skills/test_main.py
index f716a8533d8..c037f60ac25 100644
--- a/tests/test_litellm/proxy/hooks/litellm_skills/test_main.py
+++ b/tests/test_litellm/proxy/hooks/litellm_skills/test_main.py
@@ -1,3 +1,6 @@
+import subprocess
+import sys
+from typing import Final
from unittest.mock import AsyncMock, patch
import pytest
@@ -65,3 +68,16 @@ async def test_execute_code_loop_dispatches_litellm_skill_tool():
mock_exec.assert_awaited_once()
assert mock_exec.await_args.args[0] == SKILL_TOOL_NAME
assert result is final_response
+
+
+def test_importing_proxy_hooks_does_not_mutate_litellm_callbacks():
+ script: Final = (
+ "import litellm; "
+ "litellm.callbacks = []; "
+ "import litellm.proxy.hooks; "
+ "assert len(litellm.callbacks) == 0, f'mutated: {litellm.callbacks}'"
+ )
+ result: Final = subprocess.run(
+ [sys.executable, "-c", script], capture_output=True, text=True
+ )
+ assert result.returncode == 0, f"stdout: {result.stdout}\nstderr: {result.stderr}"