|
| 1 | +"""User-provided ``litellm:`` settings are applied verbatim onto the litellm module. |
| 2 | +
|
| 3 | +``litellm:`` in config.yaml is a free-form passthrough of LiteLLM module-level |
| 4 | +globals (``drop_params``, ``modify_params``, ``ssl_verify``, ...). |
| 5 | +``cli._apply_litellm_settings`` assigns each key onto the ``litellm`` module so |
| 6 | +it takes effect process-wide for every call routed through LiteLLM. It warns, |
| 7 | +without blocking, for a key the installed LiteLLM doesn't expose (typo / version |
| 8 | +mismatch) and refuses to overwrite a LiteLLM *function*. Settings are sticky: |
| 9 | +applied, never reset — see ``test_apply_is_sticky_not_reset``. |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import logging |
| 14 | + |
| 15 | +import litellm |
| 16 | +import pytest |
| 17 | + |
| 18 | +from openkb.cli import _KNOWN_PROVIDER_KEYS, _apply_litellm_settings, _setup_llm_key |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def _restore_litellm_globals(): |
| 23 | + """Snapshot and restore the litellm globals these tests mutate. |
| 24 | +
|
| 25 | + setattr on the litellm module is process-wide, so without this an applied |
| 26 | + value would leak into every later test sharing the interpreter. ``api_key`` |
| 27 | + is included because the end-to-end test exercises full ``_setup_llm_key``. |
| 28 | + """ |
| 29 | + keys = ("drop_params", "modify_params", "ssl_verify", "api_key") |
| 30 | + saved = {k: getattr(litellm, k) for k in keys} |
| 31 | + try: |
| 32 | + yield |
| 33 | + finally: |
| 34 | + for k, v in saved.items(): |
| 35 | + setattr(litellm, k, v) |
| 36 | + |
| 37 | + |
| 38 | +def test_apply_sets_known_global_verbatim(): |
| 39 | + litellm.drop_params = False |
| 40 | + _apply_litellm_settings({"drop_params": True}) |
| 41 | + assert litellm.drop_params is True |
| 42 | + |
| 43 | + |
| 44 | +def test_apply_forwards_values_as_is_no_coercion(): |
| 45 | + _apply_litellm_settings({"ssl_verify": False, "modify_params": True}) |
| 46 | + assert litellm.ssl_verify is False |
| 47 | + assert litellm.modify_params is True |
| 48 | + |
| 49 | + |
| 50 | +def test_apply_skips_unknown_key_with_warning(caplog): |
| 51 | + bogus = "definitely_not_a_litellm_setting_xyz" |
| 52 | + assert not hasattr(litellm, bogus) |
| 53 | + with caplog.at_level(logging.WARNING, logger="openkb.cli"): |
| 54 | + _apply_litellm_settings({bogus: 123}) |
| 55 | + # Not silently created as a dead attribute… |
| 56 | + assert not hasattr(litellm, bogus) |
| 57 | + # …and the user is told (on the logger, like the sibling resolvers). |
| 58 | + assert bogus in caplog.text |
| 59 | + assert "ignoring it" in caplog.text |
| 60 | + |
| 61 | + |
| 62 | +def test_apply_refuses_to_overwrite_callable(caplog): |
| 63 | + """A key naming a LiteLLM *function* (hasattr is True) must NOT be clobbered |
| 64 | + — overwriting litellm.completion with a scalar would brick every later call. |
| 65 | + """ |
| 66 | + assert callable(litellm.completion) |
| 67 | + with caplog.at_level(logging.WARNING, logger="openkb.cli"): |
| 68 | + _apply_litellm_settings({"completion": 5}) |
| 69 | + assert callable(litellm.completion) # untouched |
| 70 | + assert "completion" in caplog.text |
| 71 | + assert "function" in caplog.text |
| 72 | + |
| 73 | + |
| 74 | +def test_apply_applies_known_even_when_another_key_is_unknown(): |
| 75 | + litellm.drop_params = False |
| 76 | + _apply_litellm_settings({"nope_not_real_xyz": 1, "drop_params": True}) |
| 77 | + assert litellm.drop_params is True |
| 78 | + |
| 79 | + |
| 80 | +def test_apply_empty_is_noop(): |
| 81 | + litellm.drop_params = False |
| 82 | + _apply_litellm_settings({}) |
| 83 | + assert litellm.drop_params is False |
| 84 | + |
| 85 | + |
| 86 | +def test_apply_is_sticky_not_reset(): |
| 87 | + """Documented contract: settings are applied, never reset. Applying {} after |
| 88 | + a real setting leaves the earlier value in place (it does NOT revert to the |
| 89 | + LiteLLM default) — unlike timeout/extra_headers. Pins the intentional |
| 90 | + process-wide stickiness so a future 'reset on empty' change is caught. |
| 91 | + """ |
| 92 | + litellm.drop_params = False |
| 93 | + _apply_litellm_settings({"drop_params": True}) |
| 94 | + _apply_litellm_settings({}) # a later config without a litellm: block |
| 95 | + assert litellm.drop_params is True # stays set, not reset to default |
| 96 | + |
| 97 | + |
| 98 | +def test_setup_llm_key_applies_litellm_block_from_config(tmp_path, monkeypatch): |
| 99 | + """End-to-end: a ``litellm:`` block in config.yaml lands on the module the |
| 100 | + next time any command runs _setup_llm_key. |
| 101 | +
|
| 102 | + Env is cleared so full ``_setup_llm_key`` doesn't set litellm.api_key / |
| 103 | + provider env vars from a key in the dev environment — which would leak |
| 104 | + process-wide state into other tests. |
| 105 | + """ |
| 106 | + monkeypatch.delenv("LLM_API_KEY", raising=False) |
| 107 | + for key in _KNOWN_PROVIDER_KEYS: |
| 108 | + monkeypatch.delenv(key, raising=False) |
| 109 | + |
| 110 | + openkb_dir = tmp_path / ".openkb" |
| 111 | + openkb_dir.mkdir(parents=True) |
| 112 | + (openkb_dir / "config.yaml").write_text( |
| 113 | + "model: gpt-4o-mini\nlitellm:\n drop_params: true\n", encoding="utf-8" |
| 114 | + ) |
| 115 | + litellm.drop_params = False |
| 116 | + _setup_llm_key(tmp_path) |
| 117 | + assert litellm.drop_params is True |
| 118 | + |
| 119 | + |
| 120 | +def _write_kb_config(tmp_path, body: str): |
| 121 | + openkb_dir = tmp_path / ".openkb" |
| 122 | + openkb_dir.mkdir(parents=True) |
| 123 | + (openkb_dir / "config.yaml").write_text(body, encoding="utf-8") |
| 124 | + |
| 125 | + |
| 126 | +def _isolate_env(monkeypatch): |
| 127 | + monkeypatch.delenv("LLM_API_KEY", raising=False) |
| 128 | + for key in _KNOWN_PROVIDER_KEYS: |
| 129 | + monkeypatch.delenv(key, raising=False) |
| 130 | + |
| 131 | + |
| 132 | +def test_litellm_block_routes_timeout_and_extra_headers_per_call(tmp_path, monkeypatch): |
| 133 | + """`timeout` / `extra_headers` inside the litellm: block route to the |
| 134 | + per-call stashes (not litellm module globals); the rest stay globals. |
| 135 | + """ |
| 136 | + from openkb.config import get_extra_headers, get_timeout |
| 137 | + |
| 138 | + _isolate_env(monkeypatch) |
| 139 | + _write_kb_config( |
| 140 | + tmp_path, |
| 141 | + "model: gpt-4o-mini\n" |
| 142 | + "litellm:\n" |
| 143 | + " timeout: 1200\n" |
| 144 | + " drop_params: true\n" |
| 145 | + " extra_headers:\n" |
| 146 | + " Editor-Version: vscode/1.95.0\n", |
| 147 | + ) |
| 148 | + litellm.drop_params = False |
| 149 | + _setup_llm_key(tmp_path) |
| 150 | + assert get_timeout() == 1200.0 |
| 151 | + assert get_extra_headers() == {"Editor-Version": "vscode/1.95.0"} |
| 152 | + assert litellm.drop_params is True |
| 153 | + # timeout/extra_headers must NOT have been setattr'd onto litellm as globals |
| 154 | + assert not isinstance(getattr(litellm, "timeout", None), float) or \ |
| 155 | + getattr(litellm, "timeout", None) != 1200.0 |
| 156 | + |
| 157 | + |
| 158 | +def test_litellm_block_timeout_wins_over_legacy_toplevel(tmp_path, monkeypatch): |
| 159 | + """The litellm: block value wins over the legacy top-level key.""" |
| 160 | + from openkb.config import get_timeout |
| 161 | + |
| 162 | + _isolate_env(monkeypatch) |
| 163 | + _write_kb_config( |
| 164 | + tmp_path, |
| 165 | + "model: gpt-4o-mini\n" |
| 166 | + "timeout: 30\n" # legacy top-level |
| 167 | + "litellm:\n" |
| 168 | + " timeout: 1200\n", # canonical — wins |
| 169 | + ) |
| 170 | + _setup_llm_key(tmp_path) |
| 171 | + assert get_timeout() == 1200.0 |
| 172 | + |
| 173 | + |
| 174 | +def test_legacy_toplevel_timeout_still_works(tmp_path, monkeypatch): |
| 175 | + """Back-compat: a top-level `timeout:` (no litellm: block) is still honored.""" |
| 176 | + from openkb.config import get_timeout |
| 177 | + |
| 178 | + _isolate_env(monkeypatch) |
| 179 | + _write_kb_config(tmp_path, "model: gpt-4o-mini\ntimeout: 900\n") |
| 180 | + _setup_llm_key(tmp_path) |
| 181 | + assert get_timeout() == 900.0 |
0 commit comments