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
fix llm request utils
Ishaan Jaff3ee7962f9c24
diff --git a/litellm/litellm_core_utils/llm_request_utils.py b/litellm/litellm_core_utils/llm_request_utils.pyindex 8cfa48e937d..50dbdc5536e 100644--- a/litellm/litellm_core_utils/llm_request_utils.py+++ b/litellm/litellm_core_utils/llm_request_utils.py@@ -3,89 +3,66 @@ from typing import Dict, Optionalimport litellm-class LitellmCoreRequestUtils:+def _ensure_extra_body_is_safe(extra_body: Optional[Dict]) -> Optional[Dict]:+ """+ Ensure that the extra_body sent in the request is safe, otherwise users will see this error- @staticmethod- def _ensure_extra_body_is_safe(extra_body: Optional[Dict]) -> Optional[Dict]:- """- Ensure that the extra_body sent in the request is safe, otherwise users will see this error+ "Object of type TextPromptClient is not JSON serializable- "Object of type TextPromptClient is not JSON serializable+ Relevant Issue: https://github.com/BerriAI/litellm/issues/4140+ """+ if extra_body is None:+ return None- Relevant Issue: https://github.com/BerriAI/litellm/issues/4140- """- if extra_body is None:- return None+ if not isinstance(extra_body, dict):+ return extra_body- if not isinstance(extra_body, dict):- return extra_body+ if "metadata" in extra_body and isinstance(extra_body["metadata"], dict):+ if "prompt" in extra_body["metadata"]:+ _prompt = extra_body["metadata"].get("prompt")- if "metadata" in extra_body and isinstance(extra_body["metadata"], dict):- if "prompt" in extra_body["metadata"]:- _prompt = extra_body["metadata"].get("prompt")+ # users can send Langfuse TextPromptClient objects, so we need to convert them to dicts+ # Langfuse TextPromptClients have .__dict__ attribute+ if _prompt is not None and hasattr(_prompt, "__dict__"):+ extra_body["metadata"]["prompt"] = _prompt.__dict__- # users can send Langfuse TextPromptClient objects, so we need to convert them to dicts- # Langfuse TextPromptClients have .__dict__ attribute- if _prompt is not None and hasattr(_prompt, "__dict__"):- extra_body["metadata"]["prompt"] = _prompt.__dict__+ return extra_body- return extra_body- @staticmethod- def pick_cheapest_chat_models_from_llm_provider(custom_llm_provider: str, n=1):- """- Pick the n cheapest chat models from the LLM provider.-- Args:- custom_llm_provider (str): The name of the LLM provider.- n (int): The number of cheapest models to return.-- Returns:- list[str]: A list of the n cheapest chat models.- """- if custom_llm_provider not in litellm.models_by_provider:- return []-- known_models = litellm.models_by_provider.get(custom_llm_provider, [])- model_costs = []-- for model in known_models:- try:- model_info = litellm.get_model_info(- model=model, custom_llm_provider=custom_llm_provider- )- except Exception:- continue- if model_info.get("mode") != "chat":- continue- _cost = model_info.get("input_cost_per_token", 0) + model_info.get(- "output_cost_per_token", 0+def pick_cheapest_chat_models_from_llm_provider(custom_llm_provider: str, n=1):+ """+ Pick the n cheapest chat models from the LLM provider.++ Args:+ custom_llm_provider (str): The name of the LLM provider.+ n (int): The number of cheapest models to return.++ Returns:+ list[str]: A list of the n cheapest chat models.+ """+ if custom_llm_provider not in litellm.models_by_provider:+ return []++ known_models = litellm.models_by_provider.get(custom_llm_provider, [])+ model_costs = []++ for model in known_models:+ try:+ model_info = litellm.get_model_info(+ model=model, custom_llm_provider=custom_llm_provider)- model_costs.append((model, _cost))-- # Sort by cost (ascending)- model_costs.sort(key=lambda x: x[1])-- # Return the top n cheapest models- return [model for model, _ in model_costs[:n]]-- @staticmethod- def select_model_for_request_transformation(- model: str,- base_model: Optional[str] = None,- litellm_params: Optional[Dict] = None,- ) -> str:- """- If `base_model` is passed in by user, use it for the request transformation-- Else, use the model passed in the request- """- if base_model is not None:- return base_model- elif (- litellm_params is not None and litellm_params.get("base_model") is not None- ):- return litellm_params["base_model"]- else:- return model+ except Exception:+ continue+ if model_info.get("mode") != "chat":+ continue+ _cost = model_info.get("input_cost_per_token", 0) + model_info.get(+ "output_cost_per_token", 0+ )+ model_costs.append((model, _cost))++ # Sort by cost (ascending)+ model_costs.sort(key=lambda x: x[1])++ # Return the top n cheapest models+ return [model for model, _ in model_costs[:n]]