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 anthropic_messages implementation

Ishaan Jaff3640262dbfac
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py
index 9b890db2669..54826a38bae 100644
--- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py
+++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py
@@ -6,7 +6,7 @@
"""
import json
-from typing import Any, AsyncIterator, Dict, Optional, Union, cast
+from typing import AsyncIterator, Dict, List, Optional, Union, cast
import httpx
@@ -64,9 +64,20 @@ class AnthropicMessagesHandler:
@client
async def anthropic_messages(
- api_key: str,
+ max_tokens: int,
+ messages: List[Dict[str, Union[str, List[Dict[str, str]]]]],
model: str,
- stream: bool = False,
+ metadata: Optional[Dict] = None,
+ stop_sequences: Optional[List[str]] = None,
+ stream: Optional[bool] = False,
+ system: Optional[str] = None,
+ temperature: Optional[float] = 1.0,
+ thinking: Optional[Dict] = None,
+ tool_choice: Optional[Dict] = None,
+ tools: Optional[List[Dict]] = None,
+ top_k: Optional[int] = None,
+ top_p: Optional[float] = None,
+ api_key: Optional[str] = None,
api_base: Optional[str] = None,
client: Optional[AsyncHTTPHandler] = None,
custom_llm_provider: Optional[str] = None,
@@ -133,7 +144,7 @@ async def anthropic_messages(
litellm_logging_obj.model_call_details.update(kwargs)
# Prepare request body
- request_body = kwargs.copy()
+ request_body = locals().copy()
request_body = {
k: v
for k, v in request_body.items()
@@ -165,7 +176,7 @@ async def anthropic_messages(
url=request_url,
headers=headers,
data=json.dumps(request_body),
- stream=stream,
+ stream=stream or False,
)
response.raise_for_status()
diff --git a/litellm/messages/__init__.py b/litellm/messages/__init__.py
index 1274f768ba7..0a4ea92c2c3 100644
--- a/litellm/messages/__init__.py
+++ b/litellm/messages/__init__.py
@@ -4,13 +4,105 @@ Interface for Anthropic's messages API
Use this to call LLMs in Anthropic /messages Request/Response format
"""
+from typing import Dict, List, Optional, Union
+
from litellm.llms.anthropic.experimental_pass_through.handler import (
anthropic_messages as _async_anthropic_messages,
)
-async def acreate(*args, **kwargs):
+async def acreate(
+ max_tokens: int,
+ messages: List[Dict[str, Union[str, List[Dict[str, str]]]]],
+ model: str,
+ metadata: Optional[Dict] = None,
+ stop_sequences: Optional[List[str]] = None,
+ stream: Optional[bool] = False,
+ system: Optional[str] = None,
+ temperature: Optional[float] = 1.0,
+ thinking: Optional[Dict] = None,
+ tool_choice: Optional[Dict] = None,
+ tools: Optional[List[Dict]] = None,
+ top_k: Optional[int] = None,
+ top_p: Optional[float] = None,
+ **kwargs
+) -> Dict:
"""
- Wrapper around Anthropic's messages API
+ Async wrapper for Anthropic's messages API
+
+ Args:
+ max_tokens (int): Maximum tokens to generate (required)
+ messages (List[Dict]): List of message objects with role and content (required)
+ model (str): Model name to use (required)
+ metadata (Dict, optional): Request metadata
+ stop_sequences (List[str], optional): Custom stop sequences
+ stream (bool, optional): Whether to stream the response
+ system (str, optional): System prompt
+ temperature (float, optional): Sampling temperature (0.0 to 1.0)
+ thinking (Dict, optional): Extended thinking configuration
+ tool_choice (Dict, optional): Tool choice configuration
+ tools (List[Dict], optional): List of tool definitions
+ top_k (int, optional): Top K sampling parameter
+ top_p (float, optional): Nucleus sampling parameter
+ **kwargs: Additional arguments
+
+ Returns:
+ Dict: Response from the API
+ """
+ return await _async_anthropic_messages(
+ max_tokens=max_tokens,
+ messages=messages,
+ model=model,
+ metadata=metadata,
+ stop_sequences=stop_sequences,
+ stream=stream,
+ system=system,
+ temperature=temperature,
+ thinking=thinking,
+ tool_choice=tool_choice,
+ tools=tools,
+ top_k=top_k,
+ top_p=top_p,
+ **kwargs,
+ )
+
+
+async def create(
+ max_tokens: int,
+ messages: List[Dict[str, Union[str, List[Dict[str, str]]]]],
+ model: str,
+ metadata: Optional[Dict] = None,
+ stop_sequences: Optional[List[str]] = None,
+ stream: Optional[bool] = False,
+ system: Optional[str] = None,
+ temperature: Optional[float] = 1.0,
+ thinking: Optional[Dict] = None,
+ tool_choice: Optional[Dict] = None,
+ tools: Optional[List[Dict]] = None,
+ top_k: Optional[int] = None,
+ top_p: Optional[float] = None,
+ **kwargs
+) -> Dict:
+ """
+ Async wrapper for Anthropic's messages API
+
+ Args:
+ max_tokens (int): Maximum tokens to generate (required)
+ messages (List[Dict]): List of message objects with role and content (required)
+ model (str): Model name to use (required)
+ metadata (Dict, optional): Request metadata
+ stop_sequences (List[str], optional): Custom stop sequences
+ stream (bool, optional): Whether to stream the response
+ system (str, optional): System prompt
+ temperature (float, optional): Sampling temperature (0.0 to 1.0)
+ thinking (Dict, optional): Extended thinking configuration
+ tool_choice (Dict, optional): Tool choice configuration
+ tools (List[Dict], optional): List of tool definitions
+ top_k (int, optional): Top K sampling parameter
+ top_p (float, optional): Nucleus sampling parameter
+ **kwargs: Additional arguments
+
+ Returns:
+ Dict: Response from the API
"""
- return await _async_anthropic_messages(*args, **kwargs)
+ raise NotImplementedError("This function is not implemented")