From eeba33fd951d9d0560c7d8a716a0cd3cc7dc79f4 Mon Sep 17 00:00:00 2001 From: Kolade Fajimi <107228310+koladefaj@users.noreply.github.com> Date: Tue, 8 Sep 2026 08:49:21 +0100 Subject: [PATCH] test(vertex): cover the brace fallback and non-text credentials file --- litellm/llms/vertex_ai/credentials_source.py | 2 +- .../llms/vertex_ai/test_vertex_llm_base.py | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/litellm/llms/vertex_ai/credentials_source.py b/litellm/llms/vertex_ai/credentials_source.py index 6c86d02ad21..dbeeb593afe 100644 --- a/litellm/llms/vertex_ai/credentials_source.py +++ b/litellm/llms/vertex_ai/credentials_source.py @@ -142,5 +142,5 @@ def raise_vertex_credentials_failure(failure: VertexCredentialsFailure) -> NoRet f"The inline `vertex_credentials` value is not valid JSON: {detail}. " "Check for unescaped newlines in private_key." ) - case _: + case _: # pragma: no cover - exhaustiveness guard, unreachable while the union holds assert_never(failure) diff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py b/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py index e636b8c2637..00368182cf3 100644 --- a/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py +++ b/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py @@ -2322,10 +2322,15 @@ class TestVertexCredentialsSource: assert "embedded null" in str(exc_info.value) - def test_a_file_whose_name_starts_with_a_brace_is_still_read(self, tmp_path): - """Shape dispatch must not strand a real file that happens to be named like JSON.""" + def test_a_file_whose_name_starts_with_a_brace_is_still_read(self, tmp_path, monkeypatch): + """Shape dispatch must not strand a real file that happens to be named like JSON. + + The value has to be relative for this to bite: an absolute path never starts with + a brace, so only "{vertex}.json" reaches the inline branch and needs the fallback. + """ braced = tmp_path / "{vertex}.json" braced.write_text(json.dumps({"type": "service_account", "project_id": "from-braced-file"})) + monkeypatch.chdir(tmp_path) vertex_base = VertexBase() mock_creds = MagicMock() mock_creds.project_id = "from-braced-file" @@ -2334,8 +2339,17 @@ class TestVertexCredentialsSource: patch.object(vertex_base, "_credentials_from_service_account", return_value=mock_creds) as from_sa, patch.object(vertex_base, "refresh_auth"), ): - creds, project_id = vertex_base.load_auth(credentials=str(braced), project_id=None) + creds, project_id = vertex_base.load_auth(credentials="{vertex}.json", project_id=None) assert creds is mock_creds assert project_id == "from-braced-file" assert from_sa.call_args.args[0] == {"type": "service_account", "project_id": "from-braced-file"} + + def test_a_credentials_file_that_is_not_text_is_reported_as_such(self, tmp_path): + not_text = tmp_path / "vertexai.json" + not_text.write_bytes(bytes([0xFF, 0xFE, 0x00, 0x01])) + + with pytest.raises(ValueError, match="is not valid JSON") as exc_info: + VertexBase().load_auth(credentials=str(not_text), project_id="p") + + assert "UTF-8" in str(exc_info.value)