← Back to PRs

#19031: fix(web-search): normalize ui_lang parameter for Brave Search API

by moxunjinmu open 2026-02-17 08:21 View on GitHub →
agents size: S
## Problem The `web_search` tool sends bare language codes (e.g. `en`) as the `ui_lang` parameter to the Brave Search API, which only accepts locale codes (e.g. `en-US`). This causes a 422 validation error, completely breaking web search for Brave users. Reported in #18795. ## Solution - Add `normalizeBraveUiLang()` that validates `ui_lang` against Brave's accepted locale set - Map bare language codes to their default locale (e.g. `en` → `en-US`, `de` → `de-DE`) - Silently drop unrecognized values instead of forwarding them to cause API errors - Update the tool schema description to clarify the expected format (locale code, not bare language code) ## Test Plan - Added unit tests for `normalizeBraveUiLang`: valid locales pass through, bare codes get mapped, unrecognized values return undefined, whitespace is trimmed - Updated integration test: verified bare `ui_lang` codes are normalized in the actual Brave API URL, and unrecognized values are omitted - All 57 tests pass (`npx vitest run --config vitest.e2e.config.ts src/agents/tools/web-search.e2e.test.ts src/agents/tools/web-tools.enabled-defaults.e2e.test.ts`) Closes #18795 <!-- greptile_comment --> <h3>Greptile Summary</h3> Fixes Brave Search API 422 errors caused by sending bare language codes (e.g., `en`) as `ui_lang` instead of required locale codes (e.g., `en-US`). Adds a `normalizeBraveUiLang()` function that validates against Brave's accepted locale set, maps common bare language codes to default locales, and silently drops unrecognized values. - Adds `BRAVE_UI_LANGS` set (38 valid locales) and `BRAVE_LANG_TO_DEFAULT_LOCALE` mapping (18 bare codes → locales) as module-level constants - Applies normalization at the Brave API call site, only setting `ui_lang` when a valid locale is resolved - Updates schema description to clarify the expected locale format - Comprehensive unit and integration tests covering pass-through, mapping, rejection, and edge cases - Minor optimization opportunity: the cache key at line 658 uses the raw `ui_lang` rather than the normalized value, which can cause redundant API calls for equivalent inputs (e.g., `"en"` and `"en-US"` produce different cache keys despite resolving to the same request) <h3>Confidence Score: 4/5</h3> - This PR is safe to merge — it fixes a real API validation error with a well-tested normalization function. - The fix is focused and correct: it adds input normalization that prevents 422 errors from the Brave Search API. The locale set and bare-code mapping are accurate. Tests are thorough. The only minor issue is that the cache key uses the raw ui_lang value, which is a small optimization miss rather than a correctness bug. - `src/agents/tools/web-search.ts` — cache key construction at line 658 uses raw `ui_lang` instead of normalized value. <sub>Last reviewed commit: 80117a0</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs