diff --git a/litellm/llms/oci/chat/generic.py b/litellm/llms/oci/chat/generic.py index 8ff8ef9abc4..57a8c3819fb 100644 --- a/litellm/llms/oci/chat/generic.py +++ b/litellm/llms/oci/chat/generic.py @@ -13,6 +13,7 @@ from typing import Any, Final import httpx from pydantic import ValidationError +from litellm._logging import verbose_logger from litellm.llms.oci.common_utils import ( OCIError, resolve_oci_schema_anyof, @@ -218,8 +219,18 @@ def adapt_tool_definition_to_oci_standard(tools: list[dict], vendor: OCIVendors) """ new_tools: Final = [] for tool in tools: - if tool["type"] != "function": - raise OCIError(status_code=400, message="OCI only supports function tools") + tool_type = tool.get("type") + if tool_type != "function": + # Coding-agent clients (Codex over the Responses API, Claude Code + # over the Anthropic Messages API) mix built-in / server tools in + # with their function tools. OCI GENERIC only knows FUNCTION tools, + # so translate what we can and skip the rest instead of failing + # the whole request (see BerriAI/litellm#31449). + verbose_logger.warning( + "OCI GENERIC only supports function tools; skipping tool of type %r", + tool_type, + ) + continue tool_function = tool.get("function") if not isinstance(tool_function, dict): diff --git a/litellm/llms/oci/chat/transformation.py b/litellm/llms/oci/chat/transformation.py index 8e4e41b4ac1..90b5bbe944c 100644 --- a/litellm/llms/oci/chat/transformation.py +++ b/litellm/llms/oci/chat/transformation.py @@ -230,6 +230,33 @@ def _normalize_response_format(selected_params: dict, vendor: OCIVendors) -> Non selected_params["responseFormat"] = {"type": "JSON_OBJECT" if fmt == "JSON" else fmt} +def _model_supports_reasoning_effort(model: str) -> bool: + """Return True when the OCI-hosted model accepts ``reasoningEffort``. + + Verified against the service (us-chicago-1, 2026-09): every served xAI + Grok model rejects the parameter with ``400 "Model ... does not support + parameter reasoningEffort"`` (including the ``*-reasoning`` variants), and + OpenAI's non-reasoning commercial models (``gpt-4o``, ``gpt-4.1``) reject it + with ``Unrecognized request argument supplied: reasoning_effort``. OpenAI + reasoning models (gpt-5 family, o-series, driven by the catalog's + ``supports_reasoning`` flag) and the ``gpt-oss-*`` open weights accept it, + and Meta / Google models ignore it harmlessly. Cohere is handled by its own + param map. See BerriAI/litellm#31449. + """ + if not model: + return True + name: Final = model[4:] if model.lower().startswith("oci/") else model + lowered: Final = name.lower() + vendor: Final = lowered.split(".")[0] + if vendor == "xai": + return False + if vendor == "openai": + if lowered.startswith("openai.gpt-oss"): + return True + return bool(supports_reasoning(model=name, custom_llm_provider="oci")) + return True + + def get_vendor_from_model(model: str) -> OCIVendors: """Return the OCI vendor enum for a model name. @@ -323,7 +350,10 @@ class OCIChatConfig(BaseConfig): # honoured and advertising it would be misleading. Callers that gate on # this list strip n=1 (a no-op, matching what map_openai_params does); # callers that bypass it have n=1 dropped there. Both paths converge. - return [key for key, value in param_map.items() if value] + supported: Final = [key for key, value in param_map.items() if value] + if "reasoning_effort" in supported and not _model_supports_reasoning_effort(model): + supported.remove("reasoning_effort") + return supported def map_openai_params( self, @@ -340,6 +370,20 @@ class OCIChatConfig(BaseConfig): for key, value in {**non_default_params, **optional_params}.items(): alias = param_map.get(key) + if key == "reasoning_effort" and alias and not _model_supports_reasoning_effort(model): + # The GENERIC map forwards reasoning_effort for every non-Cohere + # model, but xAI Grok and OpenAI's non-reasoning models 400 on + # it. Follow the standard unsupported-param contract so + # drop_params (and additional_drop_params) work as documented. + if drop_params or litellm.drop_params: + continue + raise OCIError( + status_code=400, + message=( + f"param `reasoning_effort` is not supported on OCI model `{model}` " + "(the model rejects reasoningEffort); set drop_params=True to omit it" + ), + ) if alias is False: # max_retries is a litellm-level control param (litellm applies # retries itself); it is never a generation param OCI accepts, so @@ -507,6 +551,12 @@ class OCIChatConfig(BaseConfig): selected_params["tools"], vendor, ) + if not selected_params["tools"]: + # Every tool was skipped (no function tools): send a plain chat + # request rather than an empty tools array + toolChoice. + selected_params.pop("tools") + for key in ("toolChoice", "tool_choice"): + selected_params.pop(key, None) # Normalise tool_choice to OCI's flat uppercase dict form # ({"type": "AUTO"|"NONE"|"REQUIRED"} or {"type": "FUNCTION", "name": ""}). diff --git a/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py b/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py index 4c9bd29b337..1f9695dc5d6 100644 --- a/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py +++ b/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py @@ -1178,15 +1178,19 @@ class TestOCIReasoningEffort: return body["chatRequest"] def test_reasoning_effort_lowercase_uppercased(self): + # gpt-5 accepts reasoningEffort; every OCI-hosted Grok model rejects it + # (see TestReasoningEffortGating in test_oci_tool_and_reasoning_gating.py). chat_request = self._build_chat_request( - "xai.grok-4-fast-reasoning", + "openai.gpt-5", {"reasoning_effort": "low"}, ) assert chat_request.get("reasoningEffort") == "LOW" def test_reasoning_effort_disable_mapped_to_none(self): + # gpt-5 accepts reasoningEffort; every OCI-hosted Grok model rejects it + # (see TestReasoningEffortGating in test_oci_tool_and_reasoning_gating.py). chat_request = self._build_chat_request( - "xai.grok-4-fast-reasoning", + "openai.gpt-5", {"reasoning_effort": "disable"}, ) assert chat_request.get("reasoningEffort") == "NONE" diff --git a/tests/test_litellm/llms/oci/chat/test_oci_tool_and_reasoning_gating.py b/tests/test_litellm/llms/oci/chat/test_oci_tool_and_reasoning_gating.py new file mode 100644 index 00000000000..ddae6a6afa5 --- /dev/null +++ b/tests/test_litellm/llms/oci/chat/test_oci_tool_and_reasoning_gating.py @@ -0,0 +1,121 @@ +"""Regression tests for BerriAI/litellm#31449. + +1. Non-function tools must be skipped, not fail the whole request. +2. ``reasoning_effort`` must not be forwarded to OCI models that reject it + (xAI Grok, OpenAI non-reasoning models), so ``drop_params`` works. +""" + +import pytest + +import litellm +from litellm.llms.oci.chat.generic import adapt_tool_definition_to_oci_standard +from litellm.llms.oci.chat.transformation import ( + OCIChatConfig, + _model_supports_reasoning_effort, +) +from litellm.llms.oci.common_utils import OCIError +from litellm.types.llms.oci import OCIVendors + +FUNCTION_TOOL = { + "type": "function", + "function": { + "name": "get_weather", + "description": "Weather for a city", + "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}, + }, +} +BUILTIN_TOOL = {"type": "web_search_preview"} +SERVER_TOOL = {"type": "computer_use_preview", "display_width": 1024} + + +class TestNonFunctionToolsAreSkipped: + def test_mixed_tools_keep_function_tools_only(self): + result = adapt_tool_definition_to_oci_standard([BUILTIN_TOOL, FUNCTION_TOOL, SERVER_TOOL], OCIVendors.GENERIC) + assert [tool.name for tool in result] == ["get_weather"] + assert result[0].type == "FUNCTION" + + def test_only_non_function_tools_yield_empty_list(self): + assert adapt_tool_definition_to_oci_standard([BUILTIN_TOOL], OCIVendors.GENERIC) == [] + + def test_tool_without_type_is_skipped_not_keyerror(self): + assert adapt_tool_definition_to_oci_standard([{"name": "x"}], OCIVendors.GENERIC) == [] + + def test_request_drops_tools_and_tool_choice_when_none_remain(self): + config = OCIChatConfig() + params = config._get_optional_params( + OCIVendors.GENERIC, + {"tools": [BUILTIN_TOOL], "tool_choice": "auto"}, + model="xai.grok-4.20", + ) + assert "tools" not in params + assert "toolChoice" not in params + assert "tool_choice" not in params + + def test_request_keeps_function_tools_and_tool_choice(self): + config = OCIChatConfig() + params = config._get_optional_params( + OCIVendors.GENERIC, + {"tools": [BUILTIN_TOOL, FUNCTION_TOOL], "tool_choice": "auto"}, + model="xai.grok-4.20", + ) + assert [tool.name for tool in params["tools"]] == ["get_weather"] + assert params["toolChoice"] == {"type": "AUTO"} + + +class TestReasoningEffortGating: + @pytest.mark.parametrize( + "model,expected", + [ + ("xai.grok-4.20", False), + ("oci/xai.grok-4.20-0309-reasoning", False), + ("xai.grok-code-fast-1", False), + ("openai.gpt-4o", False), + ("openai.gpt-4.1", False), + ("openai.gpt-5", True), + ("oci/openai.gpt-5-mini", True), + ("openai.gpt-oss-120b", True), + ("meta.llama-3.3-70b-instruct", True), + ("google.gemini-2.5-flash", True), + ], + ) + def test_model_supports_reasoning_effort(self, model, expected): + assert _model_supports_reasoning_effort(model) is expected + + def test_supported_params_exclude_reasoning_effort_for_grok(self): + supported = OCIChatConfig().get_supported_openai_params("xai.grok-4.20") + assert "reasoning_effort" not in supported + assert "tools" in supported + + def test_supported_params_include_reasoning_effort_for_gpt5(self): + assert "reasoning_effort" in OCIChatConfig().get_supported_openai_params("openai.gpt-5") + + @pytest.mark.parametrize("model", ["xai.grok-4.20", "openai.gpt-4o"]) + def test_reasoning_effort_dropped_under_drop_params(self, model): + result = OCIChatConfig().map_openai_params( + non_default_params={"reasoning_effort": "low", "temperature": 0.1}, + optional_params={}, + model=model, + drop_params=True, + ) + assert "reasoningEffort" not in result + assert "reasoning_effort" not in result + assert result["temperature"] == 0.1 + + def test_reasoning_effort_raises_without_drop_params_for_grok(self, monkeypatch): + monkeypatch.setattr(litellm, "drop_params", False) + with pytest.raises(OCIError, match=r"reasoning_effort.*xai\.grok-4\.20"): + OCIChatConfig().map_openai_params( + non_default_params={"reasoning_effort": "low"}, + optional_params={}, + model="xai.grok-4.20", + drop_params=False, + ) + + def test_reasoning_effort_still_forwarded_for_reasoning_models(self): + result = OCIChatConfig().map_openai_params( + non_default_params={"reasoning_effort": "low"}, + optional_params={}, + model="openai.gpt-5", + drop_params=False, + ) + assert result["reasoningEffort"] == "low"