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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
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.pyindex 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_hookfrom 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.pyindex 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 selefrom litellm.llms.anthropic.experimental_pass_through.messages import handler as mainfrom litellm.rust_bridge.catalog import Context, Delivery, Route-from litellm.rust_bridge.dispatch import PublicDispatch+from litellm.rust_bridge.dispatch import PublicDispatch, call_hookfrom 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.pyindex 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 OCRResponsefrom litellm.ocr import mainfrom litellm.ocr.main import convert_file_document_to_url_document, get_mime_typefrom litellm.rust_bridge.catalog import Context, Route-from litellm.rust_bridge.dispatch import PublicDispatch+from litellm.rust_bridge.dispatch import PublicDispatch, call_hookfrom 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: prkwargs,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.pyindex 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 selefrom litellm.responses import mainfrom litellm.responses.streaming_iterator import BaseResponsesAPIStreamingIteratorfrom litellm.rust_bridge.catalog import Context, Delivery, Route-from litellm.rust_bridge.dispatch import PublicDispatch+from litellm.rust_bridge.dispatch import PublicDispatch, call_hookfrom litellm.rust_bridge.public_call import bind, optional_bool, optional_mapping, optional_str, signaturefrom 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: # kwakwargs,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.pyindex 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]):