Skip to content

Papers

client.papers talks to the /paper routes and returns Paper models. Later blocks on this page reuse the client created here.

import asyncio

from bibliofabric.semanticscholar import SemanticScholarClient

client = SemanticScholarClient()

paper = await client.papers.get(
    "DOI:10.1038/nature12373",
    select=["title", "year", "citationCount"],
)
print(paper.title, paper.year, paper.citation_count)
Nanometer scale thermometry in a living cell 2013 1847

Get one paper by any id form

get() accepts every documented id form: a bare S2 id (649def34f8be52c8b66281af98ae884c09aef38b), DOI:10.1038/nature12373, ARXIV:1304.1068, PMID:23903748, PMCID:, MAG:, ACL:, PubMedCentral:, CorpusId:, and URL:. A DOI resolver URL such as https://doi.org/10.1038/nature12373 normalizes to the DOI: form in get, batch_get, citations, references, and authors; a bare DOI stays bare, and the API accepts it. The client percent-encodes every id as one path segment, so pass the raw value and not a pre-encoded one. An unknown id raises NotFoundError, and exists() returns False for one instead.

Without select the API answers with paperId only. select takes the API's field names and travels as the fields query parameter; the key-gated names (embedding, openAccessDoi) answer HTTP 400 keyless:

paper = await client.papers.get(
    "DOI:10.1038/nature12373",
    select=["title", "year", "citationCount", "externalIds", "tldr"],
)
print(paper.external_ids.doi, "|", paper.tldr.text[:60])
10.1038/nature12373 | A new approach to nanoscale thermometry is demonstrated that

The Paper model uses snake_case field names (paper_id, citation_count, open_access_pdf) and also accepts the API's camelCase wire names on input. A convenience doi property reads external_ids.doi. Fields the API adds later land in model_extra. Results covers the models.

Search papers

search() returns one Page. page and page_size map to the API's offset and limit:

page = await client.papers.search(
    query="photosynthesis",
    select=["title", "year", "citationCount"],
    page_size=5,
)
print(page.total)
for paper in page.results[:2]:
    print(paper.year, paper.citation_count, paper.title[:60])
153278
2016 6368 Graphitic Carbon Nitride (g-C3N4)-Based Photocatalysts for A
1989 7089 Carbon Isotope Discrimination and Photosynthesis

One relevance query reaches at most 10,000 records (offset + page_size must stay below 10,000); the client refuses a page whose window crosses the cap with ConfigurationError instead of letting the API answer HTTP 400, and the error names the bulk token route as the way past the cap. Paging covers the mechanics.

Filters are their own query parameters. Pass them as the typed model or a plain mapping:

from bibliofabric.semanticscholar import PaperSearchFilters

page = await client.papers.search(
    PaperSearchFilters(year="2020-2023", min_citation_count=50),
    query="crispr",
    select=["title", "year"],
    page_size=5,
)
print(page.total, page.results[0].year)
3629 2020

The mapping form sends the same request: client.papers.search({"venue": "Nature"}, query="crispr"). open_access_pdf="" restricts the results to papers with a public PDF. Passing a PaperSearchFilters object as query= raises ConfigurationError.

bulk_search() returns up to 1000 records per call on the /paper/search/bulk route, ordered by corpus id rather than relevance. The route has no limit parameter; the answer carries a continuation token, and page.next_cursor holds it:

page = await client.papers.bulk_search(query="crispr", select=["title", "year"])
print(page.total, len(page.results))

papers = [
    paper async for paper in client.papers.bulk_iterate(query="crispr", limit=200)
]
print(len(papers))
102734 1000
200

bulk_search() takes the filters as its first positional argument and the query as the query= keyword; params={"sort": "publicationDate:asc"} adds the sort. Pass token=page.next_cursor to fetch the next page. bulk_iterate() follows the tokens for you and stops at limit; limit=0 yields nothing without a request, and a negative limit raises ConfigurationError. It raises APIError instead of looping if the API answers the same token twice.

Harvest with iterate and collect

iterate() and collect() follow the bulk tokens by default, so a harvest walks the whole result set instead of stopping at the 10,000-record window of the relevance search:

papers = [paper async for paper in client.papers.iterate(query="crispr", limit=200)]

Pass via="search" to page the relevance route with offsets instead; that form takes page_size and rejects nothing else. via="bulk" takes no page_size and resumes from a saved token with cursor=. first() reads the first row of the relevance-ranked /paper/search route with page_size=1 and rejects a cursor, because a bulk cursor does not address that route.

Fetch many papers at once

batch_get() sends POST /paper/batch calls of up to 500 ids each and returns a dict keyed by the requested id. The API answers null for unknown ids, and the client drops those entries, so the result can be shorter than the id list. The POST is a read and is sent with retries:

papers = await client.papers.batch_get(
    ["DOI:10.1038/nature12373", "ARXIV:2106.15928", "DOI:10.9999/nope"],
    select=["title", "year"],
)
print(len(papers), [p.year for p in papers.values()])
2 [2013, 2021]

Blank ids raise ConfigurationError before any request, duplicate ids collapse into one POST entry, and lists longer than 500 are chunked into consecutive POSTs.

Walk the citation graph

citations() returns one page of the papers citing a paper, references() one page of the papers it cites, and authors() one page of its authors. Each citation record carries citing_paper, contexts, intents, contexts_with_intent, and is_influential:

citing = await client.papers.citations(
    "DOI:10.1038/nature12373",
    select=["title", "isInfluential"],
    page_size=5,
)
print(len(citing.results), citing.results[0].citing_paper.title[:40])

references = await client.papers.references(
    "DOI:10.1038/nature12373", select=["title"], page_size=5
)
print(len(references.results))

authors = await client.papers.authors("DOI:10.1038/nature12373", page_size=5)
print(len(authors.results), authors.results[0].name)
5 Luminescence thermometry at the microsca
5
5 G. Kucsko

page.next_cursor is the next offset or None on the last page. The routes serve at most the most recent 10,000 citation and reference edges per paper.

Complete a partial title

autocomplete() returns completion candidates for a partial query, and search_match() returns the single paper whose title matches best, with match_score on the result:

candidates = await client.papers.autocomplete("semanti")
print(len(candidates), candidates[0].title)

match = await client.papers.search_match(
    "Nanometer scale thermometry in a living cell",
    select=["title", "year"],
)
print(match.paper.title, match.match_score)
10 Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected CRFs
Nanometer scale thermometry in a living cell 181.30511

search_match() raises NotFoundError when no candidate passes the relevance threshold.

Where to go next