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 - support anthropic_messages call types and add max tokens validation (#12162)

Guanghua Dinge6111f032bfb
diff --git a/litellm/utils.py b/litellm/utils.py
index 621221be1bb..5d952aaa8f6 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -1152,6 +1152,7 @@ def client(original_function): # noqa: PLR0915
and (
call_type == CallTypes.acompletion.value
or call_type == CallTypes.completion.value
+ or call_type == CallTypes.anthropic_messages.value
)
):
try:
@@ -1370,6 +1371,40 @@ def client(original_function): # noqa: PLR0915
elif _caching_handler_response.embedding_all_elements_cache_hit is True:
return _caching_handler_response.final_embedding_cached_response
+ # CHECK MAX TOKENS
+ if (
+ kwargs.get("max_tokens", None) is not None
+ and model is not None
+ and litellm.modify_params
+ is True # user is okay with params being modified
+ and (
+ call_type == CallTypes.acompletion.value
+ or call_type == CallTypes.completion.value
+ or call_type == CallTypes.anthropic_messages.value
+ )
+ ):
+ try:
+ base_model = model
+ if kwargs.get("hf_model_name", None) is not None:
+ base_model = f"huggingface/{kwargs.get('hf_model_name')}"
+ messages = None
+ if len(args) > 1:
+ messages = args[1]
+ elif kwargs.get("messages", None):
+ messages = kwargs["messages"]
+ user_max_tokens = kwargs.get("max_tokens")
+ modified_max_tokens = get_modified_max_tokens(
+ model=model,
+ base_model=base_model,
+ messages=messages,
+ user_max_tokens=user_max_tokens,
+ buffer_num=None,
+ buffer_perc=None,
+ )
+ kwargs["max_tokens"] = modified_max_tokens
+ except Exception as e:
+ print_verbose(f"Error while checking max token limit: {str(e)}")
+
# MODEL CALL
result = await original_function(*args, **kwargs)
end_time = datetime.datetime.now()