Error in Data Retrieval

When running the following code to retrieve the chgcar data for a material:


with MPRester(api_key="*******") as mpr:
    material_id="mp-2137"
    chgcar = mpr.get_charge_density_from_material_id(material_id)
    
    # In addition, you can specify the level of theory by using "thermo_type", the default is "GGA_GGA+U_R2SCAN":
    #thermo_docs = mpr.thermo.search(
        #material_ids=["mp-12807"], thermo_types=["GGA_GGA+U", "GGA_GGA+U_R2SCAN", "R2SCAN"])

I get an error that looks like this:

NoSuchKey                                 Traceback (most recent call last)
File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:434, in _get(client, bucket, key, version, range_string)
    432         params["Range"] = range_string
--> 434     return client.get_object(**params)
    435 except botocore.client.ClientError as error:

File ~/.local/lib/python3.10/site-packages/botocore/client.py:565, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
    564 # The "self" in this scope is referring to the BaseClient.
--> 565 return self._make_api_call(operation_name, kwargs)

File ~/.local/lib/python3.10/site-packages/botocore/client.py:1021, in BaseClient._make_api_call(self, operation_name, api_params)
   1020     error_class = self.exceptions.from_code(error_code)
-> 1021     raise error_class(parsed_response, operation_name)
   1022 else:

NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.

The above exception was the direct cause of the following exception:

OSError                                   Traceback (most recent call last)
Cell In[23], line 5
      3 with MPRester(api_key="MY-API-KEY") as mpr:
      4     material_id="mp-2137"
----> 5     chgcar = mpr.get_charge_density_from_material_id(material_id)
      7     # In addition, you can specify the level of theory by using "thermo_type", the default is "GGA_GGA+U_R2SCAN":
      8     #thermo_docs = mpr.thermo.search(
      9         #material_ids=["mp-12807"], thermo_types=["GGA_GGA+U", "GGA_GGA+U_R2SCAN", "R2SCAN"])

File ~/.local/lib/python3.10/site-packages/mp_api/client/mprester.py:1348, in MPRester.get_charge_density_from_material_id(self, material_id, inc_task_doc)
   1339 latest_doc = max(  # type: ignore
   1340     results,
   1341     key=lambda x: x.last_updated  # type: ignore
   1342     if self.use_document_model
   1343     else x["last_updated"],  # type: ignore
   1344 )
   1346 decoder = MontyDecoder().decode if self.monty_decode else json.loads
   1347 chgcar = (
-> 1348     self.tasks._query_open_data(
   1349         bucket="materialsproject-parsed",
   1350         key=f"chgcars/{str(latest_doc.task_id)}.json.gz",
   1351         decoder=decoder,
   1352         fields=["data"],
   1353     )[0]
   1354     or {}
   1355 )
   1357 if not chgcar:
   1358     raise MPRestError(f"No charge density fetched for {material_id}.")

File ~/.local/lib/python3.10/site-packages/mp_api/client/core/client.py:390, in BaseRester._query_open_data(self, bucket, key, decoder, fields)
    376 def _query_open_data(
    377     self, bucket: str, key: str, decoder: Callable, fields: list[str]
    378 ) -> tuple[list[dict] | list[bytes], int]:
    379     """Query and deserialize Materials Project AWS open data s3 buckets.
    380 
    381     Args:
   (...)
    388         dict: MontyDecoded data
    389     """
--> 390     file = open(
    391         f"s3://{bucket}/{key}",
    392         encoding="utf-8",
    393         transport_params={"client": self.s3_client},
    394     )
    396     if "jsonl" in key:
    397         decoded_data = [decoder(jline) for jline in file.read().splitlines()]

File ~/.local/lib/python3.10/site-packages/smart_open/smart_open_lib.py:224, in open(uri, mode, buffering, encoding, errors, newline, closefd, opener, compression, transport_params)
    221 except ValueError as ve:
    222     raise NotImplementedError(ve.args[0])
--> 224 binary = _open_binary_stream(uri, binary_mode, transport_params)
    225 decompressed = so_compression.compression_wrapper(binary, binary_mode, compression)
    227 if 'b' not in mode or explicit_encoding is not None:

File ~/.local/lib/python3.10/site-packages/smart_open/smart_open_lib.py:400, in _open_binary_stream(uri, mode, transport_params)
    398 scheme = _sniff_scheme(uri)
    399 submodule = transport.get_transport(scheme)
--> 400 fobj = submodule.open_uri(uri, mode, transport_params)
    401 if not hasattr(fobj, 'name'):
    402     fobj.name = uri

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:308, in open_uri(uri, mode, transport_params)
    306 parsed_uri, transport_params = _consolidate_params(parsed_uri, transport_params)
    307 kwargs = smart_open.utils.check_kwargs(open, transport_params)
--> 308 return open(parsed_uri['bucket_id'], parsed_uri['key_id'], mode, **kwargs)

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:392, in open(bucket_id, key_id, mode, version_id, buffer_size, min_part_size, multipart_upload, defer_seek, client, client_kwargs, writebuffer)
    389     raise ValueError("version_id must be None when writing")
    391 if mode == constants.READ_BINARY:
--> 392     fileobj = Reader(
    393         bucket_id,
    394         key_id,
    395         version_id=version_id,
    396         buffer_size=buffer_size,
    397         defer_seek=defer_seek,
    398         client=client,
    399         client_kwargs=client_kwargs,
    400     )
    401 elif mode == constants.WRITE_BINARY:
    402     if multipart_upload:

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:698, in Reader.__init__(self, bucket, key, version_id, buffer_size, line_terminator, defer_seek, client, client_kwargs)
    695 self.raw = None
    697 if not defer_seek:
--> 698     self.seek(0)

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:793, in Reader.seek(self, offset, whence)
    788     offset += self._current_pos
    790 if not self._seek_initialized or not (
    791     whence == constants.WHENCE_START and offset == self._current_pos
    792 ):
--> 793     self._current_pos = self._raw_reader.seek(offset, whence)
    795     self._buffer.empty()
    797 self._eof = self._current_pos == self._raw_reader._content_length

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:521, in _SeekableRawReader.seek(self, offset, whence)
    519     self._position = self._content_length
    520 else:
--> 521     self._open_body(start, stop)
    523 return self._position

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:542, in _SeekableRawReader._open_body(self, start, stop)
    538 range_string = smart_open.utils.make_range_string(start, stop)
    540 try:
    541     # Optimistically try to fetch the requested content range.
--> 542     response = _get(
    543         self._client,
    544         self._bucket,
    545         self._key,
    546         self._version_id,
    547         range_string,
    548     )
    549 except IOError as ioe:
    550     # Handle requested content range exceeding content size.
    551     error_response = _unwrap_ioerror(ioe)

File ~/.local/lib/python3.10/site-packages/smart_open/s3.py:442, in _get(client, bucket, key, version, range_string)
    436 wrapped_error = IOError(
    437     'unable to access bucket: %r key: %r version: %r error: %s' % (
    438         bucket, key, version, error
    439     )
    440 )
    441 wrapped_error.backend_error = error
--> 442 raise wrapped_error from error

OSError: unable to access bucket: 'materialsproject-parsed' key: 'chgcars/mp-1436207.json.gz' version: None error: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.

From what I can gather it seems that mp-api is referencing a different material_id(mp-1426207) than my input(mp-2137). This error is not unique to this particular material, but occurs in a few. Here is a list of the ones that have given me the same type of error:

mp-19410
mp-19399
mp-1211715
mp-18750
mp-20758
mp-608016
mp-637188
mp-19202
mp-20167
mp-648893
mp-755882
mp-510598
mp-1103973
mp-36447
mp-540763
mp-2137
mp-18937
Note: this is not a comprehensive list, just ones that I’ve encountered personally.

Is this an error on my end or in mp-api?

Thanks!

Thanks for reaching out. I edited your traceback to remove your API key. Please consider using the MP_API_KEY environment variable to avoid accidentally exposing your API key.

Make sure to use the latest material_id for a material and not a task_id. Run your list of task_id/material_ids through a search to get a mapping:

mpr.materials.search(task_ids=[<list of ids>], fields=["material_id"])

Also, see this thread for how to deal with the remaining NoSuchKey errors: NoSuchKey error - #2 by tschaume