From cdbdf8652c4f06f4dedba27728c91d439002cad6 Mon Sep 17 00:00:00 2001 From: Kerry Lu Date: Mon, 16 Nov 2026 10:51:21 -0800 Subject: [PATCH] fix(gemini): send reasoning_effort xhigh as thinkingLevel xhigh for gemini-4-hydrogen (#52060) * fix(gemini): send reasoning_effort xhigh as thinkingLevel xhigh for gemini-4-hydrogen * Apply suggestion from @greptile-apps[bot] Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * fix(gemini): allow the one in-place thinkingLevel write now that the field is ReadOnly --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- .../vertex_and_google_ai_studio_gemini.py | 12 +- ...odel_prices_and_context_window_backup.json | 2 + litellm/types/llms/vertex_ai.py | 2 +- model_prices_and_context_window.json | 2 + tests/integration/proxy_config.yaml | 5 + .../test_chat_completions_reasoning_gemini.py | 120 ++++++++++++++++++ ...test_vertex_and_google_ai_studio_gemini.py | 36 +++++- 7 files changed, 176 insertions(+), 3 deletions(-) diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py index 572dcd5eecf..8fa75c3b638 100644 --- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py +++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py @@ -99,6 +99,7 @@ from litellm.utils import ( ModelResponse, is_base64_encoded, is_explicitly_disabled_factory, + supports_factory, supports_reasoning, ) @@ -1637,6 +1638,13 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): model=model, custom_llm_provider=None, key="supports_minimal_reasoning_effort" ) + @staticmethod + def _supports_xhigh_thinking_level(model: str) -> bool: + return any( + supports_factory(model=model, custom_llm_provider=provider, key="supports_xhigh_reasoning_effort") + for provider in ("gemini", "vertex_ai") + ) + @staticmethod def _map_reasoning_effort_to_thinking_level( reasoning_effort: str, @@ -1665,6 +1673,8 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): return {"thinkingLevel": "medium", "includeThoughts": sends_thoughts} elif reasoning_effort == "high": return {"thinkingLevel": "high", "includeThoughts": sends_thoughts} + elif reasoning_effort == "xhigh" and model and VertexGeminiConfig._supports_xhigh_thinking_level(model): + return {"thinkingLevel": "xhigh", "includeThoughts": sends_thoughts} elif reasoning_effort in ("disable", "none"): return { "thinkingLevel": "minimal" if supports_minimal else "low", @@ -1751,7 +1761,7 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): ) # Follow provider defaults unless explicitly opted into legacy behavior. if litellm.enable_gemini_default_thinking_level_low is True: - params["thinkingLevel"] = ( + params["thinkingLevel"] = ( # pyright: ignore[reportTypedDictNotRequiredAccess] # built in place "minimal" if VertexGeminiConfig._supports_minimal_thinking_level(model) else "low" ) else: diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index da74c7f1c41..f5abbc617ac 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -101819,6 +101819,7 @@ "supports_video_input": true, "supports_vision": true, "supports_web_search": true, + "supports_xhigh_reasoning_effort": true, "tpm": 800000.0, "web_search_billing_unit": "per_query" }, @@ -101878,6 +101879,7 @@ "supports_video_input": true, "supports_vision": true, "supports_web_search": true, + "supports_xhigh_reasoning_effort": true, "web_search_billing_unit": "per_query" }, "together_ai/thinkingmachines/Inkling-2-Small": { diff --git a/litellm/types/llms/vertex_ai.py b/litellm/types/llms/vertex_ai.py index 2fbf9996f4d..4840144cca6 100644 --- a/litellm/types/llms/vertex_ai.py +++ b/litellm/types/llms/vertex_ai.py @@ -219,7 +219,7 @@ class SafetSettingsConfig(TypedDict, total=False): class GeminiThinkingConfig(TypedDict, total=False): includeThoughts: bool thinkingBudget: int - thinkingLevel: Literal["minimal", "low", "medium", "high"] + thinkingLevel: ReadOnly[Literal["minimal", "low", "medium", "high", "xhigh"]] GeminiResponseModalities = Literal["TEXT", "IMAGE", "AUDIO", "VIDEO"] diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json index da74c7f1c41..f5abbc617ac 100644 --- a/model_prices_and_context_window.json +++ b/model_prices_and_context_window.json @@ -101819,6 +101819,7 @@ "supports_video_input": true, "supports_vision": true, "supports_web_search": true, + "supports_xhigh_reasoning_effort": true, "tpm": 800000.0, "web_search_billing_unit": "per_query" }, @@ -101878,6 +101879,7 @@ "supports_video_input": true, "supports_vision": true, "supports_web_search": true, + "supports_xhigh_reasoning_effort": true, "web_search_billing_unit": "per_query" }, "together_ai/thinkingmachines/Inkling-2-Small": { diff --git a/tests/integration/proxy_config.yaml b/tests/integration/proxy_config.yaml index c552e11c22a..c639f653ef9 100644 --- a/tests/integration/proxy_config.yaml +++ b/tests/integration/proxy_config.yaml @@ -191,6 +191,11 @@ model_list: model: gemini/gemini-4-argon api_base: http://127.0.0.1:8191 api_key: synthetic-gemini-key + - model_name: gemini/gemini-4-hydrogen + litellm_params: + model: gemini/gemini-4-hydrogen + api_base: http://127.0.0.1:8191 + api_key: synthetic-gemini-key - model_name: azure_ai/claude-haiku-4-5 litellm_params: model: azure_ai/claude-haiku-4-5 diff --git a/tests/integration/translation/chat_completions/reasoning/test_chat_completions_reasoning_gemini.py b/tests/integration/translation/chat_completions/reasoning/test_chat_completions_reasoning_gemini.py index e2369fea443..75d33f4f4aa 100644 --- a/tests/integration/translation/chat_completions/reasoning/test_chat_completions_reasoning_gemini.py +++ b/tests/integration/translation/chat_completions/reasoning/test_chat_completions_reasoning_gemini.py @@ -699,6 +699,120 @@ VERTEX_GEMINI_3_8_FLASH_THOUGHTS_OFF_TEST_CASE: Final = replace( }, ) +GEMINI_4_HYDROGEN_XHIGH_THOUGHT: Final = ( + "**Initial Project Review and File Selection**\n\nOkay, let's break this down. First, I need to mak" + "e sure I'm following the instructions and user's request precisely. The instructions are clear: " + "I'm maintaining a synthetic Python project, so I need to carefully review the files before makin" + "g any suggestions. I'm using the `chat_completions` client for this review, and importantly, I'm" + " never changing files - read-only only. The emphasis on considering zero, negative, and non-inte" + "ger quantities is a key parameter for this process.\n\nThe user's direct request is to \"Read the v" + 'alidation file first so we can understand how zero and negative quantities are handled." Right, ' + "so the user wants me to start with the validation file. That simplifies the initial decision.\n\nT" + "herefore, the next step is straightforward: I need to explain which file I need next, which is `" + "shop/validation.py`, and then call `read_file` on that same file. Before I call it, I need to ch" + "eck the parameters of `read_file`. It looks like it takes `path`, `start_line`, and `end_line` a" + "s inputs. I'll plan to read the first 100 or 50 lines of `shop/validation.py`. I'll confirm that" + " the `read_file` tool accepts those parameters - `end_line` and `start_line` are positive intege" + "rs, and `path` is a string.\n\nOkay, let's articulate this clearly. My explanation will be: \"I wil" + "l read `shop/validation.py` next to inspect the `validate_quantity` function and determine how z" + "ero, negative, and non-integer quantities are handled. Then I'll call the `read_file` tool.\" Tha" + "t seems logical.\n\n\n" +) + +GEMINI_4_HYDROGEN_XHIGH_THOUGHT_SIGNATURE: Final = ( + "EsQLCsELAWkUfRM5gOWUDlbdY45vVQCWaNNlBBBBhIayn7iTEElwrtI7iqRVbZ6V2IWFlZ8jRU22hEUf0FukOa+rPl1VdAT1" + "M+TNO/LQzDFpufvAxjKrqWpcb8CIkwC50Di67ofVugVtWnMBiu4eQf+Bc0z8Q+j58eABxe48s78B1y/9wmAu4YPGMJhBolap" + "4SIRUkIf/LcYTao4WYJqUYcJ1x6Y5qMzpwaFVi0SfH9vjqNMCdtqRGvadBeWojIJCPDwLg61dYUVJNllAZXZoA4FplPFcM3N" + "ipqKj5b9W0uApFHIM1fAeSaGwyEMfL1Yu4HfSAJP5VDOM7RCq68sWRlN0upuN5s+OhRT4bBV8sKYYwgP8i9UOmXaF9+Gohfk" + "KDdvxtW5dviCv154twZxwi9eZZRpac/6B6T909GykVEvO80pmIjf7qroSl91Hqf/R4TIZaeDa3I6Iy7gECCE0HB4W7OjW7Qy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+ "hhH5MxHa8JRvJ5OBICxDfEO2cb7/Kb1ltFzw+MTChmGSfCQ7yH+NV+LReLRn4XBa/uveSgWZfdwL6tq5m9/g+eA7dT2apXMa" + "mfhHnznqfoadCyMMWkxZNgo6eXsEXJkyYqbjgp/xVAByjaPdqBX2mH1EE3hqn4b1i1Xa2Ckfpx+dTncVD4cyEM81cof348ZA" + "nwsWFb6GYD/WJGV6te4j2x716ZbYs+lkjxHjCQ9JyNESiTHPODYnC0/MxuTwBAN6o2FMK6OcZL27ZOkoHchL0918EzL5w1Gv" + "N3ApeDm9xLgseqrS+tMfyQtd1GCBAl7CeNZ9fgn2GpgOpKzDtcoGlFO4CtqH2RIkfsdzdJeCgDTsqpDjOPGKZyxpurIsEuJ7" + "MpJlVVrBtux+lE5qoc6+BLyhcbOxIzVtDx3cxFQiBr9u/cBCUC7d" +) + +GEMINI_4_HYDROGEN_XHIGH_TEST_CASE: Final = replace( + GEMINI_3_8_FLASH_TEST_CASE, + scenario="xhigh_reasoning_effort", + litellm_request={**THINKING_REQUEST, "model": "gemini/gemini-4-hydrogen", "reasoning_effort": "xhigh"}, + expected_provider_endpoint="/models/gemini-4-hydrogen:generateContent", + expected_provider_request=with_thinking_config( + GEMINI_3_8_FLASH_TEST_CASE.expected_provider_request, {"thinkingLevel": "xhigh", "includeThoughts": True} + ), + mock_provider_response={ + "candidates": [ + { + "content": { + "parts": [ + {"text": GEMINI_4_HYDROGEN_XHIGH_THOUGHT, "thought": True}, + { + "text": "I need to read `shop/validation.py` next to inspect `validate_quantity` and see how it handles zero, negative, and non-integer quantities." + }, + { + "functionCall": { + "name": "read_file", + "args": {"start_line": 1, "path": "shop/validation.py", "end_line": 100}, + "id": "call_5567188554998993723", + }, + "thoughtSignature": GEMINI_4_HYDROGEN_XHIGH_THOUGHT_SIGNATURE, + }, + ], + "role": "model", + }, + "finishReason": "STOP", + "index": 0, + "finishMessage": "Model generated function call(s).", + }, + ], + "usageMetadata": { + "promptTokenCount": 414, + "candidatesTokenCount": 74, + "totalTokenCount": 833, + "promptTokensDetails": [{"modality": "TEXT", "tokenCount": 414}], + "candidatesTokensDetails": [{"modality": "TEXT", "tokenCount": 74}], + "thoughtsTokenCount": 345, + "serviceTier": "standard", + }, + "modelVersion": "gemini-4-hydrogen", + "responseId": "e0D7asXVF42GqtsP2LnWqAM", + }, + expected_litellm_response={ + **gemini_chat_response( + content="I need to read `shop/validation.py` next to inspect `validate_quantity` and see how it handles zero, negative, and non-integer quantities.", + tool_call=signed_tool_call( + "call_5567188554998993723", + '{"start_line": 1, "path": "shop/validation.py", "end_line": 100}', + GEMINI_4_HYDROGEN_XHIGH_THOUGHT_SIGNATURE, + ), + message_fields={ + "reasoning_content": GEMINI_4_HYDROGEN_XHIGH_THOUGHT, + "thinking_blocks": [{"type": "thinking", "thinking": GEMINI_4_HYDROGEN_XHIGH_THOUGHT}], + "provider_specific_fields": {"thought_signatures": [GEMINI_4_HYDROGEN_XHIGH_THOUGHT_SIGNATURE]}, + }, + usage={ + "completion_tokens": 419, + "prompt_tokens": 414, + "total_tokens": 833, + "completion_tokens_details": {"reasoning_tokens": 345, "text_tokens": 74}, + "prompt_tokens_details": {"text_tokens": 414}, + }, + ), + "model": "gemini/gemini-4-hydrogen", + }, +) + GEMINI_3_8_FLASH_THINKING_OMITTED_TEST_CASE: Final = replace( GEMINI_3_8_FLASH_TEST_CASE, scenario="thinking_omitted", @@ -1035,3 +1149,9 @@ def test_every_worker_honors_the_flag_on_every_endpoint_under_a_burst_and_after_ _assert_each_request_follows_its_own_ask(owned.gateway, provider) owned.kill_worker(min(owned.worker_pids())) _assert_each_request_follows_its_own_ask(owned.gateway, provider) + + +def test_gemini_4_hydrogen_sends_xhigh_reasoning_effort_as_the_xhigh_thinking_level( + gateway: Gateway, provider: SharedProvider +) -> None: + assert_translation(GEMINI_4_HYDROGEN_XHIGH_TEST_CASE, gateway, provider) diff --git a/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py b/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py index 31dbb9a4e9e..24c7f3a4113 100644 --- a/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py +++ b/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py @@ -1886,7 +1886,11 @@ def test_vertex_ai_map_tools_declares_a_member_a_namespace_lists_twice_once_from [("get_weather", None), ("get_weather", None), ("weather__get_weather", None)], ), ], - ids=["top_level_function_has_the_qualified_name", "namespaces_build_one_qualified_name", "client_repeats_a_function"], + ids=[ + "top_level_function_has_the_qualified_name", + "namespaces_build_one_qualified_name", + "client_repeats_a_function", + ], ) def test_vertex_ai_map_tools_never_repeats_a_generated_name( tools: list[dict], declared: list[tuple[str, str | None]] @@ -7857,6 +7861,36 @@ def test_invalid_reasoning_effort_surfaces_as_400_through_completion(custom_llm_ assert not isinstance(exc_info.value, litellm.APIConnectionError) +@pytest.mark.parametrize("model", ["gemini-4-hydrogen", "gemini/gemini-4-hydrogen", "vertex_ai/gemini-4-hydrogen"]) +def test_xhigh_reasoning_effort_maps_to_the_xhigh_thinking_level_for_a_model_that_supports_it( + model: str, local_model_cost_map: None +) -> None: + result: Final = VertexGeminiConfig().map_openai_params( + non_default_params={"reasoning_effort": "xhigh"}, + optional_params={}, + model=model, + drop_params=False, + ) + + assert result["thinkingConfig"] == {"thinkingLevel": "xhigh", "includeThoughts": True} + + +@pytest.mark.parametrize("model", ["gemini-4-argon", "gemini-3.8-flash", "gemini-3-pro-preview"]) +def test_xhigh_reasoning_effort_is_a_400_for_a_gemini_model_that_does_not_support_it( + model: str, local_model_cost_map: None +) -> None: + with pytest.raises(litellm.BadRequestError) as exc_info: + VertexGeminiConfig().map_openai_params( + non_default_params={"reasoning_effort": "xhigh"}, + optional_params={}, + model=model, + drop_params=False, + ) + + assert exc_info.value.status_code == 400 + assert "xhigh" in str(exc_info.value) + + @pytest.mark.parametrize("model", ["gemini-2.5-flash", "gemini-3-pro-preview"]) def test_supported_reasoning_efforts_still_map(model): """Guards the fix against over-rejecting: every advertised value must still produce a config."""