fix: Add array type checks for model, agent, and MCP hub data to prev…#20469
Conversation
…ent crashes from non-array API responses and include a regression test.
This PR:Fixes a frontend regression where the
PublicModelHub
page would crash with TypeError: e.filter is not a function when the API returned an error object (e.g. { "detail": "..." }) instead of the expected data array.
Changes:
Added defensive Array.isArray() checks in
src/components/public_model_hub.tsx
for:
modelHubData
agentHubData
mcpHubData
Updated useMemo hooks and helper functions to handle invalid data gracefully.
Added a regression test in
src/components/public_model_hub.test.tsx
that mocks a non-array API response to ensure the component renders without crashing.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
Greptile OverviewGreptile SummaryFixed a frontend crash where the Changes:
The fix is minimal, defensive, and follows best practices for handling potentially invalid API responses. Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| ui/litellm-dashboard/src/components/public_model_hub.tsx | Added defensive Array.isArray() checks in multiple locations to prevent crashes when API returns error objects instead of arrays |
| ui/litellm-dashboard/src/components/public_model_hub.test.tsx | Added regression test that mocks non-array API response to ensure component renders without crashing |
Sequence Diagram
sequenceDiagram
participant User
participant PublicModelHub
participant API
participant useMemo
participant HelperFuncs as Helper Functions
User->>PublicModelHub: Load page
PublicModelHub->>API: modelHubPublicModelsCall()
alt API returns valid array
API-->>PublicModelHub: [model1, model2, ...]
PublicModelHub->>useMemo: Process modelHubData
useMemo->>useMemo: Check Array.isArray(modelHubData)
useMemo-->>PublicModelHub: filteredData array
PublicModelHub->>HelperFuncs: getUniqueProviders(modelHubData)
Note over HelperFuncs: Array.isArray check passed
HelperFuncs->>HelperFuncs: forEach() on array
HelperFuncs-->>PublicModelHub: [provider1, provider2, ...]
PublicModelHub->>User: Render model hub with data
else API returns error object
API-->>PublicModelHub: {detail: "error message"}
PublicModelHub->>useMemo: Process modelHubData
useMemo->>useMemo: Check Array.isArray(modelHubData)
Note over useMemo: Not an array - return []
useMemo-->>PublicModelHub: [] (empty array)
PublicModelHub->>HelperFuncs: getUniqueProviders(modelHubData)
Note over HelperFuncs: Array.isArray check fails
Note over HelperFuncs: Returns [] without calling forEach
HelperFuncs-->>PublicModelHub: []
PublicModelHub->>User: Render empty model hub (no crash)
end
|
|
||
| const filteredData = useMemo(() => { | ||
| if (!modelHubData) return []; | ||
| if (!modelHubData || !Array.isArray(modelHubData)) return []; |
There was a problem hiding this comment.
Having a defensive check here is good, and it solves the problem. However, I believe a better way to solve this is checking for response.ok inside of modelHubPublicModelsCall. This is more standard, and it tells the downstream components that an error explicitly happened instead of pretending it did not and going down the happy route
There was a problem hiding this comment.
I have addressed the comment . Please check
…ormatting across networking functions.
…ent crashes from non-array API responses and include a regression test.
Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitCI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
🧹 Refactoring
✅ Test
Changes
Fixes a frontend regression where the
PublicModelHub
page would crash with TypeError: e.filter is not a function when the API returned an error object (e.g. { "detail": "..." }) instead of the expected data array.
Changes:
Added defensive Array.isArray() checks in
src/components/public_model_hub.tsx
for:
modelHubData
agentHubData
mcpHubData
Updated useMemo hooks and helper functions to handle invalid data gracefully. Added a regression test in
src/components/public_model_hub.test.tsx
that mocks a non-array API response to ensure the component renders without crashing.