fix(cli): resolve the stored proxy URL in the eager --version path

Greptile review follow-ups: lite --version fired its eager callback before
--base-url parsing, so it always reported localhost even when a URL was stored
from a previous login or set via LITELLM_PROXY_URL; it now resolves through the
same chain (flag, env var, stored URL, localhost). Also import ParameterSource
from click's public namespace instead of click.core.
This commit is contained in:
mateo-berri 2026-06-10 19:05:27 -07:00
parent 5c40ec618e
commit 04ed9280f7
2 changed files with 51 additions and 13 deletions

View file

@ -1,9 +1,10 @@
# stdlib imports
import os
from typing import Optional
# third party imports
import click
from click.core import ParameterSource
from click import ParameterSource
from litellm._version import version as litellm_version
from litellm.litellm_core_utils.cli_token_utils import get_stored_base_url
@ -22,6 +23,8 @@ from .commands.teams import teams
from .commands.users import users
from .interface import interactive_shell
DEFAULT_BASE_URL = "http://localhost:4000"
def print_version(base_url: str, api_key: Optional[str]):
"""Print CLI and server version info."""
@ -39,6 +42,25 @@ def print_version(base_url: str, api_key: Optional[str]):
click.echo(f"Could not retrieve server version: {e}")
def _eager_version_base_url(ctx: click.Context) -> str:
"""Resolve the base URL for `--version`, which can fire before --base-url is parsed."""
return (
ctx.params.get("base_url")
or os.environ.get("LITELLM_PROXY_URL")
or get_stored_base_url()
or DEFAULT_BASE_URL
)
def _print_version_callback(
ctx: click.Context, param: click.Parameter, value: bool
) -> None:
if not value or ctx.resilient_parsing:
return
print_version(_eager_version_base_url(ctx), ctx.params.get("api_key"))
ctx.exit()
@click.group(invoke_without_command=True)
@click.option(
"--version",
@ -47,23 +69,13 @@ def print_version(base_url: str, api_key: Optional[str]):
is_eager=True,
expose_value=False,
help="Show the LiteLLM Proxy CLI and server version and exit.",
callback=lambda ctx, param, value: (
(
print_version(
ctx.params.get("base_url") or "http://localhost:4000",
ctx.params.get("api_key"),
)
or ctx.exit()
)
if value and not ctx.resilient_parsing
else None
),
callback=_print_version_callback,
)
@click.option(
"--base-url",
envvar="LITELLM_PROXY_URL",
show_envvar=True,
default="http://localhost:4000",
default=DEFAULT_BASE_URL,
help="Base URL of the LiteLLM proxy server",
)
@click.option(

View file

@ -113,3 +113,29 @@ class TestBaseUrlResolution:
def test_falls_back_to_localhost_without_stored_url(self, cli_runner, monkeypatch):
output = self._server_url_line(cli_runner, ["version"], None, monkeypatch)
assert "LiteLLM Proxy Server URL: http://localhost:4000" in output
def test_version_flag_uses_stored_base_url(self, cli_runner, monkeypatch):
output = self._server_url_line(
cli_runner, ["--version"], "https://llm.acme.com", monkeypatch
)
assert "LiteLLM Proxy Server URL: https://llm.acme.com" in output
def test_version_flag_env_var_wins_over_stored(self, cli_runner, monkeypatch):
monkeypatch.setenv("LITELLM_PROXY_URL", "http://env:1234")
with (
patch(
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
return_value="1.2.3",
),
patch(
"litellm.proxy.client.cli.main.get_stored_base_url",
return_value="https://llm.acme.com",
),
):
result = cli_runner.invoke(cli, ["--version"])
assert result.exit_code == 0, result.output
assert "LiteLLM Proxy Server URL: http://env:1234" in result.output
def test_version_flag_falls_back_to_localhost(self, cli_runner, monkeypatch):
output = self._server_url_line(cli_runner, ["--version"], None, monkeypatch)
assert "LiteLLM Proxy Server URL: http://localhost:4000" in output