fix: add timeout to prompt list fetch - #6644
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a 10-second timeout to the LangChain Hub prompts request and adds unit tests to verify both successful and failed request scenarios. However, the feedback identifies a critical bug in the URL construction where interpolating the tags parameter results in a malformed query string by merging the tags and has_commits parameters. Additionally, the function can return undefined if the response data does not contain repos. It is recommended to refactor the URL construction using URLSearchParams, ensure a consistent return structure, and add a test case to verify tag filtering.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| // Default to 100, TODO: add pagination and use offset & limit | ||
| const url = `https://api.hub.langchain.com/repos/?limit=100&${tags}has_commits=true&sort_field=num_likes&sort_direction=desc&is_archived=false` | ||
| const resp = await axios.get(url) | ||
| const resp = await axios.get(url, { timeout: LANGCHAIN_HUB_PROMPTS_TIMEOUT_MS }) |
There was a problem hiding this comment.
There is a bug in the URL construction on line 9 when tags are provided. If requestBody.tags is present, tags is set to 'tags=some-tag'. When interpolated into the URL:
...&${tags}has_commits=true...
It results in:
...&tags=some-taghas_commits=true...
This merges the tags and has_commits parameters, making the request malformed and breaking the tag filtering.
Additionally, if resp.data.repos is falsy, the function currently reaches the end of the try block without returning anything, resulting in undefined being returned instead of a consistent { status: 'ERROR', repos: [] } or { status: 'OK', repos: [] } structure.
To fix both issues, consider refactoring the URL construction using URLSearchParams and ensuring a consistent return value:
const createPromptsList = async (requestBody: any) => {
try {
const params = new URLSearchParams({
limit: '100',
has_commits: 'true',
sort_field: 'num_likes',
sort_direction: 'desc',
is_archived: 'false'
})
if (requestBody.tags) {
params.append('tags', requestBody.tags)
}
const url = `https://api.hub.langchain.com/repos/?${params.toString()}`
const resp = await axios.get(url, { timeout: LANGCHAIN_HUB_PROMPTS_TIMEOUT_MS })
if (resp.data?.repos) {
return {
status: 'OK',
repos: resp.data.repos
}
}
return { status: 'ERROR', repos: [] }
} catch (error) {
return { status: 'ERROR', repos: [] }
}
}| status: 'OK', | ||
| repos | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Add a test case to verify that the tags parameter is correctly appended to the URL query parameters. This will help prevent regressions and verify the fix for the URL query parameter formatting bug.
})
it('fetches LangChain Hub prompts with tags', async () => {
const repos = [{ id: 'owner/prompt', num_likes: 42 }]
mockedAxios.get.mockResolvedValue({ data: { repos } })
const result = await promptsListsService.createPromptsList({ tags: 'agent' })
expect(mockedAxios.get).toHaveBeenCalledWith(
'https://api.hub.langchain.com/repos/?limit=100&tags=agent&has_commits=true&sort_field=num_likes&sort_direction=desc&is_archived=false',
{ timeout: 10000 }
)
expect(result).toEqual({
status: 'OK',
repos
})
}
Summary
Closes #6514
Tests