■Abstract
Since around March 1, 2025, updates have been made to the MaterialsProject database, leading to errors occurring during API access when using Python 3.9.
We have organized solutions to address these errors and will introduce them below.
-----------------------------
Table of Contents
■How to Fix 1 (Upgrade into latest library)
-----------------------------
■Error details
I will share the combination of libraries that caused the error and the error code.
| Library | Version |
| python | 3.9.16 |
| pymatgen | 2024.7.18 |
| mp-api | 0.43.0 |
| emmet-core | 0.84.2 |
Error code
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File ~/.py39/lib/python3.9/site-packages/mp_api/client/mprester.py:252, in MPRester.__init__(self, api_key, endpoint, notify_db_version, include_user_agent, monty_decode, use_document_model, session, headers, mute_progress_bars)
249 # Instantiate top level molecules and materials resters and set them as attributes
250 core_suffix = ["molecules/core", "materials/core"]
--> 252 core_resters = {
253 cls.suffix.split("/")[0]: cls(
254 api_key=api_key,
255 endpoint=self.endpoint,
256 include_user_agent=include_user_agent,
257 session=self.session,
258 monty_decode=monty_decode,
259 use_document_model=use_document_model,
260 headers=self.headers,
261 mute_progress_bars=self.mute_progress_bars,
262 )
263 for cls in self._all_resters
264 if cls.suffix in core_suffix
265 }
267 # Set remaining top level resters, or get an attribute-class name mapping
268 # for all sub-resters
269 _sub_rester_suffix_map = {"materials": {}, "molecules": {}}
File ~/.py39/lib/python3.9/site-packages/mp_api/client/mprester.py:253, in <dictcomp>(.0)
249 # Instantiate top level molecules and materials resters and set them as attributes
250 core_suffix = ["molecules/core", "materials/core"]
252 core_resters = {
--> 253 cls.suffix.split("/")[0]: cls(
254 api_key=api_key,
255 endpoint=self.endpoint,
256 include_user_agent=include_user_agent,
257 session=self.session,
258 monty_decode=monty_decode,
259 use_document_model=use_document_model,
260 headers=self.headers,
261 mute_progress_bars=self.mute_progress_bars,
262 )
263 for cls in self._all_resters
264 if cls.suffix in core_suffix
265 }
267 # Set remaining top level resters, or get an attribute-class name mapping
268 # for all sub-resters
269 _sub_rester_suffix_map = {"materials": {}, "molecules": {}}
File ~/.py39/lib/python3.9/site-packages/mp_api/client/core/client.py:124, in BaseRester.__init__(self, api_key, endpoint, include_user_agent, session, s3_client, debug, monty_decode, use_document_model, timeout, headers, mute_progress_bars)
122 self.headers = headers or {}
123 self.mute_progress_bars = mute_progress_bars
--> 124 self.db_version = BaseRester._get_database_version(self.endpoint)
126 if self.suffix:
127 self.endpoint = urljoin(self.endpoint, self.suffix)
File ~/.py39/lib/python3.9/site-packages/mp_api/client/core/client.py:227, in BaseRester._get_database_version(endpoint)
225 date_str = requests.get(url=endpoint + "heartbeat").json()["db_version"]
226 # Convert the string to a datetime object
--> 227 date_obj = datetime.strptime(date_str, "%Y.%m.%d")
229 # Format the datetime object as a string
230 formatted_date = date_obj.strftime("%Y.%m.%d")
File /usr/local/pyenv/versions/3.9.16/lib/python3.9/_strptime.py:568, in _strptime_datetime(cls, data_string, format)
565 def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
566 """Return a class cls instance based on the input string and the
567 format string."""
--> 568 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
569 tzname, gmtoff = tt[-2:]
570 args = tt[:6] + (fraction,)
File /usr/local/pyenv/versions/3.9.16/lib/python3.9/_strptime.py:352, in _strptime(data_string, format)
349 raise ValueError("time data %r does not match format %r" %
350 (data_string, format))
351 if len(data_string) != found.end():
--> 352 raise ValueError("unconverted data remains: %s" %
353 data_string[found.end():])
355 iso_year = year = None
356 month = day = 1
ValueError: unconverted data remains: .post
■How to Fix 1 (Upgrade into latest library)
1, Upgrade mp-api, pymatgen into latest version
pip install -U mp-api pymatgen
2, Downgrade emmet-core into 0.84.2
pip uninstall emmet-core
pip install emmet-core==0.84.2
In this solution, the error can be avoided by using the latest libraries available in the python3.9 environment.
However, please note that the current latest version of mp-api=0.45.3 requires emmet-core>=0.84.3rc6, which may lead to some functionalities being unavailable.
Ref) SymmetryUndeterminedError
■How to Fix 2 (Set endpoint)
1, Set the attribute for endpoint when MPRester used as shown in below
from mp_api.client import MPRester
# A workaround
from mp_api.client.core import BaseRester
setattr(BaseRester, "_get_database_version", lambda _endpoint: "2025.02.12.post")
with MPRester("your_api_key_here") as mpr:
# do stuff with mpr...
In this solution, the error can be avoided by specifying the endpoint for access.
However, it is important to note that each time you use it, you will need to use the above code to circumvent the issue. Furthermore, the uncertain longevity of the endpoint may lead to it suddenly becoming unavailable.