#18994: fix(web-search): normalize bare ui_lang codes to full Brave locales
agents
size: S
Cluster:
Web Search Provider Enhancements
## Summary
Based on #18931 by @arosstale, with fixes for review feedback:
- Maps bare language codes (e.g. `en`) to full Brave locales (`en-US`) to prevent 422 validation errors
- **Normalizes hyphenated locale case** to `language-REGION` format (e.g. `en-us` → `en-US`, `EN-US` → `en-US`) to satisfy Brave's strict enum validation
- **Converts underscore-separated locales** (e.g. `en_US`, `zh_cn`) to hyphenated form before lookup, preventing them from being silently dropped
- Unknown bare codes are silently dropped (Brave falls back to its own default)
## Test plan
- [x] Unit tests for `normalizeBraveUiLang` (8 tests):
- Bare language codes → full Brave locale
- Case-insensitive bare codes
- Full hyphenated locales pass through with normalized case
- Mixed-case hyphenated locales normalized (`en-us` → `en-US`)
- Underscore locales converted (`en_US` → `en-US`)
- Unknown codes → undefined
- Empty/undefined → undefined
- Whitespace trimming
- [x] Lint and format clean
Fixes #18795
Supersedes #18931
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds locale normalization for Brave Search `ui_lang` parameter to prevent 422 validation errors when LLMs send bare language codes. The implementation maps bare codes like `en` to full Brave locales (`en-US`), normalizes case to `language-REGION` format, and converts underscore separators to hyphens. Test coverage includes 8 unit tests covering all edge cases.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with no risks identified
- The changes are well-isolated to locale normalization logic with comprehensive test coverage (8 tests), proper edge case handling, and clear implementation that directly addresses the 422 validation error issue. The normalization function is pure, side-effect free, and only affects the Brave Search API parameter formatting.
- No files require special attention
<sub>Last reviewed commit: 065c148</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19031: fix(web-search): normalize ui_lang parameter for Brave Search API
by moxunjinmu · 2026-02-17
90.3%
#15512: fix(web-search): normalize Chinese language codes for Brave API
by brandonwise · 2026-02-13
85.0%
#18805: fix(web-search): skip invalid bare ui_lang codes in Brave Search re...
by andrey-esipov · 2026-02-17
72.2%
#19084: fix: Brave Search baseUrl and Inter-session Message Role
by jackjin1997 · 2026-02-17
71.5%
#22165: feat(i18n): translate hardcoded button labels to all 4 locales
by NikolaVujovic1988 · 2026-02-20
71.1%
#16894: Fix text truncation splitting surrogate pairs in web-fetch, subagen...
by Clawborn · 2026-02-15
71.0%
#19314: feat: add Brave web_search baseUrl override (AI-assisted)
by mrutunjay-kinagi · 2026-02-17
70.8%
#16096: fix(i18n): use Unicode-aware word boundaries for non-ASCII language...
by PeterRosdahl · 2026-02-14
69.9%
#11880: fix: guard decodeURIComponent against malformed percent-encoding in...
by Yida-Dev · 2026-02-08
69.8%
#22332: i18n: add Korean (ko-KR) locale support
by arusia88 · 2026-02-21
69.4%