Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

MUI Autocomplete: getOptionLabel did not return a string

ErrorMUI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for

Short 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.

  • Applies to MUI v7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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)));
  2. 2Match what you see, then jump to its fix

    1. 1The warning prints an object such as {"id":1,"name":"Lahore"}The options are objects without a label fieldFix
    2. 2Only a few options are named in the warningSome items are missing the field you readFix
    3. 3The warning appears after the user types text that is not in the listfreeSolo hands the function a plain stringFix

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.