From 20df5ff7f0833397567a247bc69e9288acfe33a7 Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Tue, 29 Sep 2026 04:03:27 +0800 Subject: [PATCH 1/8] fix(gigachat): surface unsupported stop and json_object response_format instead of silently dropping them get_supported_openai_params declared 'stop' as supported while map_openai_params explicitly discarded it, so stop sequences vanished without error even with drop_params=False. response_format={'type': 'json_object'} had the same problem: declared supported, but only json_schema was mapped. - remove 'stop' from the supported list so get_optional_params raises UnsupportedParamsError by default (explicit drop with drop_params=True, consistent with litellm's contract) - raise UnsupportedParamsError for non-json_schema response_format values (json_object etc.) unless drop_params=True; 'text' stays a no-op - replace the test that locked the silent drop in as expected behavior; add contract tests for both params Fixes #43606 --- litellm/llms/gigachat/chat/transformation.py | 22 +++++-- .../chat/test_gigachat_chat_transformation.py | 61 ++++++++++++++++--- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 15a1b463c3f..81a77e25b44 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -142,7 +142,6 @@ class GigaChatConfig(BaseConfig): "top_p", "max_tokens", "max_completion_tokens", - "stop", "tools", "tool_choice", "functions", @@ -171,9 +170,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): @@ -212,6 +208,24 @@ class GigaChatConfig(BaseConfig): ] optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True + elif not ( + isinstance(value, Mapping) + and (value.get("type") in (None, "text", "json_schema")) + ): + # Only json_schema is actually mapped above; other response_format + # values (e.g. json_object) would otherwise be silently ignored. + if not drop_params: + from litellm.utils import UnsupportedParamsError + + 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 b1307f56336..26f13472ebb 100644 --- a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py +++ b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py @@ -19,6 +19,7 @@ from litellm.llms.gigachat.chat.transformation import ( is_valid_json, ) from litellm.types.utils import ModelResponse, Usage +from litellm.utils import UnsupportedParamsError, get_optional_params TRANSFORM_MODULE = "litellm.llms.gigachat.chat.transformation" @@ -174,7 +175,6 @@ class TestGetSupportedOpenAiParams: "top_p", "max_tokens", "max_completion_tokens", - "stop", "tools", "tool_choice", "functions", @@ -243,12 +243,30 @@ class TestMapOpenAiParams: ) assert result["max_tokens"] == 200 - def test_stop_is_dropped(self): - result = self.config.map_openai_params( - non_default_params={"stop": ["\n\n"]}, - optional_params={}, - model="GigaChat", - drop_params=False, + def test_stop_not_declared_supported(self): + params = self.config.get_supported_openai_params("GigaChat") + assert "stop" not in params + + def test_stop_raises_unsupported_params_error(self): + # stop is not supported by the GigaChat API; with drop_params=False + # (the default) litellm must surface that instead of silently ignoring it + 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 @@ -354,6 +372,35 @@ class TestMapOpenAiParams: assert result["function_call"] == {"name": "test_schema"} assert result["_structured_output"] is True + def test_response_format_json_object_raises_unsupported(self): + with pytest.raises(UnsupportedParamsError): + self.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): + result = self.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_text_is_a_noop(self): + result = self.config.map_openai_params( + non_default_params={"response_format": {"type": "text"}}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert "functions" not in result + assert "function_call" not in result + class TestConvertToolsToFunctions: def setup_method(self): From 8de7014ec777492027ab5fdf47354f037fc6e12f Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Tue, 29 Sep 2026 04:22:18 +0800 Subject: [PATCH 2/8] style: ruff format transformation.py --- litellm/llms/gigachat/chat/transformation.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 81a77e25b44..5a3cbbaf4a9 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -208,10 +208,7 @@ class GigaChatConfig(BaseConfig): ] optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True - elif not ( - isinstance(value, Mapping) - and (value.get("type") in (None, "text", "json_schema")) - ): + elif not (isinstance(value, Mapping) and (value.get("type") in (None, "text", "json_schema"))): # Only json_schema is actually mapped above; other response_format # values (e.g. json_object) would otherwise be silently ignored. if not drop_params: From 3c8adc3a66bc62a676d4ea1df93a6dce02cc026c Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Sun, 11 Oct 2026 18:43:55 +0800 Subject: [PATCH 3/8] fix(gigachat): honor the global litellm.drop_params for response_format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Greptile flagged this as a merge blocker and it is right: the `json_object` rejection checked only the per-call `drop_params` argument, so a user who had set `litellm.drop_params = True` globally still got an `UnsupportedParamsError` for `response_format={'type': 'json_object'}` — the opposite of what that setting means everywhere else in the codebase. `litellm/utils.py` and the Azure transformations all gate on litellm.drop_params is True or drop_params is True so this now uses the same test, and the import moved out of the branch since it is needed on both paths. Two tests cover it: with the global set the mapping returns cleanly, and with the global explicitly False the per-call `False` still raises, so a leftover global cannot mask a real unsupported parameter. --- litellm/llms/gigachat/chat/transformation.py | 10 ++++--- .../chat/test_gigachat_chat_transformation.py | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 5a3cbbaf4a9..525b39cbf33 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -15,6 +15,7 @@ from typing import TYPE_CHECKING, Any, Final import httpx +import litellm from litellm._logging import verbose_logger from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException from litellm.llms.gigachat.utils import convert_usage, get_api_base @@ -209,11 +210,12 @@ class GigaChatConfig(BaseConfig): optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True elif not (isinstance(value, Mapping) and (value.get("type") in (None, "text", "json_schema"))): - # Only json_schema is actually mapped above; other response_format - # values (e.g. json_object) would otherwise be silently ignored. - if not drop_params: - from litellm.utils import UnsupportedParamsError + # Only json_schema is mapped above; anything else (json_object, + # a missing schema, an unexpected shape) is silently dropped by + # GigaChat unless we say so. + from litellm.utils import UnsupportedParamsError + if litellm.drop_params is not True and drop_params is not True: raise UnsupportedParamsError( status_code=400, message=( 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 26f13472ebb..27849c147ec 100644 --- a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py +++ b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py @@ -391,6 +391,33 @@ class TestMapOpenAiParams: assert "functions" not in result assert "function_call" not in result + def test_response_format_json_object_dropped_with_global_drop_params(self, monkeypatch): + """The per-call flag is not the only switch: litellm.drop_params is global.""" + import litellm + + monkeypatch.setattr(litellm, "drop_params", True, raising=False) + result = self.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, monkeypatch): + """A global drop_params left over from elsewhere must not mask the error.""" + import litellm + + monkeypatch.setattr(litellm, "drop_params", False, raising=False) + with pytest.raises(UnsupportedParamsError): + self.config.map_openai_params( + non_default_params={"response_format": {"type": "json_object"}}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + def test_response_format_text_is_a_noop(self): result = self.config.map_openai_params( non_default_params={"response_format": {"type": "text"}}, From b165a3f3929b7ece4750bdc80eecb0898b7f56c1 Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Sun, 11 Oct 2026 18:59:09 +0800 Subject: [PATCH 4/8] test(gigachat): use the mapped test file and drop the structural assertion Greptile's two remaining findings, both against AGENTS.md: - the new file sat next to `chat/test_transformation.py`, the file `tests/unit/` maps `litellm/llms/gigachat/chat/transformation.py` onto. AGENTS.md says to extend the mapped file rather than create a new one. The behavioural tests move there as-is and `test_gigachat_chat_transformation.py` goes away. - `test_stop_not_declared_supported` asserted that "stop" is absent from `get_supported_openai_params()`. That pins the structure of our own list instead of any request outcome, which AGENTS.md rules out, and the two stop tests already cover the behaviour. Removed. The comment above the response_format branch restated the condition it sits on, which AGENTS.md also rules out. Removed; the `elif` is self-explanatory. 164 tests pass in `tests/unit/llms/gigachat/`. The branch is also rebased onto current `main`, which is where `test_transformation.py` came from. --- litellm/llms/gigachat/chat/transformation.py | 3 - .../chat/test_gigachat_chat_transformation.py | 948 ------------------ .../llms/gigachat/chat/test_transformation.py | 64 ++ 3 files changed, 64 insertions(+), 951 deletions(-) delete mode 100644 tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 525b39cbf33..f3e4b8b22cf 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -210,9 +210,6 @@ class GigaChatConfig(BaseConfig): optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True elif not (isinstance(value, Mapping) and (value.get("type") in (None, "text", "json_schema"))): - # Only json_schema is mapped above; anything else (json_object, - # a missing schema, an unexpected shape) is silently dropped by - # GigaChat unless we say so. from litellm.utils import UnsupportedParamsError if litellm.drop_params is not True and drop_params is not True: diff --git a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py deleted file mode 100644 index 27849c147ec..00000000000 --- a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py +++ /dev/null @@ -1,948 +0,0 @@ -""" -Unit tests for GigaChat chat transformation. - -Tests GigaChatConfig covering get_complete_url, validate_environment, -get_supported_openai_params, map_openai_params, _convert_tools_to_functions, -_map_tool_choice, _transform_messages, transform_request, transform_response, -get_model_response_iterator, and get_error_class. -""" - -import json -from unittest.mock import MagicMock, patch - -import httpx -import pytest - -from litellm.llms.gigachat.chat.transformation import ( - GigaChatConfig, - GigaChatError, - is_valid_json, -) -from litellm.types.utils import ModelResponse, Usage -from litellm.utils import UnsupportedParamsError, get_optional_params - -TRANSFORM_MODULE = "litellm.llms.gigachat.chat.transformation" - - -def _make_httpx_response( - body: dict, status_code: int = 200 -) -> httpx.Response: - return httpx.Response( - status_code=status_code, - headers={"content-type": "application/json"}, - content=json.dumps(body).encode("utf-8"), - request=httpx.Request( - "POST", - "https://gigachat.devices.sberbank.ru/api/v1/chat/completions", - ), - ) - - -# --------------------------------------------------------------------------- -# is_valid_json -# --------------------------------------------------------------------------- - - -class TestIsValidJson: - def test_valid_json_object(self): - assert is_valid_json('{"key": "value"}') is True - - def test_valid_json_array(self): - assert is_valid_json("[1, 2, 3]") is True - - def test_valid_json_string(self): - assert is_valid_json('"hello"') is True - - def test_invalid_json(self): - assert is_valid_json("{invalid}") is False - - def test_empty_string(self): - assert is_valid_json("") is False - - -# --------------------------------------------------------------------------- -# GigaChatConfig -# --------------------------------------------------------------------------- - - -class TestGetCompleteUrl: - def setup_method(self): - self.config = GigaChatConfig() - - def test_uses_api_base_from_param(self): - url = self.config.get_complete_url( - api_base="https://custom.example.com", - api_key=None, - model="GigaChat", - optional_params={}, - litellm_params={}, - stream=False, - ) - assert url == "https://custom.example.com/chat/completions" - - def test_uses_api_base_with_trailing_slash(self): - url = self.config.get_complete_url( - api_base="https://custom.example.com/", - api_key=None, - model="GigaChat", - optional_params={}, - litellm_params={}, - stream=False, - ) - # get_api_base passes the value through without stripping the slash - assert url == "https://custom.example.com//chat/completions" - - def test_uses_api_base_from_get_api_base_when_none(self): - url = self.config.get_complete_url( - api_base=None, - api_key=None, - model="GigaChat", - optional_params={}, - litellm_params={}, - stream=False, - ) - assert url.endswith("/chat/completions") - - -class TestValidateEnvironment: - def setup_method(self): - self.config = GigaChatConfig() - - @patch(f"{TRANSFORM_MODULE}.get_access_token", return_value="test-token") - @patch(f"{TRANSFORM_MODULE}.get_secret_str", return_value=None) - def test_sets_auth_headers(self, mock_get_secret, mock_get_token): - headers: dict = {} - result = self.config.validate_environment( - headers=headers, - model="GigaChat", - messages=[{"role": "user", "content": "hi"}], - optional_params={}, - litellm_params={}, - api_key="creds", - api_base="https://api.example.com", - ) - assert result["Authorization"] == "Bearer test-token" - assert result["Content-Type"] == "application/json" - assert result["Accept"] == "application/json" - - @patch(f"{TRANSFORM_MODULE}.get_access_token", return_value="token") - @patch(f"{TRANSFORM_MODULE}.get_secret_str", return_value=None) - def test_stores_credentials_and_api_base_for_image_uploads( - self, mock_get_secret, mock_get_token - ): - self.config.validate_environment( - headers={}, - model="GigaChat", - messages=[], - optional_params={}, - litellm_params={}, - api_key="my-creds", - api_base="https://my-api.example.com", - ) - assert self.config._current_credentials == "my-creds" - assert self.config._current_api_base == "https://my-api.example.com" - - @patch( - f"{TRANSFORM_MODULE}.get_access_token", - side_effect=lambda credentials, litellm_params: f"token-for-{credentials}", - ) - def test_falls_back_to_env_credentials_when_api_key_missing( - self, mock_get_token, monkeypatch: pytest.MonkeyPatch - ): - monkeypatch.setenv("GIGACHAT_CREDENTIALS", "env-creds") - result = self.config.validate_environment( - headers={}, - model="GigaChat", - messages=[], - optional_params={}, - litellm_params={}, - api_key=None, - api_base=None, - ) - assert result["Authorization"] == "Bearer token-for-env-creds" - assert self.config._current_credentials == "env-creds" - - -class TestGetSupportedOpenAiParams: - def setup_method(self): - self.config = GigaChatConfig() - - def test_returns_expected_params(self): - params = self.config.get_supported_openai_params("GigaChat") - expected = [ - "stream", - "temperature", - "top_p", - "max_tokens", - "max_completion_tokens", - "tools", - "tool_choice", - "functions", - "function_call", - "response_format", - ] - assert params == expected - - -class TestMapOpenAiParams: - def setup_method(self): - self.config = GigaChatConfig() - - def test_stream(self): - result = self.config.map_openai_params( - non_default_params={"stream": True}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["stream"] is True - - def test_temperature_zero_maps_to_top_p_zero(self): - result = self.config.map_openai_params( - non_default_params={"temperature": 0}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["top_p"] == 0 - assert "temperature" not in result - - def test_temperature_non_zero(self): - result = self.config.map_openai_params( - non_default_params={"temperature": 0.7}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["temperature"] == 0.7 - - def test_top_p(self): - result = self.config.map_openai_params( - non_default_params={"top_p": 0.5}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["top_p"] == 0.5 - - def test_max_tokens(self): - result = self.config.map_openai_params( - non_default_params={"max_tokens": 100}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["max_tokens"] == 100 - - def test_max_completion_tokens(self): - result = self.config.map_openai_params( - non_default_params={"max_completion_tokens": 200}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["max_tokens"] == 200 - - def test_stop_not_declared_supported(self): - params = self.config.get_supported_openai_params("GigaChat") - assert "stop" not in params - - def test_stop_raises_unsupported_params_error(self): - # stop is not supported by the GigaChat API; with drop_params=False - # (the default) litellm must surface that instead of silently ignoring it - 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_tools_converted_to_functions(self): - tools = [ - { - "type": "function", - "function": { - "name": "get_weather", - "description": "Get weather", - "parameters": {"type": "object"}, - }, - } - ] - result = self.config.map_openai_params( - non_default_params={"tools": tools}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert "functions" in result - assert result["functions"] == [ - {"name": "get_weather", "description": "Get weather", "parameters": {"type": "object"}} - ] - - def test_tool_choice_auto(self): - result = self.config.map_openai_params( - non_default_params={"tool_choice": "auto"}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result.get("function_call") == "auto" - - def test_tool_choice_none(self): - result = self.config.map_openai_params( - non_default_params={"tool_choice": "none"}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result.get("function_call") == "none" - - def test_tool_choice_required(self): - result = self.config.map_openai_params( - non_default_params={"tool_choice": "required"}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result.get("function_call") == "auto" - - def test_tool_choice_dict(self): - result = self.config.map_openai_params( - non_default_params={ - "tool_choice": { - "type": "function", - "function": {"name": "get_weather"}, - } - }, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result.get("function_call") == {"name": "get_weather"} - - def test_functions(self): - funcs = [{"name": "my_func", "description": "desc", "parameters": {}}] - result = self.config.map_openai_params( - non_default_params={"functions": funcs}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["functions"] == funcs - - def test_function_call(self): - result = self.config.map_openai_params( - non_default_params={"function_call": {"name": "my_func"}}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert result["function_call"] == {"name": "my_func"} - - def test_response_format_json_schema(self): - response_format = { - "type": "json_schema", - "json_schema": { - "name": "test_schema", - "schema": {"type": "object", "properties": {"name": {"type": "string"}}}, - }, - } - result = self.config.map_openai_params( - non_default_params={"response_format": response_format}, - optional_params={"functions": []}, - model="GigaChat", - drop_params=False, - ) - # Should add a function for the schema - assert len(result["functions"]) == 1 - assert result["functions"][0]["name"] == "test_schema" - assert result["function_call"] == {"name": "test_schema"} - assert result["_structured_output"] is True - - def test_response_format_json_object_raises_unsupported(self): - with pytest.raises(UnsupportedParamsError): - self.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): - result = self.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, monkeypatch): - """The per-call flag is not the only switch: litellm.drop_params is global.""" - import litellm - - monkeypatch.setattr(litellm, "drop_params", True, raising=False) - result = self.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, monkeypatch): - """A global drop_params left over from elsewhere must not mask the error.""" - import litellm - - monkeypatch.setattr(litellm, "drop_params", False, raising=False) - with pytest.raises(UnsupportedParamsError): - self.config.map_openai_params( - non_default_params={"response_format": {"type": "json_object"}}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - - def test_response_format_text_is_a_noop(self): - result = self.config.map_openai_params( - non_default_params={"response_format": {"type": "text"}}, - optional_params={}, - model="GigaChat", - drop_params=False, - ) - assert "functions" not in result - assert "function_call" not in result - - -class TestConvertToolsToFunctions: - def setup_method(self): - self.config = GigaChatConfig() - - def test_converts_function_tools_only(self): - tools = [ - {"type": "function", "function": {"name": "a", "description": "d", "parameters": {}}}, - {"type": "code_interpreter"}, # should be ignored - ] - result = self.config._convert_tools_to_functions(tools) - assert len(result) == 1 - assert result[0]["name"] == "a" - - def test_empty_tools(self): - assert self.config._convert_tools_to_functions([]) == [] - - -class TestMapToolChoice: - def setup_method(self): - self.config = GigaChatConfig() - - def test_none(self): - assert self.config._map_tool_choice("none") == "none" - - def test_auto(self): - assert self.config._map_tool_choice("auto") == "auto" - - def test_required(self): - assert self.config._map_tool_choice("required") == "auto" - - def test_dict_with_function(self): - result = self.config._map_tool_choice( - {"type": "function", "function": {"name": "get_weather"}} - ) - assert result == {"name": "get_weather"} - - def test_dict_without_name(self): - result = self.config._map_tool_choice( - {"type": "function", "function": {}} - ) - assert result is None - - def test_unknown_value(self): - assert self.config._map_tool_choice("unknown") is None - - -class TestTransformMessages: - def setup_method(self): - self.config = GigaChatConfig() - - def test_developer_role_to_system(self): - result = self.config._transform_messages( - [{"role": "developer", "content": "be helpful"}] - ) - assert result[0]["role"] == "system" - assert result[0]["content"] == "be helpful" - - def test_system_message_not_first_becomes_user(self): - result = self.config._transform_messages([ - {"role": "user", "content": "hi"}, - {"role": "system", "content": "instruction"}, - ]) - assert result[0]["role"] == "user" - assert result[1]["role"] == "user" - assert result[1]["content"] == "instruction" - - def test_tool_role_to_function(self): - result = self.config._transform_messages([ - {"role": "tool", "content": '{"result": "ok"}'} - ]) - assert result[0]["role"] == "function" - - def test_tool_role_content_wraps_non_json(self): - result = self.config._transform_messages([ - {"role": "tool", "content": "plain text"} - ]) - assert result[0]["role"] == "function" - assert is_valid_json(result[0]["content"]) - - def test_none_content_becomes_empty_string(self): - result = self.config._transform_messages([ - {"role": "user", "content": None} - ]) - assert result[0]["content"] == "" - - def test_name_field_removed(self): - result = self.config._transform_messages([ - {"role": "user", "content": "hi", "name": "John"} - ]) - assert "name" not in result[0] - - def test_tool_calls_converted_to_function_call(self): - result = self.config._transform_messages([ - { - "role": "assistant", - "content": "", - "tool_calls": [ - { - "id": "call_abc", - "type": "function", - "function": { - "name": "get_weather", - "arguments": '{"city": "London"}', - }, - } - ], - } - ]) - assert "tool_calls" not in result[0] - assert result[0]["function_call"]["name"] == "get_weather" - assert result[0]["function_call"]["arguments"] == {"city": "London"} - - def test_tool_calls_with_dict_arguments(self): - result = self.config._transform_messages([ - { - "role": "assistant", - "content": "", - "tool_calls": [ - { - "id": "call_xyz", - "type": "function", - "function": { - "name": "search", - "arguments": {"query": "test"}, - }, - } - ], - } - ]) - assert result[0]["function_call"]["arguments"] == {"query": "test"} - - def test_list_content_multimodal(self): - content = [ - {"type": "text", "text": "describe this"}, - { - "type": "image_url", - "image_url": {"url": "https://example.com/img.jpg"}, - }, - ] - with patch.object(self.config, "_upload_image", return_value="file-123"): - result = self.config._transform_messages([ - {"role": "user", "content": content} - ]) - assert result[0]["content"] == "describe this" - assert result[0]["attachments"] == ["file-123"] - - def test_list_content_with_image_url_string(self): - content = [ - {"type": "text", "text": "look"}, - {"type": "image_url", "image_url": "https://example.com/img.jpg"}, - ] - with patch.object(self.config, "_upload_image", return_value="file-456"): - result = self.config._transform_messages([ - {"role": "user", "content": content} - ]) - assert result[0]["content"] == "look" - assert "file-456" in result[0]["attachments"] - - -class TestTransformRequest: - def setup_method(self): - self.config = GigaChatConfig() - - def test_builds_basic_request(self): - body = self.config.transform_request( - model="gigachat/GigaChat", - messages=[{"role": "user", "content": "hi"}], - optional_params={}, - litellm_params={}, - headers={}, - ) - assert body["model"] == "GigaChat" - assert len(body["messages"]) == 1 - assert body["messages"][0]["content"] == "hi" - - def test_model_prefix_stripped(self): - body = self.config.transform_request( - model="gigachat/GigaChat-Pro", - messages=[{"role": "user", "content": "hello"}], - optional_params={}, - litellm_params={}, - headers={}, - ) - assert body["model"] == "GigaChat-Pro" - - def test_includes_optional_params(self): - body = self.config.transform_request( - model="gigachat/GigaChat", - messages=[{"role": "user", "content": "hi"}], - optional_params={ - "temperature": 0.5, - "max_tokens": 100, - "stream": True, - }, - litellm_params={}, - headers={}, - ) - assert body["temperature"] == 0.5 - assert body["max_tokens"] == 100 - assert body["stream"] is True - - def test_includes_functions(self): - body = self.config.transform_request( - model="gigachat/GigaChat", - messages=[{"role": "user", "content": "hi"}], - optional_params={ - "functions": [{"name": "my_func"}], - "function_call": {"name": "my_func"}, - }, - litellm_params={}, - headers={}, - ) - assert body["functions"] == [{"name": "my_func"}] - assert body["function_call"] == {"name": "my_func"} - - def test_skips_unsupported_params(self): - body = self.config.transform_request( - model="gigachat/GigaChat", - messages=[{"role": "user", "content": "hi"}], - optional_params={"n": 2, "user": "abc"}, - litellm_params={}, - headers={}, - ) - assert "n" not in body - assert "user" not in body - - -class TestTransformResponse: - def setup_method(self): - self.config = GigaChatConfig() - - def test_basic_response(self): - raw = _make_httpx_response({ - "id": "chatcmpl-123", - "created": 1700000000, - "model": "GigaChat", - "choices": [ - { - "index": 0, - "message": {"role": "assistant", "content": "Hello!"}, - "finish_reason": "stop", - } - ], - "usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - assert result.choices[0].message.content == "Hello!" - assert result.choices[0].finish_reason == "stop" - assert result.usage.prompt_tokens == 5 - assert result.usage.total_tokens == 8 - - def test_function_call_into_tool_calls(self): - raw = _make_httpx_response({ - "id": "chatcmpl-456", - "created": 1700000000, - "model": "GigaChat", - "choices": [ - { - "index": 0, - "message": { - "role": "assistant", - "content": None, - "function_call": { - "name": "get_weather", - "arguments": {"city": "Moscow"}, - }, - }, - "finish_reason": "function_call", - } - ], - "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - assert result.choices[0].finish_reason == "tool_calls" - tool_calls = result.choices[0].message.tool_calls - assert tool_calls is not None - assert len(tool_calls) == 1 - assert tool_calls[0].function.name == "get_weather" - assert '{"city": "Moscow"}' in tool_calls[0].function.arguments - - def test_function_call_structured_output(self): - raw = _make_httpx_response({ - "id": "chatcmpl-789", - "created": 1700000000, - "model": "GigaChat", - "choices": [ - { - "index": 0, - "message": { - "role": "assistant", - "content": None, - "function_call": { - "name": "test_schema", - "arguments": {"name": "John"}, - }, - }, - "finish_reason": "function_call", - } - ], - "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={"_structured_output": True}, - litellm_params={}, - encoding=None, - ) - # Structured output: function_call -> content - assert result.choices[0].finish_reason == "stop" - assert result.choices[0].message.content is not None - assert '"name": "John"' in result.choices[0].message.content - - def test_function_call_string_arguments(self): - raw = _make_httpx_response({ - "choices": [ - { - "index": 0, - "message": { - "role": "assistant", - "function_call": { - "name": "get_weather", - "arguments": '{"city": "Moscow"}', - }, - }, - "finish_reason": "function_call", - } - ], - "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - tc = result.choices[0].message.tool_calls[0] - assert '{"city": "Moscow"}' in tc.function.arguments - - def test_cleans_up_gigachat_specific_fields(self): - raw = _make_httpx_response({ - "choices": [ - { - "index": 0, - "message": { - "role": "assistant", - "content": "done", - "functions_state_id": "some-state", - }, - "finish_reason": "stop", - } - ], - "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - # functions_state_id should have been removed from the message data - assert result.choices[0].message.content == "done" - - def test_raises_on_invalid_json(self): - raw = httpx.Response( - status_code=500, - headers={"content-type": "text/plain"}, - content=b"not json", - request=httpx.Request("POST", "https://example.com"), - ) - model_response = ModelResponse() - with pytest.raises(GigaChatError) as exc_info: - self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - assert "Invalid JSON response" in str(exc_info.value.message) - - def test_empty_choices(self): - raw = _make_httpx_response({ - "choices": [], - "usage": {}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - assert result.choices == [] - - def test_function_call_with_non_dict_arguments(self): - raw = _make_httpx_response({ - "choices": [ - { - "index": 0, - "message": { - "role": "assistant", - "function_call": { - "name": "say_hello", - "arguments": "hello", - }, - }, - "finish_reason": "function_call", - } - ], - "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, - }) - model_response = ModelResponse() - result = self.config.transform_response( - model="gigachat/GigaChat", - raw_response=raw, - model_response=model_response, - logging_obj=MagicMock(), - request_data={}, - messages=[], - optional_params={}, - litellm_params={}, - encoding=None, - ) - tc = result.choices[0].message.tool_calls[0] - assert tc.function.arguments == "hello" - - -class TestGetModelResponseIterator: - def setup_method(self): - self.config = GigaChatConfig() - - def test_returns_gigachat_iterator_sync(self): - from litellm.llms.gigachat.chat.streaming import ( - GigaChatModelResponseIterator, - ) - - result = self.config.get_model_response_iterator( - streaming_response=iter(["data"]), - sync_stream=True, - json_mode=False, - ) - assert isinstance(result, GigaChatModelResponseIterator) - - -class TestGetErrorClass: - def setup_method(self): - self.config = GigaChatConfig() - - def test_returns_gigachat_error(self): - error = self.config.get_error_class( - error_message="something went wrong", - status_code=400, - headers={"x-request-id": "abc"}, - ) - assert isinstance(error, GigaChatError) - assert error.status_code == 400 - assert error.message == "something went wrong" - assert error.headers == {"x-request-id": "abc"} - - -class TestUploadImage: - def setup_method(self): - self.config = GigaChatConfig() - - @patch(f"{TRANSFORM_MODULE}.upload_file_sync", side_effect=Exception("fail")) - def test_upload_image_failure_returns_none(self, mock_upload): - result = self.config._upload_image("https://example.com/img.jpg") - assert result is None \ No newline at end of file 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} From f2d430643301a23bb56cfe880de49fcb57881f74 Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:12:15 +0800 Subject: [PATCH 5/8] type_check_gate: keep the response_format check at its baseline count The guard read `value.get("type")` inline, where `value` arrives as `object` and `isinstance(value, Mapping)` narrows it to a bare `Mapping`, so `get()` came back partially unknown and put the file one `reportUnknownMemberType` over its merge-base ceiling. Extracting it into `_is_supported_response_format` puts the narrowing on one `object`-typed local, so the single unavoidable suppression sits there instead of on the branch. Checked against the original expression over a json_schema with a schema, text, an empty dict, json_object, a bogus type, a string, and None, and the results match in every case --- litellm/llms/gigachat/chat/transformation.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index f3e4b8b22cf..6f80b890b0c 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -52,6 +52,13 @@ class GigaChatError(BaseLLMException): """GigaChat API error.""" +def _is_supported_response_format(value: object) -> bool: + if not isinstance(value, Mapping): + return False + response_format_type: Final = value.get("type") # pyright: ignore[reportUnknownMemberType] # bare Mapping + return response_format_type in (None, "text", "json_schema") + + class GigaChatConfig(BaseConfig): """ Configuration class for GigaChat API. @@ -209,7 +216,7 @@ class GigaChatConfig(BaseConfig): ] optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True - elif not (isinstance(value, Mapping) and (value.get("type") in (None, "text", "json_schema"))): + elif not _is_supported_response_format(value): from litellm.utils import UnsupportedParamsError if litellm.drop_params is not True and drop_params is not True: From eafae887422d3c833c9bc076449be50d8b75fa39 Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:25:28 +0800 Subject: [PATCH 6/8] type_check_gate: cast the response_format mapping instead of narrowing it Three attempts at this did not clear the ceiling. Narrowing an `object` to `Mapping` or to `dict` with isinstance leaves every `.get()` on it partially unknown, so the errors move rather than go away, and putting the ignore on the branch did not fit in 120 columns. `cast("dict[str, object]", value)` gives the value a known type instead, which leaves `get()` returning `object | None` and the file back at its baseline count. Two suppressions remain, each stating what it covers: the cast, and the call site where `value` comes out of the untyped `non_default_params` loop. 164 tests pass and the strict, type-discipline and test-quality gates are all within their ceilings --- litellm/llms/gigachat/chat/transformation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 6f80b890b0c..6d7908a6e85 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -11,7 +11,7 @@ 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 import httpx @@ -55,8 +55,8 @@ class GigaChatError(BaseLLMException): def _is_supported_response_format(value: object) -> bool: if not isinstance(value, Mapping): return False - response_format_type: Final = value.get("type") # pyright: ignore[reportUnknownMemberType] # bare Mapping - return response_format_type in (None, "text", "json_schema") + 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): @@ -216,7 +216,7 @@ class GigaChatConfig(BaseConfig): ] optional_params["function_call"] = {"name": schema_name} optional_params["_structured_output"] = True - elif not _is_supported_response_format(value): + 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: From daf51cc8782a66283b823e9de1dbf83d79daea02 Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:28:46 +0800 Subject: [PATCH 7/8] TID251 bans typing.cast, so say why this one is allowed ruff_strict_gate counts TID251 against the merge-base and the new cast import put the file one over. The config's own note for that rule allows a suppression with a reason, and the reasoning is narrow: response_format reaches us as an arbitrary JSON object from the provider, and the branch immediately reads one key off it and compares against three literal strings. The stricter alternative in the rule's message is a pydantic boundary model. That would be the right call if these values were persisted or forwarded to a caller, and it is not here. strict-rule counts now match the merge-base exactly. 164 tests pass and the type-discipline and test-quality gates stay within their ceilings. --- litellm/llms/gigachat/chat/transformation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 6d7908a6e85..bc6a6673860 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -11,7 +11,7 @@ import time import uuid from collections.abc import AsyncIterator, Iterator, Mapping, Sequence from types import MappingProxyType -from typing import TYPE_CHECKING, Any, Final, cast +from typing import TYPE_CHECKING, Any, Final, cast # noqa: TID251 # response_format is a plain JSON object import httpx From 7ada3370df53e9c553636c57bf3752748d7e1b8a Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Mon, 12 Oct 2026 01:08:44 +0800 Subject: [PATCH 8/8] test(gigachat): keep the existing chat transformation suite An earlier commit on this branch removed test_gigachat_chat_transformation.py on the grounds that test_transformation.py is the mapped filename. That file held 61 tests and 52 of them appear nowhere else, including the only coverage of auth headers, api_base resolution and credential fallback. Deleting it was a coverage loss rather than a de-duplication. Restore it and update the one assertion that encodes the old contract: stop is no longer advertised as supported now that it raises instead of being dropped. --- .../chat/test_gigachat_chat_transformation.py | 873 ++++++++++++++++++ 1 file changed, 873 insertions(+) create mode 100644 tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py diff --git a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py new file mode 100644 index 00000000000..e24933c358f --- /dev/null +++ b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py @@ -0,0 +1,873 @@ +""" +Unit tests for GigaChat chat transformation. + +Tests GigaChatConfig covering get_complete_url, validate_environment, +get_supported_openai_params, map_openai_params, _convert_tools_to_functions, +_map_tool_choice, _transform_messages, transform_request, transform_response, +get_model_response_iterator, and get_error_class. +""" + +import json +from unittest.mock import MagicMock, patch + +import httpx +import pytest + +from litellm.llms.gigachat.chat.transformation import ( + GigaChatConfig, + GigaChatError, + is_valid_json, +) +from litellm.types.utils import ModelResponse, Usage + +TRANSFORM_MODULE = "litellm.llms.gigachat.chat.transformation" + + +def _make_httpx_response( + body: dict, status_code: int = 200 +) -> httpx.Response: + return httpx.Response( + status_code=status_code, + headers={"content-type": "application/json"}, + content=json.dumps(body).encode("utf-8"), + request=httpx.Request( + "POST", + "https://gigachat.devices.sberbank.ru/api/v1/chat/completions", + ), + ) + + +# --------------------------------------------------------------------------- +# is_valid_json +# --------------------------------------------------------------------------- + + +class TestIsValidJson: + def test_valid_json_object(self): + assert is_valid_json('{"key": "value"}') is True + + def test_valid_json_array(self): + assert is_valid_json("[1, 2, 3]") is True + + def test_valid_json_string(self): + assert is_valid_json('"hello"') is True + + def test_invalid_json(self): + assert is_valid_json("{invalid}") is False + + def test_empty_string(self): + assert is_valid_json("") is False + + +# --------------------------------------------------------------------------- +# GigaChatConfig +# --------------------------------------------------------------------------- + + +class TestGetCompleteUrl: + def setup_method(self): + self.config = GigaChatConfig() + + def test_uses_api_base_from_param(self): + url = self.config.get_complete_url( + api_base="https://custom.example.com", + api_key=None, + model="GigaChat", + optional_params={}, + litellm_params={}, + stream=False, + ) + assert url == "https://custom.example.com/chat/completions" + + def test_uses_api_base_with_trailing_slash(self): + url = self.config.get_complete_url( + api_base="https://custom.example.com/", + api_key=None, + model="GigaChat", + optional_params={}, + litellm_params={}, + stream=False, + ) + # get_api_base passes the value through without stripping the slash + assert url == "https://custom.example.com//chat/completions" + + def test_uses_api_base_from_get_api_base_when_none(self): + url = self.config.get_complete_url( + api_base=None, + api_key=None, + model="GigaChat", + optional_params={}, + litellm_params={}, + stream=False, + ) + assert url.endswith("/chat/completions") + + +class TestValidateEnvironment: + def setup_method(self): + self.config = GigaChatConfig() + + @patch(f"{TRANSFORM_MODULE}.get_access_token", return_value="test-token") + @patch(f"{TRANSFORM_MODULE}.get_secret_str", return_value=None) + def test_sets_auth_headers(self, mock_get_secret, mock_get_token): + headers: dict = {} + result = self.config.validate_environment( + headers=headers, + model="GigaChat", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key="creds", + api_base="https://api.example.com", + ) + assert result["Authorization"] == "Bearer test-token" + assert result["Content-Type"] == "application/json" + assert result["Accept"] == "application/json" + + @patch(f"{TRANSFORM_MODULE}.get_access_token", return_value="token") + @patch(f"{TRANSFORM_MODULE}.get_secret_str", return_value=None) + def test_stores_credentials_and_api_base_for_image_uploads( + self, mock_get_secret, mock_get_token + ): + self.config.validate_environment( + headers={}, + model="GigaChat", + messages=[], + optional_params={}, + litellm_params={}, + api_key="my-creds", + api_base="https://my-api.example.com", + ) + assert self.config._current_credentials == "my-creds" + assert self.config._current_api_base == "https://my-api.example.com" + + @patch( + f"{TRANSFORM_MODULE}.get_access_token", + side_effect=lambda credentials, litellm_params: f"token-for-{credentials}", + ) + def test_falls_back_to_env_credentials_when_api_key_missing( + self, mock_get_token, monkeypatch: pytest.MonkeyPatch + ): + monkeypatch.setenv("GIGACHAT_CREDENTIALS", "env-creds") + result = self.config.validate_environment( + headers={}, + model="GigaChat", + messages=[], + optional_params={}, + litellm_params={}, + api_key=None, + api_base=None, + ) + assert result["Authorization"] == "Bearer token-for-env-creds" + assert self.config._current_credentials == "env-creds" + + +class TestGetSupportedOpenAiParams: + def setup_method(self): + self.config = GigaChatConfig() + + def test_returns_expected_params(self): + params = self.config.get_supported_openai_params("GigaChat") + expected = [ + "stream", + "temperature", + "top_p", + "max_tokens", + "max_completion_tokens", + "tools", + "tool_choice", + "functions", + "function_call", + "response_format", + ] + assert params == expected + + +class TestMapOpenAiParams: + def setup_method(self): + self.config = GigaChatConfig() + + def test_stream(self): + result = self.config.map_openai_params( + non_default_params={"stream": True}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["stream"] is True + + def test_temperature_zero_maps_to_top_p_zero(self): + result = self.config.map_openai_params( + non_default_params={"temperature": 0}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["top_p"] == 0 + assert "temperature" not in result + + def test_temperature_non_zero(self): + result = self.config.map_openai_params( + non_default_params={"temperature": 0.7}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["temperature"] == 0.7 + + def test_top_p(self): + result = self.config.map_openai_params( + non_default_params={"top_p": 0.5}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["top_p"] == 0.5 + + def test_max_tokens(self): + result = self.config.map_openai_params( + non_default_params={"max_tokens": 100}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["max_tokens"] == 100 + + def test_max_completion_tokens(self): + result = self.config.map_openai_params( + non_default_params={"max_completion_tokens": 200}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["max_tokens"] == 200 + + def test_stop_is_dropped(self): + result = self.config.map_openai_params( + non_default_params={"stop": ["\n\n"]}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert "stop" not in result + + def test_tools_converted_to_functions(self): + tools = [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get weather", + "parameters": {"type": "object"}, + }, + } + ] + result = self.config.map_openai_params( + non_default_params={"tools": tools}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert "functions" in result + assert result["functions"] == [ + {"name": "get_weather", "description": "Get weather", "parameters": {"type": "object"}} + ] + + def test_tool_choice_auto(self): + result = self.config.map_openai_params( + non_default_params={"tool_choice": "auto"}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result.get("function_call") == "auto" + + def test_tool_choice_none(self): + result = self.config.map_openai_params( + non_default_params={"tool_choice": "none"}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result.get("function_call") == "none" + + def test_tool_choice_required(self): + result = self.config.map_openai_params( + non_default_params={"tool_choice": "required"}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result.get("function_call") == "auto" + + def test_tool_choice_dict(self): + result = self.config.map_openai_params( + non_default_params={ + "tool_choice": { + "type": "function", + "function": {"name": "get_weather"}, + } + }, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result.get("function_call") == {"name": "get_weather"} + + def test_functions(self): + funcs = [{"name": "my_func", "description": "desc", "parameters": {}}] + result = self.config.map_openai_params( + non_default_params={"functions": funcs}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["functions"] == funcs + + def test_function_call(self): + result = self.config.map_openai_params( + non_default_params={"function_call": {"name": "my_func"}}, + optional_params={}, + model="GigaChat", + drop_params=False, + ) + assert result["function_call"] == {"name": "my_func"} + + def test_response_format_json_schema(self): + response_format = { + "type": "json_schema", + "json_schema": { + "name": "test_schema", + "schema": {"type": "object", "properties": {"name": {"type": "string"}}}, + }, + } + result = self.config.map_openai_params( + non_default_params={"response_format": response_format}, + optional_params={"functions": []}, + model="GigaChat", + drop_params=False, + ) + # Should add a function for the schema + assert len(result["functions"]) == 1 + assert result["functions"][0]["name"] == "test_schema" + assert result["function_call"] == {"name": "test_schema"} + assert result["_structured_output"] is True + + +class TestConvertToolsToFunctions: + def setup_method(self): + self.config = GigaChatConfig() + + def test_converts_function_tools_only(self): + tools = [ + {"type": "function", "function": {"name": "a", "description": "d", "parameters": {}}}, + {"type": "code_interpreter"}, # should be ignored + ] + result = self.config._convert_tools_to_functions(tools) + assert len(result) == 1 + assert result[0]["name"] == "a" + + def test_empty_tools(self): + assert self.config._convert_tools_to_functions([]) == [] + + +class TestMapToolChoice: + def setup_method(self): + self.config = GigaChatConfig() + + def test_none(self): + assert self.config._map_tool_choice("none") == "none" + + def test_auto(self): + assert self.config._map_tool_choice("auto") == "auto" + + def test_required(self): + assert self.config._map_tool_choice("required") == "auto" + + def test_dict_with_function(self): + result = self.config._map_tool_choice( + {"type": "function", "function": {"name": "get_weather"}} + ) + assert result == {"name": "get_weather"} + + def test_dict_without_name(self): + result = self.config._map_tool_choice( + {"type": "function", "function": {}} + ) + assert result is None + + def test_unknown_value(self): + assert self.config._map_tool_choice("unknown") is None + + +class TestTransformMessages: + def setup_method(self): + self.config = GigaChatConfig() + + def test_developer_role_to_system(self): + result = self.config._transform_messages( + [{"role": "developer", "content": "be helpful"}] + ) + assert result[0]["role"] == "system" + assert result[0]["content"] == "be helpful" + + def test_system_message_not_first_becomes_user(self): + result = self.config._transform_messages([ + {"role": "user", "content": "hi"}, + {"role": "system", "content": "instruction"}, + ]) + assert result[0]["role"] == "user" + assert result[1]["role"] == "user" + assert result[1]["content"] == "instruction" + + def test_tool_role_to_function(self): + result = self.config._transform_messages([ + {"role": "tool", "content": '{"result": "ok"}'} + ]) + assert result[0]["role"] == "function" + + def test_tool_role_content_wraps_non_json(self): + result = self.config._transform_messages([ + {"role": "tool", "content": "plain text"} + ]) + assert result[0]["role"] == "function" + assert is_valid_json(result[0]["content"]) + + def test_none_content_becomes_empty_string(self): + result = self.config._transform_messages([ + {"role": "user", "content": None} + ]) + assert result[0]["content"] == "" + + def test_name_field_removed(self): + result = self.config._transform_messages([ + {"role": "user", "content": "hi", "name": "John"} + ]) + assert "name" not in result[0] + + def test_tool_calls_converted_to_function_call(self): + result = self.config._transform_messages([ + { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "id": "call_abc", + "type": "function", + "function": { + "name": "get_weather", + "arguments": '{"city": "London"}', + }, + } + ], + } + ]) + assert "tool_calls" not in result[0] + assert result[0]["function_call"]["name"] == "get_weather" + assert result[0]["function_call"]["arguments"] == {"city": "London"} + + def test_tool_calls_with_dict_arguments(self): + result = self.config._transform_messages([ + { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "id": "call_xyz", + "type": "function", + "function": { + "name": "search", + "arguments": {"query": "test"}, + }, + } + ], + } + ]) + assert result[0]["function_call"]["arguments"] == {"query": "test"} + + def test_list_content_multimodal(self): + content = [ + {"type": "text", "text": "describe this"}, + { + "type": "image_url", + "image_url": {"url": "https://example.com/img.jpg"}, + }, + ] + with patch.object(self.config, "_upload_image", return_value="file-123"): + result = self.config._transform_messages([ + {"role": "user", "content": content} + ]) + assert result[0]["content"] == "describe this" + assert result[0]["attachments"] == ["file-123"] + + def test_list_content_with_image_url_string(self): + content = [ + {"type": "text", "text": "look"}, + {"type": "image_url", "image_url": "https://example.com/img.jpg"}, + ] + with patch.object(self.config, "_upload_image", return_value="file-456"): + result = self.config._transform_messages([ + {"role": "user", "content": content} + ]) + assert result[0]["content"] == "look" + assert "file-456" in result[0]["attachments"] + + +class TestTransformRequest: + def setup_method(self): + self.config = GigaChatConfig() + + def test_builds_basic_request(self): + body = self.config.transform_request( + model="gigachat/GigaChat", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + headers={}, + ) + assert body["model"] == "GigaChat" + assert len(body["messages"]) == 1 + assert body["messages"][0]["content"] == "hi" + + def test_model_prefix_stripped(self): + body = self.config.transform_request( + model="gigachat/GigaChat-Pro", + messages=[{"role": "user", "content": "hello"}], + optional_params={}, + litellm_params={}, + headers={}, + ) + assert body["model"] == "GigaChat-Pro" + + def test_includes_optional_params(self): + body = self.config.transform_request( + model="gigachat/GigaChat", + messages=[{"role": "user", "content": "hi"}], + optional_params={ + "temperature": 0.5, + "max_tokens": 100, + "stream": True, + }, + litellm_params={}, + headers={}, + ) + assert body["temperature"] == 0.5 + assert body["max_tokens"] == 100 + assert body["stream"] is True + + def test_includes_functions(self): + body = self.config.transform_request( + model="gigachat/GigaChat", + messages=[{"role": "user", "content": "hi"}], + optional_params={ + "functions": [{"name": "my_func"}], + "function_call": {"name": "my_func"}, + }, + litellm_params={}, + headers={}, + ) + assert body["functions"] == [{"name": "my_func"}] + assert body["function_call"] == {"name": "my_func"} + + def test_skips_unsupported_params(self): + body = self.config.transform_request( + model="gigachat/GigaChat", + messages=[{"role": "user", "content": "hi"}], + optional_params={"n": 2, "user": "abc"}, + litellm_params={}, + headers={}, + ) + assert "n" not in body + assert "user" not in body + + +class TestTransformResponse: + def setup_method(self): + self.config = GigaChatConfig() + + def test_basic_response(self): + raw = _make_httpx_response({ + "id": "chatcmpl-123", + "created": 1700000000, + "model": "GigaChat", + "choices": [ + { + "index": 0, + "message": {"role": "assistant", "content": "Hello!"}, + "finish_reason": "stop", + } + ], + "usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + assert result.choices[0].message.content == "Hello!" + assert result.choices[0].finish_reason == "stop" + assert result.usage.prompt_tokens == 5 + assert result.usage.total_tokens == 8 + + def test_function_call_into_tool_calls(self): + raw = _make_httpx_response({ + "id": "chatcmpl-456", + "created": 1700000000, + "model": "GigaChat", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": None, + "function_call": { + "name": "get_weather", + "arguments": {"city": "Moscow"}, + }, + }, + "finish_reason": "function_call", + } + ], + "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + assert result.choices[0].finish_reason == "tool_calls" + tool_calls = result.choices[0].message.tool_calls + assert tool_calls is not None + assert len(tool_calls) == 1 + assert tool_calls[0].function.name == "get_weather" + assert '{"city": "Moscow"}' in tool_calls[0].function.arguments + + def test_function_call_structured_output(self): + raw = _make_httpx_response({ + "id": "chatcmpl-789", + "created": 1700000000, + "model": "GigaChat", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": None, + "function_call": { + "name": "test_schema", + "arguments": {"name": "John"}, + }, + }, + "finish_reason": "function_call", + } + ], + "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={"_structured_output": True}, + litellm_params={}, + encoding=None, + ) + # Structured output: function_call -> content + assert result.choices[0].finish_reason == "stop" + assert result.choices[0].message.content is not None + assert '"name": "John"' in result.choices[0].message.content + + def test_function_call_string_arguments(self): + raw = _make_httpx_response({ + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "function_call": { + "name": "get_weather", + "arguments": '{"city": "Moscow"}', + }, + }, + "finish_reason": "function_call", + } + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + tc = result.choices[0].message.tool_calls[0] + assert '{"city": "Moscow"}' in tc.function.arguments + + def test_cleans_up_gigachat_specific_fields(self): + raw = _make_httpx_response({ + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "done", + "functions_state_id": "some-state", + }, + "finish_reason": "stop", + } + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + # functions_state_id should have been removed from the message data + assert result.choices[0].message.content == "done" + + def test_raises_on_invalid_json(self): + raw = httpx.Response( + status_code=500, + headers={"content-type": "text/plain"}, + content=b"not json", + request=httpx.Request("POST", "https://example.com"), + ) + model_response = ModelResponse() + with pytest.raises(GigaChatError) as exc_info: + self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + assert "Invalid JSON response" in str(exc_info.value.message) + + def test_empty_choices(self): + raw = _make_httpx_response({ + "choices": [], + "usage": {}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + assert result.choices == [] + + def test_function_call_with_non_dict_arguments(self): + raw = _make_httpx_response({ + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "function_call": { + "name": "say_hello", + "arguments": "hello", + }, + }, + "finish_reason": "function_call", + } + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }) + model_response = ModelResponse() + result = self.config.transform_response( + model="gigachat/GigaChat", + raw_response=raw, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[], + optional_params={}, + litellm_params={}, + encoding=None, + ) + tc = result.choices[0].message.tool_calls[0] + assert tc.function.arguments == "hello" + + +class TestGetModelResponseIterator: + def setup_method(self): + self.config = GigaChatConfig() + + def test_returns_gigachat_iterator_sync(self): + from litellm.llms.gigachat.chat.streaming import ( + GigaChatModelResponseIterator, + ) + + result = self.config.get_model_response_iterator( + streaming_response=iter(["data"]), + sync_stream=True, + json_mode=False, + ) + assert isinstance(result, GigaChatModelResponseIterator) + + +class TestGetErrorClass: + def setup_method(self): + self.config = GigaChatConfig() + + def test_returns_gigachat_error(self): + error = self.config.get_error_class( + error_message="something went wrong", + status_code=400, + headers={"x-request-id": "abc"}, + ) + assert isinstance(error, GigaChatError) + assert error.status_code == 400 + assert error.message == "something went wrong" + assert error.headers == {"x-request-id": "abc"} + + +class TestUploadImage: + def setup_method(self): + self.config = GigaChatConfig() + + @patch(f"{TRANSFORM_MODULE}.upload_file_sync", side_effect=Exception("fail")) + def test_upload_image_failure_returns_none(self, mock_upload): + result = self.config._upload_image("https://example.com/img.jpg") + assert result is None \ No newline at end of file