~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Batch DOI → OpenAlex source_id enrichment.
Resolves paper DOIs to OpenAlex source IDs in a single batch HTTP request
(up to 50 DOIs per call). This populates ``openalex_source_id`` and
``source_type`` on result dicts so the journal reputation filter can look
up journals by ID rather than fragile name matching.
Runs as a pre-enrichment layer before content filters — the existing
tiered scoring system is unchanged. This just gives Tier 2 (OpenAlex
snapshot lookup) a reliable key to work with.
"""
import unicodedata
from typing import Any, Callable, Dict, List, Optional, Tuple
from loguru import logger
from ..constants import OPENALEX_ENRICHMENT_API_TIMEOUT, USER_AGENT
from ..security.log_sanitizer import scrub_error
from ..security.safe_requests import safe_get
from .citation_normalizer import _extract_doi
_OPENALEX_API = "https://api.openalex.org"
_MAX_DOIS_PER_REQUEST = 50
# HTTP statuses treated as "this credential was refused".
#
# Sourced, not assumed. OpenAlex's published error table
# (https://help.openalex.org/api/errors/) documents 200, 301, 400,
# 403 ("Forbidden - you don't have access to this resource"), 404, 429
# and 500. It documents **no 401**, and
# https://help.openalex.org/api/authentication/ does not state which
# status a rejected key earns. So this is deliberately a small
# *defensive* set: 403 is documented, and 401 is the standard status for
# a refused bearer token. Nothing else is guessed at.
#
# In particular there is no separate "daily budget exceeded" status to
# map: the errors page defines 429 as "Rate limit or daily credit budget
# exceeded; slow down or wait for the reset", and the authentication
# page confirms "Two things return 429 Too Many Requests: exceeding your
# daily budget, or making more than 100 requests per second". 429 is
# already handled by the engine's rate-limit branch, which points at the
# free key that raises the daily budget 10x.
#
# A refused key must never look like "no results": it is an operator
# misconfiguration. See :func:`send_with_key_fallback`.
OPENALEX_AUTH_FAILURE_STATUSES = frozenset({401, 403})
def normalize_openalex_api_key(api_key: Any) -> Optional[str]:
"""Normalize a configured OpenAlex API key to a real key, or ``None``.
``None`` means "make the request keyless" — OpenAlex works without a
key, so an unusable configured value must degrade to the free tier
rather than being sent as a ``Bearer`` token that earns a 401/403 and
turns a working engine into one that returns nothing.
Collapses to ``None``:
* any non-``str`` value (``True``/``1`` from a mis-typed setting used
to raise ``AttributeError`` on ``.strip()``),
* whitespace-only values (``None``, never ``""``),
* the ``"False"`` settings sentinel, case-insensitively,
* every placeholder ``BaseSearchEngine._is_valid_api_key`` rejects —
``your-api-key-here``, ``<your_api_key>``, ``${OPENALEX_API_KEY}``, …
* any value that is not usable as an HTTP header value. Two
independent rules, both required:
- *internal whitespace or control characters*. Such a value cannot
be a real key, and a newline in it makes ``requests`` raise
``InvalidHeader`` whose message embeds the value's ``repr``,
which neither scrub pass can match (the literal holds a real
newline, the message holds a backslash and an "n"). Rejecting it
keeps it out of the logs.
- *not latin-1 encodable*. This is the hard constraint: CPython's
``http.client`` encodes every header value as latin-1
(``values[i] = one_value.encode('latin-1')``), and neither
``requests``' own header validation nor anything below it gates
the encoding. A key carrying U+2010, U+2013 or U+2019 — what a
PDF paste or an editor's autocorrect produces — therefore raises
``UnicodeEncodeError`` *before any request leaves the process*.
That is not an HTTP status, so :func:`send_with_key_fallback`
cannot turn it into a keyless retry by inspecting
``response.status_code``; the value has to be refused here.
Characters that *are* latin-1 (``é``, ``ü``) are left alone —
they encode fine and could be part of a real key.
This lives here, rather than next to the placeholder list in
``web_search_engines/search_engine_base.py``, because all three
OpenAlex callers need it — the search engine, the base-class DOI
enrichment pass, and the research-library downloader — and only this
module is low-level enough for the downloader to import. The
placeholder list itself is *not* duplicated: it is imported inside
the function, which keeps the *module-level* import graph free of the
engine package (``research_library.downloaders.openalex`` imports
this module at module level).
"""
if not isinstance(api_key, str):
return None
candidate = api_key.strip()
# "False" is the repo-wide "unset" sentinel for string settings that
# round-tripped through a boolean UI control; it is not a placeholder
# name, so the shared list does not (and should not) carry it.
if candidate.lower() == "false":
return None
from ..web_search_engines.search_engine_base import (
_is_api_key_placeholder,
)
if _is_api_key_placeholder(candidate):
return None
# Checked after the placeholder list so a whitespace-bearing
# placeholder is still dropped silently rather than warned about.
# The warning deliberately does not include the value.
unusable_as_header = any(
character.isspace() or unicodedata.category(character).startswith("C")
for character in candidate
)
if not unusable_as_header:
# The encoding check the whitespace/control predicates do not
# cover. ``http.client`` encodes header values as latin-1 and
# nothing between here and there catches the resulting
# ``UnicodeEncodeError``, so a key that is not latin-1 encodable
# would take out every OpenAlex call — search, enrichment and
# download — with no status code for the keyless fallback to
# branch on. See the docstring.
try:
candidate.encode("latin-1")
except UnicodeEncodeError:
unusable_as_header = True
if unusable_as_header:
logger.warning(
"Ignoring the configured OpenAlex API key: it is not usable as "
"an HTTP header value (it contains whitespace, control "
"characters, or characters outside latin-1). Re-copy the key "
"from https://openalex.org/settings/api"
)
return None
return candidate
ERROR_BODY_SCRUB_LIMIT = 8192
def bounded_error_body(text: str, limit: int = ERROR_BODY_SCRUB_LIMIT) -> str:
"""Cut an upstream error body down to a bounded, scrub-safe prefix.
``safe_requests`` allows a 1 GiB response body, and running all of it
through the ~18 compiled patterns of the credential scrubbers only to
keep a couple of hundred characters is pointless work. This bounds
the *regex passes* — ``response.text`` has already decoded the whole
body by the time it is called, so it bounds no allocation.
Truncating before scrubbing is only safe if the cut cannot create a
partial credential, because the scrubbers are shape-anchored and a
scrub can *shrink* the text (``Bearer <token>`` collapses to 17
characters), pulling content from far beyond the caller's final cap
into the characters that get logged.
The invariant that makes it safe: :func:`normalize_openalex_api_key`
refuses any key containing whitespace, so a key is always contained
in a single whitespace-free run. Cutting back to the last whitespace
character at or before *limit* therefore drops the trailing partial
run in its entirety — no fragment of a token that straddles the
boundary can survive. A body with no whitespace at all in its first
*limit* characters yields ``""``: the whole prefix is one unbroken
run and none of it can be shown to be credential-free.
Args:
text: The decoded response body.
limit: Maximum number of characters considered before the cut
back to a whitespace boundary.
Returns:
*text* unchanged when it is no longer than *limit*, otherwise a
prefix ending at a whitespace character, possibly ``""``.
"""
if len(text) <= limit:
return text
window = text[:limit]
for index in range(len(window) - 1, -1, -1):
if window[index].isspace():
return window[: index + 1]
return ""
def send_with_key_fallback(
send_request: Callable[[bool], Any],
*,
api_key: Optional[str],
context: str,
on_key_rejected: Optional[Callable[[], None]] = None,
before_retry: Optional[Callable[[], None]] = None,
) -> Tuple[Any, Optional[str]]:
"""Send an OpenAlex request; drop a refused key and retry keyless once.
OpenAlex serves the same endpoints with no key at all, at a lower
daily budget (https://help.openalex.org/api/authentication/), so a
stale, rotated, typo'd or revoked key must never cost the caller its
results. Every LDR call site that can send the OpenAlex credential
goes through this one function so the guarantee cannot be
implemented in one place and forgotten in another.
On an auth failure (:data:`OPENALEX_AUTH_FAILURE_STATUSES`) *while a
key was actually sent*, or when the keyed send raises
``UnicodeEncodeError`` (see below):
* exactly one warning is logged. It is scrub-safe by construction:
the only interpolated values are *context* (a caller literal) and
the integer status code — the key itself is never formatted into
it;
* the key is dropped. This function returns ``None`` as the key in
effect and calls *on_key_rejected* so the caller can clear any
cached ``Authorization`` header, which means no later request pays
another wasted round-trip with a credential already known to be
refused;
* *before_retry* runs (rate-limit accounting for the extra request);
* the same request is retried once, keyless.
When no key was sent, or the response is not an auth failure, the
response is returned untouched and nothing is logged — keyless
behaviour is exactly what it was before the key existed.
If the keyless retry fails too, its response is returned as-is and
the caller applies its own pre-existing failure handling.
``UnicodeEncodeError`` from the keyed send is treated exactly like a
rejection. It is the one exception that can only come from encoding a
request *header value*: ``http.client`` encodes header values as
latin-1 *before* opening the connection, so no request went out and a
keyless resend is safe and correct. (URLs and query parameters are
percent-encoded by ``requests`` and raise ``InvalidURL``, not
``UnicodeEncodeError``.) Which header is not knowable here.
:func:`normalize_openalex_api_key` already refuses non-latin-1 keys,
so on every in-tree path the ``Authorization`` value is already
known-encodable and the likelier source is another configured header
value — the contact email interpolated into the ``User-Agent``, which
``requests`` emits before ``Authorization``. Dropping the key is
still the right response: it is the only credential-bearing header,
and a keyless resend costs nothing when the key was not at fault.
The catch is deliberately this one exception type: any other failure
is a real network or protocol error, and retrying it keyless would
both hide the fault and throw away a key the server never refused.
Args:
send_request: ``send_request(with_api_key)`` issues the request
and returns the response. It MUST omit the ``Authorization``
header when *with_api_key* is False.
api_key: The key that will be sent, or ``None`` for keyless.
context: Short literal naming the call site, for the log line.
on_key_rejected: Called once, before the retry, to clear the
caller's cached credential state.
before_retry: Called once, immediately before the retry.
Returns:
``(response, api_key)`` where *api_key* is ``None`` once dropped.
"""
def _drop_and_resend_keyless() -> Tuple[Any, Optional[str]]:
# on_key_rejected runs BEFORE the resend, so even if the keyless
# request raises, the refused key is already gone and no later
# call can send it again.
if on_key_rejected is not None:
on_key_rejected()
if before_retry is not None:
before_retry()
return send_request(False), None
try:
response = send_request(bool(api_key))
except UnicodeEncodeError:
if not api_key:
# Nothing to drop and nothing to retry differently: the
# failure did not come from the credential.
raise
# The exception carries one character of the offending header
# value and its offset, so it is never interpolated into the log
# line.
logger.warning(
f"{context}: a configured OpenAlex request header value (the "
"API key or the contact email used in the User-Agent) cannot "
"be encoded as an HTTP header value; dropping the key and "
"continuing keyless. Re-check both settings at "
"https://openalex.org/settings/api"
)
return _drop_and_resend_keyless()
if not api_key:
return response, api_key
if response.status_code not in OPENALEX_AUTH_FAILURE_STATUSES:
return response, api_key
logger.warning(
f"{context}: OpenAlex rejected the configured API key "
f"(HTTP {response.status_code}); dropping it and continuing "
"keyless. Check or rotate the key at "
"https://openalex.org/settings/api"
)
return _drop_and_resend_keyless()
def _normalize_doi(doi: str) -> str:
"""Normalize a DOI to ``https://doi.org/<...>`` form for OpenAlex.
The ``startswith`` prefix checks below are fully anchored and
CodeQL-safe. A previous code-scanning bot comment cited alert 7635
(``py/incomplete-url-substring-sanitization``) against an earlier
snapshot of this file; the current CodeQL scan does not raise it,
and the anchored ``startswith`` pattern is the rule's recommended
mitigation. Refactoring to a bare-first normalization was
evaluated (PR #3081) but rejected as no-op churn — the
``https://doi.org/`` form OpenAlex actually returns round-trips
unchanged through every branch here. Do not refactor without a
new, reproducible functional issue.
"""
doi = doi.strip()
if doi.startswith("https://doi.org/"):
return doi
if doi.startswith("http://doi.org/"):
return doi.replace("http://", "https://")
if doi.startswith("10."):
return f"https://doi.org/{doi}"
return doi
def enrich_results_with_source_ids(
results: List[Dict[str, Any]],
email: Optional[str] = None,
api_key: Optional[str] = None,
*,
on_key_rejected: Optional[Callable[[str], None]] = None,
) -> List[Dict[str, Any]]:
"""
Batch-enrich results with OpenAlex source_id by DOI lookup.
For results that have a DOI but no ``openalex_source_id``, makes a
single batch request to the OpenAlex works endpoint to resolve
DOI → journal/conference source_id.
The results list is modified in-place and also returned.
Args:
results: List of result dicts from search engines.
email: Optional email included in the User-Agent for identification.
api_key: Optional OpenAlex API key for a higher daily request budget.
Normalized through :func:`normalize_openalex_api_key`, so a
placeholder or sentinel value degrades to a keyless request
instead of a guaranteed 401. A real key OpenAlex refuses is
dropped for the rest of this call and every remaining batch
goes out keyless (:func:`send_with_key_fallback`), so a bad
key costs at most one extra request, never the enrichment.
on_key_rejected: Called with the refused key, once, the first
time OpenAlex refuses it. This call's own drop is a local,
so a caller that keeps the engine alive across runs passes
this to latch the rejection on itself — otherwise the next
run resolves the same key from the same settings snapshot
and pays the wasted round-trip again.
``BaseSearchEngine._note_openalex_key_rejected`` is the
production implementation.
Returns:
The same results list, with ``openalex_source_id`` and
``source_type`` injected where resolved.
"""
if not results:
return results
api_key = normalize_openalex_api_key(api_key)
# Collect DOIs for results that need enrichment
doi_to_indices: Dict[str, List[int]] = {}
for i, result in enumerate(results):
# Skip results that already have a source_id
if result.get("openalex_source_id"):
continue
doi = _extract_doi(result)
if doi:
normalized = _normalize_doi(doi)
doi_to_indices.setdefault(normalized, []).append(i)
if not doi_to_indices:
logger.debug("DOI enrichment: no DOIs to resolve")
return results
# Batch DOIs into chunks of MAX_DOIS_PER_REQUEST
all_dois = list(doi_to_indices.keys())
enriched_count = 0
# Kept for redaction after the key is dropped: ``scrub_error`` can
# only redact a literal it is handed, and ``api_key`` becomes None
# the moment OpenAlex refuses it.
rejected_key: Optional[str] = None
def _drop_rejected_key() -> None:
# The single mutation point: this runs *before* the keyless
# retry, so even if that retry raises, the refused key is already
# gone and no later chunk can send it again.
nonlocal api_key, rejected_key
# ``or rejected_key``: a second call must not store None over the
# literal and drop it out of the redaction set.
rejected_key = api_key or rejected_key
api_key = None
if on_key_rejected is not None and rejected_key:
# Last, so a raising callback cannot leave the key un-dropped.
on_key_rejected(rejected_key)
for chunk_start in range(0, len(all_dois), _MAX_DOIS_PER_REQUEST):
chunk = all_dois[chunk_start : chunk_start + _MAX_DOIS_PER_REQUEST]
doi_filter = "|".join(chunk)
params = {
"filter": f"doi:{doi_filter}",
"per_page": str(len(chunk)),
"select": "doi,primary_location",
}
# safe_get auto-injects the project User-Agent. We only override
# it here when an email is configured to identify this client.
headers: Dict[str, str] = {"Accept": "application/json"}
if email:
headers["User-Agent"] = f"{USER_AGENT} ({email})"
# Defaults bind the loop variables (ruff B023): within a chunk the
# params, headers and key are fixed.
def _send(
with_api_key: bool,
_params: Dict[str, str] = params,
_headers: Dict[str, str] = headers,
_api_key: Optional[str] = api_key,
) -> Any:
request_headers = dict(_headers)
if with_api_key and _api_key:
# Bearer header rather than the equally-supported
# ?api_key= query parameter, so the key never lands in a
# URL. https://help.openalex.org/api/authentication/
request_headers["Authorization"] = f"Bearer {_api_key}"
return safe_get(
f"{_OPENALEX_API}/works",
params=_params,
headers=request_headers,
timeout=OPENALEX_ENRICHMENT_API_TIMEOUT,
require_https=True,
)
try:
# A refused key is dropped here for the rest of this
# enrichment pass, so the remaining chunks do not each pay a
# wasted round-trip with a credential already refused.
response, _ = send_with_key_fallback(
_send,
api_key=api_key,
context="DOI enrichment",
on_key_rejected=_drop_rejected_key,
)
# No 401/403 branch of its own: the helper has already logged
# the one "key refused" warning if a key was involved, and a
# keyless 401/403 is not a key problem at all. Either way the
# batch is skipped with exactly the warning this module used
# before the key existed.
if response.status_code != 200:
logger.warning(
f"DOI enrichment: OpenAlex returned {response.status_code}"
)
continue
data = response.json()
works = data.get("results", [])
for work in works:
work_doi = work.get("doi", "")
if not work_doi:
continue
# Normalize for matching
work_doi_normalized = _normalize_doi(work_doi)
# Extract source info
location = work.get("primary_location") or {}
source = location.get("source") or {}
source_id_raw = source.get("id", "")
source_type = source.get("type")
if not source_id_raw:
continue
# Extract short ID from URL
source_id = source_id_raw.split("/")[-1]
# Apply to all results with this DOI
indices = doi_to_indices.get(work_doi_normalized, [])
for idx in indices:
results[idx]["openalex_source_id"] = source_id
if source_type:
results[idx]["source_type"] = source_type
enriched_count += 1
except Exception as exc:
# logger.warning rather than logger.exception: this frame's
# locals hold ``headers`` (the "Authorization: Bearer <key>"
# value) and ``api_key``, which loguru's diagnose would render
# into the traceback. Same documented trade-off as
# ``search_engine_nasa_ads``. This module is not a
# BaseSearchEngine, so it scrubs the key itself rather than
# relying on ``_scrub_error``/``_secret_attrs``.
safe_msg = scrub_error(exc, api_key, rejected_key)
logger.warning(
f"DOI enrichment: OpenAlex batch lookup failed: {safe_msg}"
)
# Graceful: results pass through unenriched
continue
if enriched_count > 0:
logger.info(
f"DOI enrichment: resolved {enriched_count} of "
f"{len(doi_to_indices)} DOIs to OpenAlex source IDs"
)
else:
logger.debug(
f"DOI enrichment: no matches from {len(doi_to_indices)} DOIs"
)
return results