MUI Autocomplete: getOptionLabel did not return a string
MUI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string forShort answer
Autocomplete calls getOptionLabel for every option and expects a string back. The warning means it got undefined. Either the option has no label property, your function reads a field that is missing on some items, or freeSolo passed it a plain string. Return a string in every case.
Fix it fast
1Find out which cause you have
The warning prints the option it failed on. Log your options and look at that one: does it have the field you read in getOptionLabel?
console.log(JSON.stringify(options.find((o) => o.label === undefined)));2Match what you see, then jump to its fix
- 1
The warning prints an object such as {"id":1,"name":"Lahore"}The options are objects without a label fieldFix - 2
Only a few options are named in the warningSome items are missing the field you readFix - 3
The warning appears after the user types text that is not in the listfreeSolo hands the function a plain stringFix
- 1
What does the error look like?
MUI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for
Cause 1 of 3
The options are objects without a label field
1Check
The default is (option) => option.label ?? option. For objects with no label it returns the object itself, which is not a string.
console.log(options[0]);2Fix
Tell Autocomplete which field holds the text.
<Autocomplete
options={cities}
getOptionLabel={(option) => option.name}
renderInput={(params) => <TextField {...params} label="City" />}
/>3Verify
Open the list. Every row shows text and the warning is gone.
console.log(cities.every((c) => typeof c.name === 'string'));Confirmed by mui.com
Cause 2 of 3
Some items are missing the field you read
1Check
Data from an API often has null or absent fields on a few records. option.name is then undefined for those.
console.log(options.filter((o) => typeof o.name !== 'string'));2Fix
Return a fallback string, or filter such records out before passing options.
getOptionLabel={(option) => option.name ?? ''}3Verify
The warning no longer appears when the list loads.
console.log(options.filter((o) => typeof o.name !== 'string').length);Confirmed by mui.com
Cause 3 of 3
freeSolo hands the function a plain string
1Check
With freeSolo, the user input is not bound to the options, so the value can be a string. The docs say getOptionLabel must then accept both the option type and a string.
grep -rn "freeSolo" src/2Fix
Check the type of the argument first.
getOptionLabel={(option) =>
typeof option === 'string' ? option : option.name ?? ''
}3Verify
Type a new value and press Enter. No warning appears.
console.log(typeof option);Confirmed by mui.com
How do I stop it coming back?
- Type your option objects and make the label field required.
- Clean API data before it reaches options.
- When you enable freeSolo, update getOptionLabel in the same change.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
MUI Autocomplete warns that the value matches no option
None of the options match with `0`
Autocomplete compares its value with the options using strict equality.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix