#19031: fix(web-search): normalize ui_lang parameter for Brave Search API
agents
size: S
Cluster:
Web Search Provider Enhancements
## 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
#18994: fix(web-search): normalize bare ui_lang codes to full Brave locales
by Phineas1500 · 2026-02-17
90.3%
#15512: fix(web-search): normalize Chinese language codes for Brave API
by brandonwise · 2026-02-13
85.2%
#19314: feat: add Brave web_search baseUrl override (AI-assisted)
by mrutunjay-kinagi · 2026-02-17
76.2%
#18805: fix(web-search): skip invalid bare ui_lang codes in Brave Search re...
by andrey-esipov · 2026-02-17
75.8%
#13370: Tools: rewrite Grok parser, add Tavily provider, multi-provider con...
by a-anand-91119 · 2026-02-10
73.8%
#23306: fix(web-search): hint at config validation failure in missing-key e...
by lbo728 · 2026-02-22
73.7%
#19084: fix: Brave Search baseUrl and Inter-session Message Role
by jackjin1997 · 2026-02-17
73.6%
#19042: Security: add URL allowlist for web_search and web_fetch
by smartprogrammer93 · 2026-02-17
73.0%
#13843: feat(web-search): allow overriding Brave Search base URL
by strelov1 · 2026-02-11
72.2%
#8715: fix(web-search): safer provider resolution & Perplexity auto-detection
by abhijeet117 · 2026-02-04
71.9%