Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/client/model_groups.py: 0%
27 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
1from typing import Any, Final
3import requests
5from .exceptions import UnauthorizedError
8class ModelGroupsManagementClient:
9 def __init__(self, base_url: str, api_key: str | None = None, timeout: int = 30):
10 """
11 Initialize the ModelGroupsManagementClient.
13 Args:
14 base_url (str): The base URL of the LiteLLM proxy server (e.g., "http://localhost:8000")
15 api_key (Optional[str]): API key for authentication. If provided, it will be sent as a Bearer token.
16 timeout (int): Request timeout in seconds (default: 30)
17 """
18 self._base_url = base_url.rstrip("/") # Remove trailing slash if present
19 self._api_key = api_key
20 self._timeout = timeout
22 def _get_headers(self) -> dict[str, str]:
23 """
24 Get the headers for API requests, including authorization if api_key is set.
26 Returns:
27 Dict[str, str]: Headers to use for API requests
28 """
29 headers: Final = {}
30 if self._api_key:
31 headers["Authorization"] = f"Bearer {self._api_key}"
32 return headers
34 def info(self, return_request: bool = False) -> list[dict[str, Any]] | requests.Request:
35 """
36 Get detailed information about all model groups from the server.
38 Args:
39 return_request (bool): If True, returns the prepared request object instead of executing it
41 Returns:
42 Union[List[Dict[str, Any]], requests.Request]: Either a list of model group information dictionaries
43 or a prepared request object if return_request is True
45 Raises:
46 UnauthorizedError: If the request fails with a 401 status code
47 requests.exceptions.RequestException: If the request fails with any other error
48 """
49 url: Final = f"{self._base_url}/model_group/info"
50 request: Final = requests.Request("GET", url, headers=self._get_headers())
52 if return_request:
53 return request
55 # Prepare and send the request
56 session: Final = requests.Session()
57 try:
58 response: Final = session.send(request.prepare(), timeout=self._timeout)
59 response.raise_for_status()
60 return response.json()["data"]
61 except requests.exceptions.HTTPError as e:
62 if e.response.status_code == 401:
63 raise UnauthorizedError(e)
64 raise