Skip to content

Search

SearchOrchestrator runs a keyword search across every configured source in parallel, then deduplicates and merges the results into a single SearchResult. It is an async context manager.

from opencite import Config
from opencite.search import SearchOrchestrator

async with SearchOrchestrator(Config.from_env()) as searcher:
    result = await searcher.search("graph neural networks", max_results=20)

SearchOrchestrator

Coordinates parallel search across all API clients.

Usage::

async with SearchOrchestrator(config) as searcher:
    result = await searcher.search("deep learning")
    for paper in result.papers:
        print(paper.title)
Source code in src/opencite/search.py
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
class SearchOrchestrator:
    """Coordinates parallel search across all API clients.

    Usage::

        async with SearchOrchestrator(config) as searcher:
            result = await searcher.search("deep learning")
            for paper in result.papers:
                print(paper.title)
    """

    def __init__(self, config: Config):
        self.config = config
        self._openalex = OpenAlexClient(config)
        self._s2 = SemanticScholarClient(config)
        self._pubmed = PubMedClient(config)
        self._arxiv = ArXivClient(config)
        self._biorxiv = BioRxivClient(config)
        self._medrxiv = MedRxivClient(config)
        self._osf = OSFClient(config)
        self._zenodo = ZenodoClient(config)
        self._figshare = FigshareClient(config)
        self._crossref = CrossRefClient(config)
        self._core = COREClient(config)

    async def __aenter__(self) -> SearchOrchestrator:
        await self._openalex.__aenter__()
        await self._s2.__aenter__()
        await self._pubmed.__aenter__()
        await self._arxiv.__aenter__()
        await self._biorxiv.__aenter__()
        await self._medrxiv.__aenter__()
        await self._osf.__aenter__()
        await self._zenodo.__aenter__()
        await self._figshare.__aenter__()
        await self._crossref.__aenter__()
        await self._core.__aenter__()
        return self

    async def __aexit__(self, *args: object) -> None:
        await self._openalex.__aexit__()
        await self._s2.__aexit__()
        await self._pubmed.__aexit__()
        await self._arxiv.__aexit__()
        await self._biorxiv.__aexit__()
        await self._medrxiv.__aexit__()
        await self._osf.__aexit__()
        await self._zenodo.__aexit__()
        await self._figshare.__aexit__()
        await self._crossref.__aexit__()
        await self._core.__aexit__()

    async def search(
        self,
        query: str,
        max_results: int = 20,
        sources: tuple[str, ...] | list[str] | None = None,
        year_from: int | None = None,
        year_to: int | None = None,
        oa_only: bool = False,
        sort: str = "relevance",
    ) -> SearchResult:
        """Search for papers across all configured sources.

        Returns a SearchResult with deduplicated, merged papers.
        """
        explicit_sources = sources is not None
        if sources is None:
            sources = ALL_SOURCES
        # Honor disabled_sources as a baseline filter that can't be
        # overridden by the per-call `sources=` argument. Callers who
        # really want a disabled source can drop it from the config.
        disabled = {canonicalize(d) for d in self.config.disabled_sources}
        if disabled:
            sources = tuple(s for s in sources if canonicalize(s) not in disabled)

        skipped: dict[str, str] = {}
        if "core" in sources and not self._core.enabled:
            # CORE rejects keyless requests. Only mention it when the caller
            # asked for it by name; default searches just leave it out.
            if explicit_sources:
                skipped["core"] = "CORE_API_KEY not set"
                logger.warning("Skipping core: CORE_API_KEY not set")
            sources = tuple(s for s in sources if s != "core")

        tasks: dict[str, asyncio.Task[list[Paper]]] = {}

        if "openalex" in sources:
            tasks["openalex"] = asyncio.create_task(
                self._search_openalex(
                    query, max_results, year_from, year_to, oa_only, sort
                )
            )
        if "s2" in sources:
            tasks["s2"] = asyncio.create_task(self._search_s2(query, max_results))
        if "pubmed" in sources:
            tasks["pubmed"] = asyncio.create_task(
                self._search_pubmed(query, max_results)
            )
        if "arxiv" in sources:
            tasks["arxiv"] = asyncio.create_task(
                self._search_arxiv(query, max_results, year_from, year_to)
            )
        if "biorxiv" in sources:
            tasks["biorxiv"] = asyncio.create_task(
                self._search_biorxiv(query, max_results)
            )
        if "medrxiv" in sources:
            tasks["medrxiv"] = asyncio.create_task(
                self._search_medrxiv(query, max_results)
            )
        if "osf" in sources:
            tasks["osf"] = asyncio.create_task(self._search_osf(query, max_results))
        if "zenodo" in sources:
            tasks["zenodo"] = asyncio.create_task(
                self._search_zenodo(query, max_results)
            )
        if "figshare" in sources:
            tasks["figshare"] = asyncio.create_task(
                self._search_figshare(query, max_results)
            )
        if "crossref" in sources:
            tasks["crossref"] = asyncio.create_task(
                self._search_crossref(query, max_results, year_from, year_to)
            )
        if "core" in sources:
            tasks["core"] = asyncio.create_task(self._search_core(query, max_results))

        all_papers: list[Paper] = []
        source_counts: dict[str, int] = {}
        failed: dict[str, str] = {}

        try:
            for source_name, task in tasks.items():
                try:
                    papers = await task
                    source_counts[source_name] = len(papers)
                    all_papers.extend(papers)
                except Exception as exc:
                    reason = str(exc) or type(exc).__name__
                    logger.warning(
                        "Search failed for source %s: %s", source_name, reason
                    )
                    failed[source_name] = reason
        finally:
            # Make sure no per-source task outlives this call. If the caller
            # cancels search() (e.g. via asyncio.wait_for) part-way through the
            # loop, the tasks not yet awaited would keep running and then hit
            # HTTP clients that __aexit__ has already closed. Cancel and drain
            # whatever is still pending.
            pending = [t for t in tasks.values() if not t.done()]
            for task in pending:
                task.cancel()
            if pending:
                await asyncio.gather(*pending, return_exceptions=True)

        # Deduplicate and merge
        total_before = len(all_papers)
        unique = deduplicate(all_papers)

        # Sort
        unique = _sort_papers(unique, sort)

        # Limit results
        unique = unique[:max_results]

        return SearchResult(
            query=query,
            papers=unique,
            total_by_source=source_counts,
            deduplicated_count=total_before - len(unique),
            failed_sources=failed,
            skipped_sources=skipped,
        )

    async def lookup(
        self,
        identifier: str,
        enrich: bool = False,
    ) -> Paper | None:
        """Look up a single paper by identifier.

        Auto-detects identifier type (DOI, PMID, PMCID, ArXiv, S2, OpenAlex).
        If enrich=True, fetches from all APIs and merges.
        """
        id_type, id_value = parse_identifier(identifier)
        paper = await self._lookup_by_type(id_type, id_value)

        if paper and enrich:
            paper = await self._enrich(paper)

        return paper

    async def batch_lookup(
        self,
        identifiers: list[str],
        enrich: bool = False,
    ) -> list[Paper]:
        """Look up multiple papers by identifiers."""
        tasks = [self.lookup(ident, enrich=enrich) for ident in identifiers]
        results = await asyncio.gather(*tasks, return_exceptions=True)
        papers = []
        for r in results:
            if isinstance(r, Paper):
                papers.append(r)
            elif isinstance(r, Exception):
                logger.warning("Lookup failed: %s", r)
        return papers

    async def _search_openalex(
        self,
        query: str,
        max_results: int,
        year_from: int | None,
        year_to: int | None,
        oa_only: bool,
        sort: str,
    ) -> list[Paper]:
        return await self._openalex.search(
            query,
            max_results=max_results,
            year_from=year_from,
            year_to=year_to,
            oa_only=oa_only,
            sort=sort,
        )

    async def _search_s2(self, query: str, max_results: int) -> list[Paper]:
        return await self._s2.search(query, max_results=max_results)

    async def _search_pubmed(self, query: str, max_results: int) -> list[Paper]:
        return await self._pubmed.search(query, max_results=max_results)

    async def _search_arxiv(
        self,
        query: str,
        max_results: int,
        year_from: int | None,
        year_to: int | None,
    ) -> list[Paper]:
        return await self._arxiv.search(
            query,
            max_results=max_results,
            year_from=year_from,
            year_to=year_to,
        )

    async def _search_biorxiv(self, query: str, max_results: int) -> list[Paper]:
        return await self._biorxiv.search(query, max_results=max_results)

    async def _search_medrxiv(self, query: str, max_results: int) -> list[Paper]:
        return await self._medrxiv.search(query, max_results=max_results)

    async def _search_osf(self, query: str, max_results: int) -> list[Paper]:
        return await self._osf.search(query, max_results=max_results)

    async def _search_zenodo(self, query: str, max_results: int) -> list[Paper]:
        return await self._zenodo.search(query, max_results=max_results)

    async def _search_figshare(self, query: str, max_results: int) -> list[Paper]:
        return await self._figshare.search(query, max_results=max_results)

    async def _search_crossref(
        self,
        query: str,
        max_results: int,
        year_from: int | None,
        year_to: int | None,
    ) -> list[Paper]:
        return await self._crossref.search(
            query, max_results=max_results, year_from=year_from, year_to=year_to
        )

    async def _search_core(self, query: str, max_results: int) -> list[Paper]:
        return await self._core.search(query, max_results=max_results)

    async def _lookup_by_type(self, id_type: IDType, id_value: str) -> Paper | None:
        """Look up paper using the most appropriate API for the ID type."""
        if id_type == IDType.DOI:
            # Try S2 first (fast), fall back to OpenAlex, then CrossRef
            paper = await self._s2.lookup(f"DOI:{id_value}")
            if not paper:
                paper = await self._openalex.lookup_doi(id_value)
            if not paper:
                paper = await self._crossref.lookup_doi(id_value)
            return paper

        if id_type == IDType.PMID:
            paper = await self._pubmed.lookup_pmid(id_value)
            if not paper:
                paper = await self._s2.lookup(f"PMID:{id_value}")
            return paper

        if id_type == IDType.PMCID:
            paper = await self._openalex.lookup_pmcid(id_value)
            if not paper:
                paper = await self._s2.lookup(f"PMCID:{id_value}")
            return paper

        if id_type == IDType.ARXIV:
            # Try arXiv directly first (authoritative), then S2 as fallback
            paper = await self._arxiv.lookup_arxiv_id(id_value)
            if not paper:
                paper = await self._s2.lookup(f"ARXIV:{id_value}")
            return paper

        if id_type == IDType.S2:
            return await self._s2.lookup(id_value)

        if id_type == IDType.OPENALEX:
            # OpenAlex IDs are looked up via DOI endpoint workaround
            return await self._openalex.lookup_doi(id_value)

        return None

    async def _enrich(self, paper: Paper) -> Paper:
        """Enrich a paper with data from all available APIs."""
        from opencite.dedup import merge_papers

        tasks: list[asyncio.Task[Paper | None]] = []

        # Look up from other sources using available IDs
        if paper.doi:
            if "openalex" not in paper.data_sources:
                tasks.append(asyncio.create_task(self._openalex.lookup_doi(paper.doi)))
            if "s2" not in paper.data_sources:
                tasks.append(asyncio.create_task(self._s2.lookup(f"DOI:{paper.doi}")))
            if "pubmed" not in paper.data_sources:
                tasks.append(asyncio.create_task(self._pubmed.lookup_doi(paper.doi)))
            # For bioRxiv/medRxiv preprints, enrich from their Content APIs.
            # Both share the openRxiv DOI prefixes; fan out in parallel and the
            # one that hosts the preprint will return; the other returns None.
            if (
                is_openrxiv_doi(paper.doi)
                and "biorxiv" not in paper.data_sources
                and "medrxiv" not in paper.data_sources
            ):
                tasks.append(asyncio.create_task(self._biorxiv.lookup_doi(paper.doi)))
                tasks.append(asyncio.create_task(self._medrxiv.lookup_doi(paper.doi)))
            # For arXiv DOIs, enrich from the arXiv API directly.
            if (
                paper.doi.lower().startswith("10.48550/arxiv.")
                and "arxiv" not in paper.data_sources
            ):
                tasks.append(asyncio.create_task(self._arxiv.lookup_doi(paper.doi)))
            # OSF Preprints (multi-prefix coverage: PsyArXiv, SocArXiv, ...).
            if OSFClient.is_osf_doi(paper.doi) and not any(
                s == "osf" or s.startswith("osf:") for s in paper.data_sources
            ):
                tasks.append(asyncio.create_task(self._osf.lookup_doi(paper.doi)))
            # Zenodo
            if (
                paper.doi.lower().startswith("10.5281/zenodo.")
                and "zenodo" not in paper.data_sources
            ):
                tasks.append(asyncio.create_task(self._zenodo.lookup_doi(paper.doi)))
            # Figshare
            if (
                paper.doi.lower().startswith("10.6084/m9.figshare.")
                and "figshare" not in paper.data_sources
            ):
                tasks.append(asyncio.create_task(self._figshare.lookup_doi(paper.doi)))
        elif paper.pmid:
            if "openalex" not in paper.data_sources:
                tasks.append(
                    asyncio.create_task(self._openalex.lookup_pmid(paper.pmid))
                )
            if "s2" not in paper.data_sources:
                tasks.append(asyncio.create_task(self._s2.lookup(f"PMID:{paper.pmid}")))
            if "pubmed" not in paper.data_sources:
                tasks.append(asyncio.create_task(self._pubmed.lookup_pmid(paper.pmid)))

        if not tasks:
            return paper

        results = await asyncio.gather(*tasks, return_exceptions=True)
        for r in results:
            if isinstance(r, Paper):
                paper = merge_papers(paper, r)
            elif isinstance(r, Exception):
                # Don't let one source's failure break enrichment, but make
                # the failure visible in logs (parallel to ``batch_lookup``).
                logger.warning(
                    "Enrichment lookup failed for %s: %s",
                    paper.doi or paper.pmid or "<unknown>",
                    r,
                )

        return paper

search async

search(
    query: str,
    max_results: int = 20,
    sources: tuple[str, ...] | list[str] | None = None,
    year_from: int | None = None,
    year_to: int | None = None,
    oa_only: bool = False,
    sort: str = "relevance",
) -> SearchResult

Search for papers across all configured sources.

Returns a SearchResult with deduplicated, merged papers.

Source code in src/opencite/search.py
async def search(
    self,
    query: str,
    max_results: int = 20,
    sources: tuple[str, ...] | list[str] | None = None,
    year_from: int | None = None,
    year_to: int | None = None,
    oa_only: bool = False,
    sort: str = "relevance",
) -> SearchResult:
    """Search for papers across all configured sources.

    Returns a SearchResult with deduplicated, merged papers.
    """
    explicit_sources = sources is not None
    if sources is None:
        sources = ALL_SOURCES
    # Honor disabled_sources as a baseline filter that can't be
    # overridden by the per-call `sources=` argument. Callers who
    # really want a disabled source can drop it from the config.
    disabled = {canonicalize(d) for d in self.config.disabled_sources}
    if disabled:
        sources = tuple(s for s in sources if canonicalize(s) not in disabled)

    skipped: dict[str, str] = {}
    if "core" in sources and not self._core.enabled:
        # CORE rejects keyless requests. Only mention it when the caller
        # asked for it by name; default searches just leave it out.
        if explicit_sources:
            skipped["core"] = "CORE_API_KEY not set"
            logger.warning("Skipping core: CORE_API_KEY not set")
        sources = tuple(s for s in sources if s != "core")

    tasks: dict[str, asyncio.Task[list[Paper]]] = {}

    if "openalex" in sources:
        tasks["openalex"] = asyncio.create_task(
            self._search_openalex(
                query, max_results, year_from, year_to, oa_only, sort
            )
        )
    if "s2" in sources:
        tasks["s2"] = asyncio.create_task(self._search_s2(query, max_results))
    if "pubmed" in sources:
        tasks["pubmed"] = asyncio.create_task(
            self._search_pubmed(query, max_results)
        )
    if "arxiv" in sources:
        tasks["arxiv"] = asyncio.create_task(
            self._search_arxiv(query, max_results, year_from, year_to)
        )
    if "biorxiv" in sources:
        tasks["biorxiv"] = asyncio.create_task(
            self._search_biorxiv(query, max_results)
        )
    if "medrxiv" in sources:
        tasks["medrxiv"] = asyncio.create_task(
            self._search_medrxiv(query, max_results)
        )
    if "osf" in sources:
        tasks["osf"] = asyncio.create_task(self._search_osf(query, max_results))
    if "zenodo" in sources:
        tasks["zenodo"] = asyncio.create_task(
            self._search_zenodo(query, max_results)
        )
    if "figshare" in sources:
        tasks["figshare"] = asyncio.create_task(
            self._search_figshare(query, max_results)
        )
    if "crossref" in sources:
        tasks["crossref"] = asyncio.create_task(
            self._search_crossref(query, max_results, year_from, year_to)
        )
    if "core" in sources:
        tasks["core"] = asyncio.create_task(self._search_core(query, max_results))

    all_papers: list[Paper] = []
    source_counts: dict[str, int] = {}
    failed: dict[str, str] = {}

    try:
        for source_name, task in tasks.items():
            try:
                papers = await task
                source_counts[source_name] = len(papers)
                all_papers.extend(papers)
            except Exception as exc:
                reason = str(exc) or type(exc).__name__
                logger.warning(
                    "Search failed for source %s: %s", source_name, reason
                )
                failed[source_name] = reason
    finally:
        # Make sure no per-source task outlives this call. If the caller
        # cancels search() (e.g. via asyncio.wait_for) part-way through the
        # loop, the tasks not yet awaited would keep running and then hit
        # HTTP clients that __aexit__ has already closed. Cancel and drain
        # whatever is still pending.
        pending = [t for t in tasks.values() if not t.done()]
        for task in pending:
            task.cancel()
        if pending:
            await asyncio.gather(*pending, return_exceptions=True)

    # Deduplicate and merge
    total_before = len(all_papers)
    unique = deduplicate(all_papers)

    # Sort
    unique = _sort_papers(unique, sort)

    # Limit results
    unique = unique[:max_results]

    return SearchResult(
        query=query,
        papers=unique,
        total_by_source=source_counts,
        deduplicated_count=total_before - len(unique),
        failed_sources=failed,
        skipped_sources=skipped,
    )

lookup async

lookup(
    identifier: str, enrich: bool = False
) -> Paper | None

Look up a single paper by identifier.

Auto-detects identifier type (DOI, PMID, PMCID, ArXiv, S2, OpenAlex). If enrich=True, fetches from all APIs and merges.

Source code in src/opencite/search.py
async def lookup(
    self,
    identifier: str,
    enrich: bool = False,
) -> Paper | None:
    """Look up a single paper by identifier.

    Auto-detects identifier type (DOI, PMID, PMCID, ArXiv, S2, OpenAlex).
    If enrich=True, fetches from all APIs and merges.
    """
    id_type, id_value = parse_identifier(identifier)
    paper = await self._lookup_by_type(id_type, id_value)

    if paper and enrich:
        paper = await self._enrich(paper)

    return paper

batch_lookup async

batch_lookup(
    identifiers: list[str], enrich: bool = False
) -> list[Paper]

Look up multiple papers by identifiers.

Source code in src/opencite/search.py
async def batch_lookup(
    self,
    identifiers: list[str],
    enrich: bool = False,
) -> list[Paper]:
    """Look up multiple papers by identifiers."""
    tasks = [self.lookup(ident, enrich=enrich) for ident in identifiers]
    results = await asyncio.gather(*tasks, return_exceptions=True)
    papers = []
    for r in results:
        if isinstance(r, Paper):
            papers.append(r)
        elif isinstance(r, Exception):
            logger.warning("Lookup failed: %s", r)
    return papers