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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor(cursor): trim LOC in cursor byok tool normalization
tin56cc475c801d
diff --git a/litellm/proxy/response_api_endpoints/endpoints.py b/litellm/proxy/response_api_endpoints/endpoints.pyindex 8ee742c0d69..a3c1100eec3 100644--- a/litellm/proxy/response_api_endpoints/endpoints.py+++ b/litellm/proxy/response_api_endpoints/endpoints.py@@ -52,20 +52,6 @@ def _nest_flat_chat_tool(tool: object) -> object:return tool-def _nest_flat_chat_tools(tools: list) -> list:- return [_nest_flat_chat_tool(tool) for tool in tools]---def _nest_flat_chat_tool_choice(tool_choice: object) -> object:- if not isinstance(tool_choice, dict) or "name" not in tool_choice:- return tool_choice- if tool_choice.get("type") == "custom" and "custom" not in tool_choice:- return {"type": "custom", "custom": {"name": tool_choice["name"]}}- if tool_choice.get("type") == "function" and "function" not in tool_choice:- return {"type": "function", "function": {"name": tool_choice["name"]}}- return tool_choice--def _flatten_chat_tool_for_responses(tool: object) -> object:from litellm.litellm_core_utils.prompt_templates.common_utils import (convert_custom_tool_format_to_responses_shape,@@ -91,10 +77,6 @@ def _flatten_chat_tool_for_responses(tool: object) -> object:return tool-def _flatten_chat_tools_for_responses(tools: list) -> list:- return [_flatten_chat_tool_for_responses(tool) for tool in tools]--def _is_chat_completions_body(data: dict) -> bool:messages = data.get("messages")if isinstance(messages, list) and len(messages) > 0:@@ -469,15 +451,11 @@ async def cursor_chat_completions(# Keyed on messages CONTENT, not key presence: Cursor can send a null or# empty messages stub alongside a real agent-mode input arraytools = data.get("tools")- tool_choice = data.get("tool_choice")normalized: dict = {}if isinstance(tools, list):- nested_tools = _nest_flat_chat_tools(tools)+ nested_tools = [_nest_flat_chat_tool(tool) for tool in tools]if nested_tools != tools:normalized["tools"] = nested_tools- nested_tool_choice = _nest_flat_chat_tool_choice(tool_choice)- if nested_tool_choice != tool_choice:- normalized["tool_choice"] = nested_tool_choiceif normalized:_safe_set_request_parsed_body(request=request, parsed_body={**data, **normalized})return await chat_completion(@@ -496,7 +474,7 @@ async def cursor_chat_completions(tools = data.get("tools")if isinstance(tools, list):- data = {**data, "tools": _flatten_chat_tools_for_responses(tools)}+ data = {**data, "tools": [_flatten_chat_tool_for_responses(tool) for tool in tools]}tool_choice = data.get("tool_choice")flattened_tool_choice = _flatten_chat_tool_choice_for_responses(tool_choice)if flattened_tool_choice != tool_choice:diff --git a/litellm/types/utils.py b/litellm/types/utils.pyindex a1e52f7584f..7ff12b617e3 100644--- a/litellm/types/utils.py+++ b/litellm/types/utils.py@@ -1084,10 +1084,7 @@ class ChatCompletionDeltaToolCall(OpenAIObject):setattr(self, key, value)-class ChatCompletionCustomToolCallPayload(OpenAIObject):- name: str- input: str-+class _CustomToolCallAccess(OpenAIObject):def __contains__(self, key):return hasattr(self, key)@@ -1097,57 +1094,32 @@ class ChatCompletionCustomToolCallPayload(OpenAIObject):def __getitem__(self, key):return getattr(self, key)+ def __setitem__(self, key, value):+ setattr(self, key, value)-class ChatCompletionDeltaCustomToolCallPayload(OpenAIObject):- name: str | None = None- input: str | None = None- def __contains__(self, key):- return hasattr(self, key)+class ChatCompletionCustomToolCallPayload(_CustomToolCallAccess):+ name: str+ input: str- def get(self, key, default=None):- return getattr(self, key, default)- def __getitem__(self, key):- return getattr(self, key)+class ChatCompletionDeltaCustomToolCallPayload(_CustomToolCallAccess):+ name: str | None = None+ input: str | None = None-class ChatCompletionMessageCustomToolCall(OpenAIObject):+class ChatCompletionMessageCustomToolCall(_CustomToolCallAccess):id: strtype: Literal["custom"] = "custom"custom: ChatCompletionCustomToolCallPayload- def __contains__(self, key):- return hasattr(self, key)- def get(self, key, default=None):- return getattr(self, key, default)-- def __getitem__(self, key):- return getattr(self, key)-- def __setitem__(self, key, value):- setattr(self, key, value)---class ChatCompletionDeltaCustomToolCall(OpenAIObject):+class ChatCompletionDeltaCustomToolCall(_CustomToolCallAccess):id: str | None = Nonetype: str | None = Nonecustom: ChatCompletionDeltaCustomToolCallPayloadindex: int- def __contains__(self, key):- return hasattr(self, key)-- def get(self, key, default=None):- return getattr(self, key, default)-- def __getitem__(self, key):- return getattr(self, key)-- def __setitem__(self, key, value):- setattr(self, key, value)-class ChatCompletionMessageToolCall(OpenAIObject):def __init__(diff --git a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.pyindex 6a1e0d0a494..88e7bbc84d4 100644--- a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py+++ b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py@@ -911,33 +911,29 @@ def test_cursor_models_route_delegates_to_model_list():class TestNestFlatChatTools:def test_flat_custom_tool_is_nested(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tools+ from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tool- result = _nest_flat_chat_tools(- [{"type": "custom", "name": "ApplyPatch", "description": "V4A patch", "format": {"type": "text"}}]+ result = _nest_flat_chat_tool(+ {"type": "custom", "name": "ApplyPatch", "description": "V4A patch", "format": {"type": "text"}})- assert result == [- {- "type": "custom",- "custom": {"name": "ApplyPatch", "description": "V4A patch", "format": {"type": "text"}},- }- ]+ assert result == {+ "type": "custom",+ "custom": {"name": "ApplyPatch", "description": "V4A patch", "format": {"type": "text"}},+ }def test_flat_function_tool_is_nested(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tools+ from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tool- result = _nest_flat_chat_tools(- [{"type": "function", "name": "read_file", "description": "d", "parameters": {"type": "object"}}]+ result = _nest_flat_chat_tool(+ {"type": "function", "name": "read_file", "description": "d", "parameters": {"type": "object"}})- assert result == [- {- "type": "function",- "function": {"name": "read_file", "description": "d", "parameters": {"type": "object"}},- }- ]+ assert result == {+ "type": "function",+ "function": {"name": "read_file", "description": "d", "parameters": {"type": "object"}},+ }def test_already_nested_and_unrecognized_tools_pass_through_unchanged(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tools+ from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tooltools = [{"type": "custom", "custom": {"name": "already_nested"}},@@ -950,7 +946,7 @@ class TestNestFlatChatTools:None,42,]- assert _nest_flat_chat_tools(tools) == tools+ assert [_nest_flat_chat_tool(tool) for tool in tools] == toolsclass TestCursorMessagesArmToolNormalization:@@ -1014,7 +1010,7 @@ class TestCursorMessagesArmToolNormalization:},},]- assert seen["body"]["tool_choice"] == {"type": "custom", "custom": {"name": "ApplyPatch"}}+ assert seen["body"]["tool_choice"] == {"type": "custom", "name": "ApplyPatch"}assert seen["body"]["messages"] == [{"role": "user", "content": "use ApplyPatch"}]@pytest.mark.asyncio@@ -1066,7 +1062,7 @@ class TestNestFlatChatToolShapeMatrix:@pytest.mark.parametrize("envelope", ["flat", "nested"])@pytest.mark.parametrize("format_shape", ["absent", "text", "flat_grammar", "nested_grammar"])def test_every_envelope_and_format_combination_lands_canonical(self, envelope, format_shape):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tools+ from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_toolformat_value = {"absent": None,@@ -1085,10 +1081,10 @@ class TestNestFlatChatToolShapeMatrix:elif format_shape == "text":canonical_payload["format"] = self.TEXT- assert _nest_flat_chat_tools([tool]) == [{"type": "custom", "custom": canonical_payload}]+ assert _nest_flat_chat_tool(tool) == {"type": "custom", "custom": canonical_payload}def test_nested_envelope_with_flat_grammar_matches_live_cursor_capture(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tools+ from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_toolcursor_tool = {"type": "custom",@@ -1097,60 +1093,25 @@ class TestNestFlatChatToolShapeMatrix:"format": {"type": "grammar", "definition": "start: patch", "syntax": "lark"},},}- assert _nest_flat_chat_tools([cursor_tool]) == [- {- "type": "custom",- "custom": {- "name": "ApplyPatch",- "format": {- "type": "grammar",- "grammar": {"definition": "start: patch", "syntax": "lark"},- },+ assert _nest_flat_chat_tool(cursor_tool) == {+ "type": "custom",+ "custom": {+ "name": "ApplyPatch",+ "format": {+ "type": "grammar",+ "grammar": {"definition": "start: patch", "syntax": "lark"},},- }- ]+ },+ }def test_canonical_nested_tool_is_returned_equal(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tools+ from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_toolcanonical = {"type": "custom","custom": {"name": "A", "format": {"type": "grammar", "grammar": {"definition": "d", "syntax": "lark"}}},}- assert _nest_flat_chat_tools([canonical]) == [canonical]---class TestNestFlatChatToolChoice:- def test_flat_custom_tool_choice_is_nested(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tool_choice-- assert _nest_flat_chat_tool_choice({"type": "custom", "name": "ApplyPatch"}) == {- "type": "custom",- "custom": {"name": "ApplyPatch"},- }-- def test_flat_function_tool_choice_is_nested(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tool_choice-- assert _nest_flat_chat_tool_choice({"type": "function", "name": "f"}) == {- "type": "function",- "function": {"name": "f"},- }-- def test_non_flat_tool_choice_values_pass_through(self):- from litellm.proxy.response_api_endpoints.endpoints import _nest_flat_chat_tool_choice-- for unchanged in (- "auto",- "required",- None,- {"type": "custom", "custom": {"name": "x"}},- {"type": "function", "function": {"name": "f"}},- {"type": "auto"},- {"name": "typeless"},- 42,- ):- assert _nest_flat_chat_tool_choice(unchanged) == unchanged+ assert _nest_flat_chat_tool(canonical) == canonicalclass TestFlattenChatToolsForResponsesInputArm:@@ -1165,7 +1126,7 @@ class TestFlattenChatToolsForResponsesInputArm:@pytest.mark.parametrize("envelope", ["flat", "nested"])@pytest.mark.parametrize("format_shape", ["absent", "text", "flat_grammar", "nested_grammar"])def test_every_envelope_and_format_combination_lands_flat(self, envelope, format_shape):- from litellm.proxy.response_api_endpoints.endpoints import _flatten_chat_tools_for_responses+ from litellm.proxy.response_api_endpoints.endpoints import _flatten_chat_tool_for_responsesformat_value = {"absent": None,@@ -1184,21 +1145,21 @@ class TestFlattenChatToolsForResponsesInputArm:elif format_shape == "text":canonical["format"] = {"type": "text"}- assert _flatten_chat_tools_for_responses([tool]) == [canonical]+ assert _flatten_chat_tool_for_responses(tool) == canonicaldef test_nested_function_tool_is_flattened_and_flat_passes_through(self):- from litellm.proxy.response_api_endpoints.endpoints import _flatten_chat_tools_for_responses+ from litellm.proxy.response_api_endpoints.endpoints import _flatten_chat_tool_for_responsesnested = {"type": "function", "function": {"name": "read_file", "parameters": {"type": "object"}}}flat = {"type": "function", "name": "read_file", "parameters": {"type": "object"}}- assert _flatten_chat_tools_for_responses([nested]) == [flat]- assert _flatten_chat_tools_for_responses([flat]) == [flat]+ assert _flatten_chat_tool_for_responses(nested) == flat+ assert _flatten_chat_tool_for_responses(flat) == flatdef test_unrecognized_entries_pass_through(self):- from litellm.proxy.response_api_endpoints.endpoints import _flatten_chat_tools_for_responses+ from litellm.proxy.response_api_endpoints.endpoints import _flatten_chat_tool_for_responsesentries = [{"type": "web_search"}, {"type": "custom"}, "junk", None, {}]- assert _flatten_chat_tools_for_responses(entries) == entries+ assert [_flatten_chat_tool_for_responses(entry) for entry in entries] == entriesclass TestFlattenChatToolChoiceForResponsesInputArm: