Optimize queries with lazy loading awareness
masterThe ads package uses lazy loading for attributes. If you access an attribute that was not included in the initial API request (e.g., citation_count), the package will trigger a new network request for every single article in your loop. This can lead to $N+1$ requests, quickly exhausting your rate limit.
To prevent this, explicitly request the required fields using the fl (field list) parameter in your SearchQuery. This ensures all data is fetched in a single request ($N=1$).
import ads
# BAD: Triggers a new request for every paper to get citation_count
q_bad = ads.SearchQuery(q='star')
for paper in q_bad:
print(paper.citation_count)
# GOOD: Requests citation_count upfront in the initial call
q_good = ads.SearchQuery(q='star', fl=['id', 'bibcode', 'title', 'citation_count'])
for paper in q_good:
print(paper.citation_count)