Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(ui): hide guardrail group headers when only one group has entries

Yuneng Jiang2e492f5cb7ee
diff --git a/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx b/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx
index a57676d07b8..dcc72ccac9c 100644
--- a/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx
+++ b/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx
@@ -1,8 +1,8 @@
import * as networking from "@/components/networking";
-import { screen, waitFor } from "@testing-library/react";
+import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { renderWithProviders } from "../../../tests/test-utils";
+import { renderWithProviders, testQueryClient } from "../../../tests/test-utils";
import TeamInfoView from "./TeamInfo";
vi.mock("@/components/networking", () => ({
@@ -1024,4 +1024,99 @@ describe("TeamInfoView", () => {
expect(payload).not.toHaveProperty("model_aliases");
});
});
+
+ describe("guardrails dropdown grouping", () => {
+ const guardrail = (name: string, defaultOn: boolean) => ({
+ guardrail_name: name,
+ litellm_params: { default_on: defaultOn },
+ });
+
+ const openGuardrailsDropdown = async (user: ReturnType<typeof userEvent.setup>) => {
+ renderWithProviders(<TeamInfoView {...defaultProps} />);
+
+ await waitFor(() => {
+ const teamNameElements = screen.queryAllByText("Test Team");
+ expect(teamNameElements.length).toBeGreaterThan(0);
+ });
+
+ await user.click(screen.getByRole("tab", { name: "Settings" }));
+
+ await waitFor(() => {
+ expect(screen.getByRole("button", { name: /edit settings/i })).toBeInTheDocument();
+ });
+
+ await user.click(screen.getByRole("button", { name: /edit settings/i }));
+
+ await waitFor(() => {
+ expect(screen.getByLabelText(/^Guardrails/)).toBeInTheDocument();
+ });
+
+ const dropdownsBefore = new Set(document.querySelectorAll(".ant-select-dropdown"));
+
+ await user.click(screen.getByLabelText(/^Guardrails/));
+
+ return waitFor(
+ () => {
+ const opened = Array.from(document.querySelectorAll(".ant-select-dropdown")).find(
+ (el) => !dropdownsBefore.has(el),
+ );
+ expect(opened).toBeDefined();
+ return opened as HTMLElement;
+ },
+ { timeout: 5000 },
+ );
+ };
+
+ beforeEach(() => {
+ testQueryClient.clear();
+ vi.mocked(networking.teamInfoCall).mockResolvedValue(createMockTeamData());
+ });
+
+ it("should not render the Global or Other group headers when no global guardrails exist", async () => {
+ const user = userEvent.setup({ delay: null });
+ vi.mocked(networking.getGuardrailsList).mockResolvedValue({
+ guardrails: [guardrail("dwacxzcz", false), guardrail("dwadsa", false)],
+ });
+
+ const dropdown = await openGuardrailsDropdown(user);
+
+ await waitFor(() => {
+ expect(within(dropdown).getByTitle("dwacxzcz")).toBeInTheDocument();
+ });
+ expect(within(dropdown).getByTitle("dwadsa")).toBeInTheDocument();
+ expect(within(dropdown).queryByText("Global")).not.toBeInTheDocument();
+ expect(within(dropdown).queryByText("Other")).not.toBeInTheDocument();
+ });
+
+ it("should not render the Global or Other group headers when every guardrail is global", async () => {
+ const user = userEvent.setup({ delay: null });
+ vi.mocked(networking.getGuardrailsList).mockResolvedValue({
+ guardrails: [guardrail("always-on", true)],
+ });
+
+ const dropdown = await openGuardrailsDropdown(user);
+
+ await waitFor(() => {
+ expect(within(dropdown).getByTitle("always-on")).toBeInTheDocument();
+ });
+ expect(within(dropdown).queryByText("Global")).not.toBeInTheDocument();
+ expect(within(dropdown).queryByText("Other")).not.toBeInTheDocument();
+ });
+
+ it("should render both group headers when global and non-global guardrails exist", async () => {
+ const user = userEvent.setup({ delay: null });
+ vi.mocked(networking.getGuardrailsList).mockResolvedValue({
+ guardrails: [guardrail("always-on", true), guardrail("opt-in", false)],
+ });
+
+ const dropdown = await openGuardrailsDropdown(user);
+
+ await waitFor(() => {
+ expect(within(dropdown).getByText("Global")).toBeInTheDocument();
+ });
+ expect(within(dropdown).getByText("Other")).toBeInTheDocument();
+ expect(within(dropdown).getByTitle("always-on")).toBeInTheDocument();
+ expect(within(dropdown).getByTitle("opt-in")).toBeInTheDocument();
+ });
+ });
});
diff --git a/ui/litellm-dashboard/src/components/team/TeamInfo.tsx b/ui/litellm-dashboard/src/components/team/TeamInfo.tsx
index 74f201b40c6..eaf2faa08ee 100644
--- a/ui/litellm-dashboard/src/components/team/TeamInfo.tsx
+++ b/ui/litellm-dashboard/src/components/team/TeamInfo.tsx
@@ -14,7 +14,7 @@ import {
teamMemberUpdateCall,
teamUpdateCall,
} from "@/components/networking";
-import { useGuardrails } from "@/app/(dashboard)/hooks/guardrails/useGuardrails";
+import { useGuardrails, GuardrailListItem } from "@/app/(dashboard)/hooks/guardrails/useGuardrails";
import { formatNumberWithCommas } from "@/utils/dataUtils";
import { mapEmptyStringToNull } from "@/utils/keyUpdateUtils";
import { isProxyAdminRole } from "@/utils/roles";
@@ -679,6 +679,16 @@ const TeamInfoView: React.FC<TeamInfoProps> = ({
? nonGlobalOptIns
: [...Array.from(globalGuardrailNames).filter((n) => !optedOutGlobals.has(n)), ...nonGlobalOptIns];
+ const allGuardrails: GuardrailListItem[] = guardrailsData?.guardrails ?? [];
+ const globalGuardrails = allGuardrails.filter((g) => g.litellm_params?.default_on);
+ const otherGuardrails = allGuardrails.filter((g) => !g.litellm_params?.default_on);
+
+ const renderGuardrailOption = (g: GuardrailListItem, disabled: boolean) => (
+ <Select.Option key={g.guardrail_name} value={g.guardrail_name} label={g.guardrail_name} disabled={disabled}>
+ {g.guardrail_name}
+ </Select.Option>
+ );
+
const preventTagMouseDown = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
@@ -1264,36 +1274,28 @@ const TeamInfoView: React.FC<TeamInfoProps> = ({
optionLabelProp="label"
tagRender={renderGuardrailTag}
>
- <Select.OptGroup
- label={
- <>
- <GlobalOutlined style={{ marginInlineEnd: 4 }} />
- Global
- </>
- }
- >
- {(guardrailsData?.guardrails ?? [])
- .filter((g) => g.litellm_params?.default_on)
- .map((g) => (
- <Select.Option
- key={g.guardrail_name}
- value={g.guardrail_name}
- label={g.guardrail_name}
- disabled={killSwitchOn}
- >
- {g.guardrail_name}
- </Select.Option>
- ))}
- </Select.OptGroup>
- <Select.OptGroup label="Other">
- {(guardrailsData?.guardrails ?? [])
- .filter((g) => !g.litellm_params?.default_on)
- .map((g) => (
- <Select.Option key={g.guardrail_name} value={g.guardrail_name} label={g.guardrail_name}>
- {g.guardrail_name}
- </Select.Option>
- ))}
- </Select.OptGroup>
+ {globalGuardrails.length > 0 && otherGuardrails.length > 0 ? (
+ <>
+ <Select.OptGroup
+ label={
+ <>
+ <GlobalOutlined style={{ marginInlineEnd: 4 }} />
+ Global
+ </>
+ }
+ >
+ {globalGuardrails.map((g) => renderGuardrailOption(g, Boolean(killSwitchOn)))}
+ </Select.OptGroup>
+ <Select.OptGroup label="Other">
+ {otherGuardrails.map((g) => renderGuardrailOption(g, false))}
+ </Select.OptGroup>
+ </>
+ ) : (
+ [
+ ...globalGuardrails.map((g) => renderGuardrailOption(g, Boolean(killSwitchOn))),
+ ...otherGuardrails.map((g) => renderGuardrailOption(g, false)),
+ ]
+ )}
</Select>
</Form.Item>
diff --git a/ui/litellm-dashboard/tests/test-utils.tsx b/ui/litellm-dashboard/tests/test-utils.tsx
index ed1f248648e..ba07af0f376 100644
--- a/ui/litellm-dashboard/tests/test-utils.tsx
+++ b/ui/litellm-dashboard/tests/test-utils.tsx
@@ -3,7 +3,7 @@ import { render, RenderOptions } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
// Create a client for testing
-const queryClient = new QueryClient({
+export const testQueryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
@@ -20,7 +20,7 @@ const queryClient = new QueryClient({
});
const Providers: React.FC<PropsWithChildren> = ({ children }) => {
- return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
+ return <QueryClientProvider client={testQueryClient}>{children}</QueryClientProvider>;
};
export const renderWithProviders = (ui: React.ReactElement, options?: RenderOptions) =>