openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

IA import 500s if no publish_date

Open cdrini opened this issue 2 years ago • 6 comments

Problem

Evidence

image

Reproducing the bug

Run:

await fetch("https://openlibrary.org/api/import/ia?identifier=robinhood0000gilb_v2r5&require_marc=false&debug=true", {
    "method": "POST",
});
  • Expected behavior: It imports the record
  • Actual behavior: It errors

Context

  • Browser (Chrome, Safari, Firefox, etc):
  • OS (Windows, Mac, etc):
  • Logged in (Y/N): Y
  • Environment (prod, dev, local): prod

Notes from this Issue's Lead

Proposal & constraints

Related files

Stakeholders


Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

cdrini avatar Jul 27 '24 17:07 cdrini

@cdrini it looks like the code would work if e1 just didn't have a publish_date , it is not expecting publish_date to exist and be equal to None.

Is there a way to clean e1 so that empty publish_dates are removed?

hornc avatar Jul 28 '24 08:07 hornc

Oh great catch @hornc ! I didn't pick up on that. I traced it back, and it seems like the most reasonable place might be here:

https://github.com/internetarchive/openlibrary/blob/53d376b148897466bb86d5accb51912bbbe9a8ed/openlibrary/catalog/add_book/init.py#L776-L778

And we can add a thing that if it's None it gets removed. But I'm not entirely sure if this will filter down; it might also be added by something else down the stream.

Alternatively, we can change the first line of the compare_date to use e1.get('public_date') instead of in on line 215. I think this might be more resilient.

cdrini avatar Jul 29 '24 14:07 cdrini

Hi @cdrini @scottbarnes, I'd like to work on this issue. I spent some time investigating, and the problem is in the compare_date function in match.py. When publish_date exists as a key but its value is None, the check 'publish_date' in e1 returns True and the function thinks there's a valid date when there isn't one, eventually causing a 500 error. My proposed fix aligns with your second suggestion, changing the existence checks to use .get() so that None values are treated the same as missing keys, making the function more resilient regardless of where the data comes from. I would love to be assigned this issue!

Saad259 avatar Feb 26 '26 21:02 Saad259

@Saad259 did you still want to try this one? :)

mekarpeles avatar Jul 29 '26 03:07 mekarpeles

Triage note: I think this one was fixed incidentally and can be closed.

The 500 came from compare_date in openlibrary/catalog/add_book/match.py: the old handler was except ValueError as TypeError: (a typo), so the TypeError raised by int(None) when publish_date was present-but-None escaped as a 500. #10462 (Feb 2025) rewrote that block to except (ValueError, TypeError): without referencing this issue, which is why it never got linked.

I verified on current master with the mock_site fixtures: load() on a record with "publish_date": None (plus an ISBN, so it clears the StrongIdentifierBook validator) creates a new edition fine, and against an existing edition with a date it returns modified and keeps the existing "1912". Infobase drops None values on save, so nothing leaks into the stored record either.

Two cosmetic leftovers if anyone wants them, neither user-visible: get_ia_record still emits "publish_date": None when IA metadata has no date, and compare_date would score "exact match" if both sides carried None (can't happen today — editions_match only copies truthy fields from the existing edition). Happy to send a 10-line hardening PR with tests if you'd rather not close on evidence alone; otherwise I'd leave it.

edubraqd avatar Sep 12 '26 19:09 edubraqd

Thanks for the investigation @edubraqd! I agree with your conclusions. This looks fixed now.

hornc avatar Oct 06 '26 09:10 hornc