Skip to content

KGClient.query() ignores id_key parameter when validating instance_id #107

@apdavison

Description

@apdavison

KGClient.query() accepts an id_key parameter (default "@id") to specify which key identifies the ID in the response JSON-LD documents. This parameter is correctly used in the release_status="any" code path (line 319):

instances[instance[id_key]] = instance

However, _check_response() always hardcodes "@id" when validating the instance_id (line 202):

if str(expected_instance_id) not in response.data[0]["@id"]:
    raise Exception("mismatched instance_id")

This means that when a query uses a custom responseVocab that maps @id to a different key (e.g. "id"), calling query() with both instance_id and id_key="id" raises a KeyError: '@id' instead of using the provided id_key.

To reproduce

from fairgraph.client import KGClient

client = KGClient(host="core.kg.ebrains.eu")

# A query whose responseVocab maps @id to "id"
query = {
    "@context": {
        "@vocab": "https://core.kg.ebrains.eu/vocab/query/",
        "query": "https://schema.hbp.eu/myQuery/",
        "propertyName": {"@id": "propertyName", "@type": "@id"},
        "path": {"@id": "path", "@type": "@id"},
    },
    "meta": {
        "type": "https://openminds.om-i.org/types/DatasetVersion",
        "responseVocab": "https://schema.hbp.eu/myQuery/",
    },
    "structure": [
        {"propertyName": "query:id", "path": "@id"},
        {"propertyName": "query:fullName", "path": "https://openminds.om-i.org/props/fullName"},
    ],
}

# Raises KeyError: '@id'
result = client.query(
    query=query,
    instance_id="07554ebd-95a2-46f0-8065-d961d56ce098",
    id_key="id",
    release_status="released",
)

Expected behaviour

_check_response() should use the id_key parameter passed to query() instead of hardcoding "@id".

Suggested fix

Thread id_key through to _check_response and use it on line 202:

if str(expected_instance_id) not in response.data[0][id_key]:

Version: 0.13.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions