Repository navigation
[Bug]: ridiculous limits on MP-API queries #1103
Copy link
Copy link
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Code snippet
"""Minimal repro: exclude_elements query string >60 chars triggers 422."""
from mp_api.client import MPRester
# 23 radioactive elements -> comma-joined string is 72 chars, exceeds API's 60-char cap
RADIOACTIVE = [
"Tc", "Pm", "Po", "At", "Rn", "Fr", "Ra",
"Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm",
"Bk", "Cf", "Es", "Fm", "Md", "No", "Lr",
]
with MPRester() as mpr:
docs = mpr.materials.summary.search(
num_elements=(2, 2),
exclude_elements=RADIOACTIVE,
fields=["material_id"],
num_chunks=1,
chunk_size=1,
)
# Yields: MPRestError 422 — "exclude_elements - String should have at most 60 characters"What happened?
I want to query for some structures but exclude radioactive elements.
The extremely conservative MP-API settings doesn't let me exclude so many elements.
So how are people supposed to do this query?
Version
current
Which OS?
- MacOS
- Windows
- Linux
Log output
(atomate2_env) ajain:~/Documents/code_venvs/atomate2_env/Part_2-MongoDB_High_Throughput$ python script2.py
Traceback (most recent call last):
File "/Users/ajain/Documents/code_venvs/atomate2_env/Part_2-MongoDB_High_Throughput/script2.py", line 12, in <module>
docs = mpr.materials.summary.search(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ajain/Documents/code_venvs/atomate2_env/lib/python3.11/site-packages/mp_api/client/routes/materials/summary.py", line 393, in search
return super()._search( # type: ignore[return-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ajain/Documents/code_venvs/atomate2_env/lib/python3.11/site-packages/mp_api/client/core/client.py", line 1456, in _search
return self._get_all_documents(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ajain/Documents/code_venvs/atomate2_env/lib/python3.11/site-packages/mp_api/client/core/client.py", line 1520, in _get_all_documents
results = self._query_resource(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/ajain/Documents/code_venvs/atomate2_env/lib/python3.11/site-packages/mp_api/client/core/client.py", line 795, in _query_resource
data = self._submit_requests(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ajain/Documents/code_venvs/atomate2_env/lib/python3.11/site-packages/mp_api/client/core/client.py", line 978, in _submit_requests
data, total_num_docs = self._submit_request_and_process(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ajain/Documents/code_venvs/atomate2_env/lib/python3.11/site-packages/mp_api/client/core/client.py", line 1237, in _submit_request_and_process
raise MPRestError(
mp_api.client.core.exceptions.MPRestError: REST query returned with error status code 422 on URL https://api.materialsproject.org/materials/summary/?nelements_min=2&nelements_max=2&exclude_elements=Tc%2CPm%2CPo%2CAt%2CRn%2CFr%2CRa%2CAc%2CTh%2CPa%2CU%2CNp%2CPu%2CAm%2CCm%2CBk%2CCf%2CEs%2CFm%2CMd%2CNo%2CLr&_limit=1&_fields=material_id with message:
exclude_elements - String should have at most 60 charactersMetadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
computron commentedon May 15, 2026
Also, if data efficiency is desired, note that I now have to overfetch materials and then re-filter on my side. So this is the least data-efficient way to do it.
tsmathis commentedon May 15, 2026
The limits on the elements queries (and related queries with semi-high cardinality fields) is more of a protection thing (for the db) rather than a push for data efficiency. If unbounded inputs were allowed it would be very easy to generate heavy load on the database instance itself very quickly with these types of queries.
That said though, I understand it's frustrating. Especially for your query which is right on the border of the current allowed character limit. So, what type of behavior/limits do you think would be reasonable here? The indexing strategy and query planning for these queries are a few years old at this point so they might be worth auditing.
esoteric-ephemera commentedon May 15, 2026
Just noting that filtering on the user side is very fast, this takes roughly 10s total for me (dependent on one's internet connection):
computron commentedon May 15, 2026