materialsproject / api

New API client for the Materials Project
https://materialsproject.github.io/api/
Other
104 stars 38 forks source link

Request too long (400 error) on get_ion_reference_data() #810

Closed brohr closed 1 year ago

brohr commented 1 year ago

Description I was trying to get the pourbaix entries for a few chemical systems, and the api responded with a 400 error indicating that the URL was too long.

After looking into it a little further, it appears that the problem arises when MPRester's get_ion_reference_data method is called. This calls the query_contributions method, which appears to construct one or many GET requests, and due to the number of IDs in the GET request, the URL is too long.

Ultimately, you can see the response status code and message in _run_futures when that method is called. An HTTPException is not raised, so the program crashes with a KeyError when the response is passed back to the query_contributions method, which expects the response to have the key "result," which it does not.

To Reproduce the Error

from pymatgen.ext.matproj import MPRester

api_key = "your_api_key"
mpr = MPRester(api_key)
entries = mpr.get_pourbaix_entries(["Fe"])

Full Traceback

request failed with status 400!
request failed with status 400!
Traceback (most recent call last):
  File "/Users/brianrohr/Documents/Modelyst/repos/ocp-pipeline/scratch/fail.py", line 6, in <module>
    entries = mpr.get_pourbaix_entries(["Fe"])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/brianrohr/opt/anaconda3/envs/ocp-pipeline/lib/python3.11/site-packages/mp_api/client/mprester.py", line 706, in get_pourbaix_entries
    ion_data = self.get_ion_reference_data_for_chemsys(chemsys)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/brianrohr/opt/anaconda3/envs/ocp-pipeline/lib/python3.11/site-packages/mp_api/client/mprester.py", line 844, in get_ion_reference_data_for_chemsys
    ion_data = self.get_ion_reference_data()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/brianrohr/opt/anaconda3/envs/ocp-pipeline/lib/python3.11/site-packages/mp_api/client/mprester.py", line 803, in get_ion_reference_data
    return self.contribs.query_contributions(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/brianrohr/opt/anaconda3/envs/ocp-pipeline/lib/python3.11/site-packages/mpcontribs/client/__init__.py", line 1498, in query_contributions
    result = resp["result"]
             ~~~~^^^^^^^^^^
KeyError: 'result'
munrojm commented 1 year ago

@brohr, thank you for bringing this up. Sorry for the delayed response!

The client normally takes care of chunking requests to avoid hitting the URL length limit, so I will investigate why that isn't happening here.

munrojm commented 1 year ago

@brohr, this is actually a bug that was fixed last week in the mpcontribs-client package. If you update that to the latest version, you should be good to go.