MPRester Query Crashing

I’m having issues querying the following data (the variable my_api_key has been properly defined before the code below):

m = MPRester(my_api_key)
basic_properties = ['task_id','pretty_formula','reduced_cell_formula','unit_cell_formula','spacegroup.number','energy','formation_energy_per_atom','density']
electronic_properties = ['band_gap','efermi','total_magnetization']
elasticity_properties_prefix = ['elasticity']
all_properties = basic_properties + electronic_properties + elasticity_properties_prefix

# Query options
criteria = {"nelements": {"$lte": 3}, "elasticity": {"$exists": True}, "spacegroup.number": {"$exists": True}, "band_gap": {"$exists": True}}

# All properties: query response
results = m.query(criteria=criteria, properties=all_properties)

The script execution stops at the above line after printing all the query results to the screen (don’t know why). In debug mode, the following line is printed out:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pymatgen/matproj/rest.py", line 129, in _make_request

And I end up in the __init__ method of class BufferedIncrementalDecoder (file codecs.py).

I’m using Python 3.6 and pymatgen 4.7.3.

EDIT

I forgot to mention that the same code above did work a month or so ago.

I found a workaround based on this documentation.

import json
import requests

data = {
    'criteria': criteria,
    'properties': all_properties
}
r = requests.post('https://materialsproject.org/rest/v2/query',
                 headers={'X-API-KEY': my_api_key},
                 data={k: json.dumps(v) for k,v in data.items()})
response_content = r.json() # a dict
results = response_content["response"]

Still not sure why the MPRester way stopped working.

Hi, thanks for this report. Your code should now work as expected. The issue was that some of the elasticity data was including structure data in MP’s structure notation language (snl) format, and newer versions of pymatgen do not install the packages required to decode them into Python objects. To correct this, we

  1. no longer include structure information with elasticity data (this was never necessary; consider it a fixed bug in our database build process), and

  2. Warn MPRester users that they may need to install extras via

    pip install pymatgen[matproj.snl]
    

    (this warning will appear in the next release of pymatgen.

Thanks for the report.

2 Likes