Skip to content

Commit 110ce54

Browse files
authored
[Feat]Add cancel endpoint support for openai and azure (#14561)
* Add cancel endpoint support for openai and azure * fix lint error * fix cancel url contruction azure * readd changes
1 parent 7fd6e62 commit 110ce54

File tree

12 files changed

+823
-129
lines changed

12 files changed

+823
-129
lines changed

litellm/llms/azure/responses/transformation.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Tuple
22

3+
import httpx
4+
35
from litellm._logging import verbose_logger
46
from litellm.llms.azure.common_utils import BaseAzureLLM
57
from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig
@@ -194,3 +196,66 @@ def transform_list_input_items_request(
194196
params["order"] = order
195197
verbose_logger.debug(f"list input items url={url}")
196198
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)

litellm/llms/base_llm/responses/transformation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,28 @@ def should_fake_stream(
217217
) -> bool:
218218
"""Returns True if litellm should fake a stream for the given model and stream value"""
219219
return False
220+
221+
#########################################################
222+
########## CANCEL RESPONSE API TRANSFORMATION ##########
223+
#########################################################
224+
@abstractmethod
225+
def transform_cancel_response_api_request(
226+
self,
227+
response_id: str,
228+
api_base: str,
229+
litellm_params: GenericLiteLLMParams,
230+
headers: dict,
231+
) -> Tuple[str, Dict]:
232+
pass
233+
234+
@abstractmethod
235+
def transform_cancel_response_api_response(
236+
self,
237+
raw_response: httpx.Response,
238+
logging_obj: LiteLLMLoggingObj,
239+
) -> ResponsesAPIResponse:
240+
pass
241+
242+
#########################################################
243+
########## END CANCEL RESPONSE API TRANSFORMATION #######
244+
#########################################################

0 commit comments

Comments
 (0)