From 9a30b5a5fa6f166edc43f81b69d152c6901d415e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 28 Aug 2026 14:25:18 +0000 Subject: [PATCH] fix(cli): preserve missing API key diagnostic Co-authored-by: Blaine Kasten --- src/together/lib/cli/__init__.py | 2 +- tests/cli/test_command_telemetry.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/together/lib/cli/__init__.py b/src/together/lib/cli/__init__.py index 7e5d2427..4f9160d0 100644 --- a/src/together/lib/cli/__init__.py +++ b/src/together/lib/cli/__init__.py @@ -169,7 +169,7 @@ def block_requests_for_api_key(_: httpx.Request) -> None: "[red]x[/red] api key missing.\n\nThe api key must be set either by passing --api-key to the command or by setting the TOGETHER_API_KEY environment variable", ) console.print("You can find your api key at https://api.together.ai/settings/api-keys") - sys.exit(1) + raise CliDiagnosticExit("Together API key missing") client._client.event_hooks["request"].append(block_requests_for_api_key) else: diff --git a/tests/cli/test_command_telemetry.py b/tests/cli/test_command_telemetry.py index 83e99011..f9049591 100644 --- a/tests/cli/test_command_telemetry.py +++ b/tests/cli/test_command_telemetry.py @@ -91,6 +91,30 @@ def _exit_1() -> None: assert "error" in failed +@pytest.mark.usefixtures("isolated_cli_config") +@pytest.mark.asyncio +async def test_missing_api_key_preserves_diagnostic( + track_cli_capture: list[tuple[CliTrackingEvents, dict[str, Any]]], + monkeypatch: pytest.MonkeyPatch, +) -> None: + from together.lib.cli import launcher + + monkeypatch.delenv("TOGETHER_API_KEY", raising=False) + monkeypatch.setenv("TOGETHER_DISABLE_VERSION_CHECK", "1") + + with pytest.raises(SystemExit) as exc_info: + await launcher("endpoints", "list", project_id="project") + + assert exc_info.value.code == 1 + assert _event_kinds(track_cli_capture) == [ + CliTrackingEvents.CommandStarted.value, + CliTrackingEvents.CommandFailed.value, + ] + failed = track_cli_capture[1][1] + assert failed["command"] == "endpoints list" + assert failed["error"] == "Together API key missing" + + @pytest.mark.usefixtures("isolated_cli_config") @pytest.mark.asyncio async def test_interactive_missing_required_argument_preserves_diagnostic(