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
Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_gigachat_passthrough_25886
mateo-berrieb00986f1831
diff --cc litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.pyindex d3df490d43c,525e7099b89..1d28ad1914f--- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py+++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py@@@ -2810,238 -2796,6 +2810,238 @@@ def create_generic_websocket_passthroug)[email protected]_route(+ "/gigachat/{endpoint:path}",+ methods=["GET", "POST", "PUT", "DELETE", "PATCH"], # mutable-ok: FastAPI route methods+ tags=["Gigachat Pass-through", "pass-through"], # mutable-ok: FastAPI route tags+)+async def gigachat_proxy_route(+ endpoint: str,+ request: Request,+ fastapi_response: Response,+ user_api_key_dict: Annotated[UserAPIKeyAuth, Depends(user_api_key_auth)],- ):++) -> Response:+ """+ [Docs](https://docs.litellm.ai/docs/pass_through/gigachat)+ """+ from litellm.proxy.proxy_server import (+ general_settings,+ llm_router,+ proxy_config,+ proxy_logging_obj,+ select_data_generator,+ user_api_base,+ user_max_tokens,+ user_model,+ user_request_timeout,+ user_temperature,+ version,+ )++ ## check for streaming+ request_body: Final[dict[str, object]] = await get_request_body(request)+ is_router_model = False # rebind-ok: conditionally set to True when model uses router++ raw_model: Final = request_body.get("model")+ model: Final = raw_model if isinstance(raw_model, str) else None+ if model:+ is_router_model = is_passthrough_request_using_router_model(+ request_body, llm_router+ ) # rebind-ok: conditionally set to True+ elif any(word in endpoint for word in ("completions", "embeddings")):+ raise HTTPException(+ status_code=400, detail={"error": "Model is required in request body"}+ ) # mutable-ok: HTTPException detail dict++ # If router model, use dedicated router passthrough handler+ # This uses the same common processing path as non-router models+ if model and is_router_model and llm_router:+ return await handle_gigachat_passthrough_router_model(+ model=model,+ endpoint=endpoint,+ request=request,+ request_body=request_body,+ fastapi_response=fastapi_response,+ llm_router=llm_router,+ user_api_key_dict=user_api_key_dict,+ proxy_logging_obj=proxy_logging_obj,+ general_settings=general_settings,+ proxy_config=proxy_config,+ select_data_generator=select_data_generator,+ user_model=user_model,+ user_temperature=user_temperature,+ user_request_timeout=user_request_timeout,+ user_max_tokens=user_max_tokens,+ user_api_base=user_api_base,+ version=version,+ )++ verbose_proxy_logger.debug(+ "Gigachat passthrough: Using direct Gigachat model '%s' for endpoint '%s'", model, endpoint+ )++ from litellm.llms.gigachat.authenticator import get_access_token+ from litellm.llms.gigachat.utils import GIGACHAT_BASE_URL++ base_target_url: Final = get_secret_str("GIGACHAT_API_BASE") or GIGACHAT_BASE_URL+ request_path: Final = httpx.URL(endpoint).path+ encoded_endpoint: Final = request_path if request_path.startswith("/") else f"/{request_path}"++ base_url: Final = httpx.URL(base_target_url)+ updated_url: Final = base_url.copy_with(+ path=HttpPassThroughEndpointHelpers.join_base_and_endpoint_path(base_url, encoded_endpoint)+ )++ is_streaming_request: Final = await is_streaming_request_fn(request)++ endpoint_func: Final = create_pass_through_route(+ endpoint=endpoint,+ target=str(updated_url),+ custom_headers={"Authorization": f"Bearer {get_access_token()}"},+ is_streaming_request=is_streaming_request,+ )+ return await endpoint_func(+ request,+ fastapi_response,+ user_api_key_dict,+ )+++async def handle_gigachat_passthrough_router_model(+ model: str,+ endpoint: str,+ request: Request,+ request_body: dict,+ fastapi_response: Response,+ llm_router: litellm.Router,+ user_api_key_dict: UserAPIKeyAuth,+ proxy_logging_obj: ProxyLoggingType,+ general_settings: dict,+ proxy_config: ProxyConfig,+ select_data_generator: Callable,+ user_model: str | None,+ user_temperature: float | None,+ user_request_timeout: float | None,+ user_max_tokens: int | None,+ user_api_base: str | None,+ version: str | None,+) -> Response | StreamingResponse:+ """+ Handle Gigachat passthrough for router models (models defined in config.yaml).++ Uses the same common processing path as non-router models to ensure+ metadata and hooks are properly initialized.++ Args:+ model: The router model name (e.g., "gigachat/gigachat-2")+ endpoint: The Gigachat endpoint path (e.g., "/chat/completions")+ request: The FastAPI request object+ request_body: The parsed request body+ llm_router: The LiteLLM router instance+ user_api_key_dict: The user API key authentication dictionary+ proxy_logging_obj: Proxy logging+ general_settings: Proxy general settings+ proxy_config: Proxy config+ select_data_generator: Select data generator function+ (additional args for common processing)++ Returns:+ Response or StreamingResponse depending on endpoint type+ """+ from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing++ # Detect streaming based on request body+ is_streaming: Final = request_body.get("stream", False) # pyright: ignore[reportUnknownVariableType] # request_body is dict[Unknown, Unknown]++ data: dict[str, Any] = await _read_request_body(+ request=request+ ) # mutable-ok: mutated in place by proxy pipeline; pyright: ignore[reportExplicitAny] # Any needed for proxy pipeline+ if user_api_key_dict is not None:+ auth_metadata: Final = {+ metadata_key: value+ for metadata_key, value in (+ ("user_api_key_user_id", getattr(user_api_key_dict, "user_id", None)),+ ("user_api_key_team_id", getattr(user_api_key_dict, "team_id", None)),+ ("user_api_key_org_id", getattr(user_api_key_dict, "org_id", None)),+ ("agent_id", getattr(user_api_key_dict, "agent_id", None)),+ )+ if value is not None+ }+ existing_metadata: Final = data.get("metadata")+ data["metadata"] = {+ **(existing_metadata if isinstance(existing_metadata, dict) else {}),+ **auth_metadata,+ }++ verbose_proxy_logger.debug(+ "Gigachat router passthrough: model='%s', endpoint='%s', streaming=%s", model, endpoint, is_streaming+ )++ # Use the common processing path (same as non-router models)+ # This ensures all metadata, hooks, and logging are properly initialized++ data["model"] = model+ data["method"] = request.method+ data["endpoint"] = endpoint+ data["json"] = request_body+ data["custom_llm_provider"] = "gigachat"++ # Remove sensitive keys from data+ keys: Final = [ # mutable-ok: list of keys to remove from data+ "gigachat_auth_url",+ "gigachat_access_token",+ "gigachat_scope",+ "api_base",+ "api_key",+ ]+ for key in keys:+ data.pop(key, None)++ client: Final = get_async_httpx_client(+ llm_provider=LlmProviders.GIGACHAT,+ params={ # mutable-ok: httpx client params+ "timeout": httpx.Timeout(timeout=600.0, connect=5.0),+ },+ )++ data["client"] = client+ base_llm_response_processor: Final = ProxyBaseLLMRequestProcessing(data=data)++ # Use the common passthrough processing to handle metadata and hooks+ # This also handles all response formatting (streaming/non-streaming) and exceptions+ try:+ result = await base_llm_response_processor.base_passthrough_process_llm_request( # rebind-ok: assigned once in try block+ request=request,+ fastapi_response=fastapi_response,+ user_api_key_dict=user_api_key_dict,+ proxy_logging_obj=proxy_logging_obj,+ llm_router=llm_router,+ general_settings=general_settings,+ proxy_config=proxy_config,+ select_data_generator=select_data_generator,+ model=model,+ user_model=user_model,+ user_temperature=user_temperature,+ user_request_timeout=user_request_timeout,+ user_max_tokens=user_max_tokens,+ user_api_base=user_api_base,+ version=version,+ )+ except Exception as e: # noqa: BLE001 # Safe catch-all for handle exception+ # Use common exception handling+ raise await base_llm_response_processor._handle_llm_api_exception(+ e=e,+ user_api_key_dict=user_api_key_dict,+ proxy_logging_obj=proxy_logging_obj,+ )+ else:+ if isinstance(result, StreamingResponse):+ if result.headers.get("Content-Type") is None:+ result.headers["Content-Type"] = "text/event-stream; charset=utf-8"++ return result++@router.api_route("/watsonx/{endpoint:path}",methods=["GET", "POST", "PUT", "DELETE", "PATCH"],