diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 81a186eadf4..32d3e9f96a9 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -11,11 +11,12 @@ import time import uuid from collections.abc import AsyncIterator, Iterator, Mapping, Sequence from types import MappingProxyType -from typing import TYPE_CHECKING, Any, Final +from typing import TYPE_CHECKING, Any, Final, cast # noqa: TID251 # response_format is a plain JSON object import httpx from pydantic import TypeAdapter +import litellm from litellm._logging import verbose_logger from litellm.litellm_core_utils.url_utils import join_api_base from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException @@ -63,6 +64,13 @@ class GigaChatError(BaseLLMException): """GigaChat API error.""" +def _is_supported_response_format(value: object) -> bool: + if not isinstance(value, Mapping): + return False + response_format: Final = cast("dict[str, object]", value) # cast-ok: the provider sends a JSON object + return response_format.get("type") in (None, "text", "json_schema") + + class GigaChatConfig(BaseConfig): """ Configuration class for GigaChat API. @@ -153,7 +161,6 @@ class GigaChatConfig(BaseConfig): "top_p", "max_tokens", "max_completion_tokens", - "stop", "tools", "tool_choice", "functions", @@ -182,9 +189,6 @@ class GigaChatConfig(BaseConfig): optional_params["top_p"] = value elif param in ("max_tokens", "max_completion_tokens"): optional_params["max_tokens"] = value - elif param == "stop": - # GigaChat doesn't support stop sequences - pass elif param == "tools": # Convert tools to functions format if isinstance(value, Sequence): @@ -223,6 +227,19 @@ class GigaChatConfig(BaseConfig): ] optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True + elif not _is_supported_response_format(value): # pyright: ignore[reportUnknownArgumentType] # value + from litellm.utils import UnsupportedParamsError + + if litellm.drop_params is not True and drop_params is not True: + raise UnsupportedParamsError( + status_code=400, + message=( + "GigaChat does not support response_format=" + f"{value!r}. Use response_format={{'type': 'json_schema', " + "'json_schema': {...}}} instead, or set `drop_params=True` " + "to drop it." + ), + ) return optional_params diff --git a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py index 115d81bfb90..702be6846b5 100644 --- a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py +++ b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py @@ -174,7 +174,6 @@ class TestGetSupportedOpenAiParams: "top_p", "max_tokens", "max_completion_tokens", - "stop", "tools", "tool_choice", "functions", diff --git a/tests/unit/llms/gigachat/chat/test_transformation.py b/tests/unit/llms/gigachat/chat/test_transformation.py index 9116a4d1979..c54f6639c69 100644 --- a/tests/unit/llms/gigachat/chat/test_transformation.py +++ b/tests/unit/llms/gigachat/chat/test_transformation.py @@ -11,6 +11,7 @@ import importlib import pytest import litellm +from litellm.utils import UnsupportedParamsError, get_optional_params from tests._vcr_conftest_common import install_live_call_probe, record_vcr_outcome @@ -195,6 +196,69 @@ class TestGigaChatParamsTransformation: return GigaChatConfig() + + def test_stop_raises_unsupported_params_error(self): + with pytest.raises(UnsupportedParamsError): + get_optional_params( + model="GigaChat-Pro", + custom_llm_provider="gigachat", + stop=["\n\n"], + request_timeout=10, + num_retries=0, + ) + + def test_stop_dropped_with_drop_params_true(self): + result = get_optional_params( + model="GigaChat-Pro", + custom_llm_provider="gigachat", + stop=["\n\n"], + request_timeout=10, + num_retries=0, + drop_params=True, + ) + assert "stop" not in result + + def test_response_format_json_object_raises_unsupported(self, config): + with pytest.raises(UnsupportedParamsError): + config.map_openai_params( + non_default_params={"response_format": {"type": "json_object"}}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + + def test_response_format_json_object_dropped_with_drop_params(self, config): + result = config.map_openai_params( + non_default_params={"response_format": {"type": "json_object"}}, + optional_params={}, + model="GigaChat", + drop_params=True, + ) + assert "functions" not in result + assert "function_call" not in result + + def test_response_format_json_object_dropped_with_global_drop_params(self, config, monkeypatch): + monkeypatch.setattr(litellm, "drop_params", True, raising=False) + result = config.map_openai_params( + non_default_params={"response_format": {"type": "json_object"}}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert "functions" not in result + assert "function_call" not in result + + def test_response_format_json_object_raises_when_global_drop_params_off(self, config, monkeypatch): + monkeypatch.setattr(litellm, "drop_params", False, raising=False) + with pytest.raises(UnsupportedParamsError): + config.map_openai_params( + non_default_params={"response_format": {"type": "json_object"}}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + + def test_temperature_zero_becomes_top_p_zero(self, config): """temperature=0 should become top_p=0""" params = {"temperature": 0}