Skip to content

KeyError: 'hash' when parsing Wikidata JSON format references #953

@dpriskorn

Description

@dpriskorn

When passing claims in the standard Wikidata JSON format (as returned by the Wikidata API) to References().from_json(), it fails with KeyError: 'hash'.
Steps to reproduce:
from wikibaseintegrator.models.references import Reference, References

Wikidata format includes 'hash' field in references

wikidata_claims = {
    "P31": [{
        "mainsnak": {...},
        "type": "statement",
        "id": "Q42$123",
        "rank": "normal",
        "references": [{
            "hash": "abc123def456",
            "snaks": {...}
        }]
    }]
}

This fails:

References().from_json(wikidata_claims["P31"][0]["references"])
Error traceback:
File "/usr/local/lib/python3.13/site-packages/wikibaseintegrator/models/references.py", line 57, in from_json
    self.add(reference=Reference().from_json(reference))
File "/usr/local/lib/python3.13/site-packages/wikibaseintegrator/models/references.py", line 136, in from_json
    self.hash = json_data['hash']
KeyError: 'hash'

Expected behavior:
The library should handle both:

  1. The format WITH hash field (Wikidata API format)
  2. The format WITHOUT hash field (when creating new references)
    Environment:
  • wikibaseintegrator version: 0.12.15
  • Python 3.13
    Additional context:
    This affects anyone trying to import entities from Wikidata JSON format to a Wikibase instance using this library for validation.

the file I tried to import in Entitybase is here https://github.com/dpriskorn/entitybase-import/blob/master/test_data/test1.jsonl line 1

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions