|
1 | 1 | from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Tuple
|
2 | 2 |
|
| 3 | +import httpx |
| 4 | + |
3 | 5 | from litellm._logging import verbose_logger
|
4 | 6 | from litellm.llms.azure.common_utils import BaseAzureLLM
|
5 | 7 | from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig
|
@@ -194,3 +196,66 @@ def transform_list_input_items_request(
|
194 | 196 | params["order"] = order
|
195 | 197 | verbose_logger.debug(f"list input items url={url}")
|
196 | 198 | return url, params
|
| 199 | + |
| 200 | + ######################################################### |
| 201 | + ########## CANCEL RESPONSE API TRANSFORMATION ########## |
| 202 | + ######################################################### |
| 203 | + def transform_cancel_response_api_request( |
| 204 | + self, |
| 205 | + response_id: str, |
| 206 | + api_base: str, |
| 207 | + litellm_params: GenericLiteLLMParams, |
| 208 | + headers: dict, |
| 209 | + ) -> Tuple[str, Dict]: |
| 210 | + """ |
| 211 | + Transform the cancel response API request into a URL and data |
| 212 | +
|
| 213 | + Azure OpenAI API expects the following request: |
| 214 | + - POST /openai/responses/{response_id}/cancel?api-version=xxx |
| 215 | +
|
| 216 | + This function handles URLs with query parameters by inserting the response_id |
| 217 | + at the correct location (before any query parameters). |
| 218 | + """ |
| 219 | + from urllib.parse import urlparse, urlunparse |
| 220 | + |
| 221 | + # Parse the URL to separate its components |
| 222 | + parsed_url = urlparse(api_base) |
| 223 | + |
| 224 | + # Insert the response_id and /cancel at the end of the path component |
| 225 | + # Remove trailing slash if present to avoid double slashes |
| 226 | + path = parsed_url.path.rstrip("/") |
| 227 | + new_path = f"{path}/{response_id}/cancel" |
| 228 | + |
| 229 | + # Reconstruct the URL with all original components but with the modified path |
| 230 | + cancel_url = urlunparse( |
| 231 | + ( |
| 232 | + parsed_url.scheme, # http, https |
| 233 | + parsed_url.netloc, # domain name, port |
| 234 | + new_path, # path with response_id and /cancel added |
| 235 | + parsed_url.params, # parameters |
| 236 | + parsed_url.query, # query string |
| 237 | + parsed_url.fragment, # fragment |
| 238 | + ) |
| 239 | + ) |
| 240 | + |
| 241 | + data: Dict = {} |
| 242 | + verbose_logger.debug(f"cancel response url={cancel_url}") |
| 243 | + return cancel_url, data |
| 244 | + |
| 245 | + def transform_cancel_response_api_response( |
| 246 | + self, |
| 247 | + raw_response: httpx.Response, |
| 248 | + logging_obj: LiteLLMLoggingObj, |
| 249 | + ) -> ResponsesAPIResponse: |
| 250 | + """ |
| 251 | + Transform the cancel response API response into a ResponsesAPIResponse |
| 252 | + """ |
| 253 | + try: |
| 254 | + raw_response_json = raw_response.json() |
| 255 | + except Exception: |
| 256 | + from litellm.llms.azure.chat.gpt_transformation import AzureOpenAIError |
| 257 | + |
| 258 | + raise AzureOpenAIError( |
| 259 | + message=raw_response.text, status_code=raw_response.status_code |
| 260 | + ) |
| 261 | + return ResponsesAPIResponse(**raw_response_json) |
0 commit comments