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]

refactor(rust_bridge): share call_hook instead of per-route native lambdas

Yujong Lee6d300065927f
diff --git a/litellm/chat_completions/dispatch.py b/litellm/chat_completions/dispatch.py
index 37e188bc0c5..a8e34943d37 100644
--- a/litellm/chat_completions/dispatch.py
+++ b/litellm/chat_completions/dispatch.py
@@ -10,7 +10,7 @@ from litellm.rust_bridge.chat_completions.entrypoints import (
NATIVE_COMPLETION,
LiteLLMChatCompletionsRequest,
)
-from litellm.rust_bridge.dispatch import PublicDispatch
+from litellm.rust_bridge.dispatch import PublicDispatch, call_hook
from litellm.rust_bridge.public_call import (
bind,
optional_bool,
@@ -103,7 +103,7 @@ def completion(
kwargs,
python=python,
binding=NATIVE_COMPLETION,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
@@ -114,7 +114,7 @@ async def acompletion(*args: object, **kwargs: object) -> ChatResult: # kwargs-
kwargs,
python=python,
binding=NATIVE_ACOMPLETION,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
diff --git a/litellm/messages/dispatch.py b/litellm/messages/dispatch.py
index 61fb869ba8e..c463999bae9 100644
--- a/litellm/messages/dispatch.py
+++ b/litellm/messages/dispatch.py
@@ -5,7 +5,7 @@ from typing import Final, TypeAlias, cast # noqa: TID251 # native binding sele
from litellm.llms.anthropic.experimental_pass_through.messages import handler as main
from litellm.rust_bridge.catalog import Context, Delivery, Route
-from litellm.rust_bridge.dispatch import PublicDispatch
+from litellm.rust_bridge.dispatch import PublicDispatch, call_hook
from litellm.rust_bridge.messages.entrypoints import (
NATIVE_AMESSAGES,
NATIVE_MESSAGES,
@@ -102,7 +102,7 @@ def anthropic_messages_handler(
kwargs,
python=python,
binding=NATIVE_MESSAGES,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
@@ -113,7 +113,7 @@ async def anthropic_messages(*args: object, **kwargs: object) -> MessagesResult:
kwargs,
python=python,
binding=NATIVE_AMESSAGES,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
diff --git a/litellm/ocr/dispatch.py b/litellm/ocr/dispatch.py
index f5690a917f4..4d530f82331 100644
--- a/litellm/ocr/dispatch.py
+++ b/litellm/ocr/dispatch.py
@@ -7,7 +7,7 @@ from litellm.llms.base_llm.ocr.transformation import OCRResponse
from litellm.ocr import main
from litellm.ocr.main import convert_file_document_to_url_document, get_mime_type
from litellm.rust_bridge.catalog import Context, Route
-from litellm.rust_bridge.dispatch import PublicDispatch
+from litellm.rust_bridge.dispatch import PublicDispatch, call_hook
from litellm.rust_bridge.ocr.entrypoints import NATIVE_AOCR, NATIVE_OCR, LiteLLMOcrRequest
__all__ = ("aocr", "convert_file_document_to_url_document", "get_mime_type", "ocr")
@@ -77,7 +77,7 @@ def ocr(
kwargs,
python=_PYTHON_OCR,
binding=NATIVE_OCR,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
@@ -87,5 +87,5 @@ async def aocr(*args: object, **kwargs: object) -> OCRResponse: # kwargs-ok: pr
kwargs,
python=_PYTHON_AOCR,
binding=NATIVE_AOCR,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
diff --git a/litellm/responses/dispatch.py b/litellm/responses/dispatch.py
index f7418df9886..60ea7ff291a 100644
--- a/litellm/responses/dispatch.py
+++ b/litellm/responses/dispatch.py
@@ -6,7 +6,7 @@ from typing import Final, TypeAlias, cast # noqa: TID251 # native binding sele
from litellm.responses import main
from litellm.responses.streaming_iterator import BaseResponsesAPIStreamingIterator
from litellm.rust_bridge.catalog import Context, Delivery, Route
-from litellm.rust_bridge.dispatch import PublicDispatch
+from litellm.rust_bridge.dispatch import PublicDispatch, call_hook
from litellm.rust_bridge.public_call import bind, optional_bool, optional_mapping, optional_str, signature
from litellm.rust_bridge.responses.entrypoints import (
NATIVE_ARESPONSES,
@@ -95,7 +95,7 @@ def responses(
kwargs,
python=python,
binding=NATIVE_RESPONSES,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
@@ -106,7 +106,7 @@ async def aresponses(*args: object, **kwargs: object) -> ResponsesResult: # kwa
kwargs,
python=python,
binding=NATIVE_ARESPONSES,
- native=lambda hook, request, call_args, call_kwargs: hook(request, call_args, call_kwargs),
+ native=call_hook,
)
diff --git a/litellm/rust_bridge/dispatch.py b/litellm/rust_bridge/dispatch.py
index 5cc1471eaf0..7ddc903df58 100644
--- a/litellm/rust_bridge/dispatch.py
+++ b/litellm/rust_bridge/dispatch.py
@@ -14,6 +14,17 @@ RequestT = TypeVar("RequestT")
NativeT = TypeVar("NativeT")
ResultT = TypeVar("ResultT")
+NativeHook = Callable[[RequestT, tuple[object, ...], Mapping[str, object]], ResultT]
+
+
+def call_hook(
+ hook: NativeHook[RequestT, ResultT],
+ request: RequestT,
+ args: tuple[object, ...],
+ kwargs: Mapping[str, object],
+) -> ResultT:
+ return hook(request, args, kwargs)
+
@dataclass(frozen=True, slots=True)
class PublicDispatch(Generic[RequestT]):