|
| 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 |
0 commit comments