Skip to main content

Pydantic models for the Reasoner API data formats

Project description

Reasoner-Pydantic

Test status via GitHub Actions ℹ️

Pydantic models for the Reasoner API data formats.

These models are very handy when setting up a Reasoner API with FastAPI.

Example usage

from reasoner_pydantic import (
    Query,
    Message,
    QNode,
    KnowledgeGraph,
    Node,
    Result,
    NodeBinding,
)


def add_result_to_query(query_dict):
    query = Query.parse_obj(query_dict)
    message: Message = query.message

    # get query graph node
    qnode_id = next(iter(message.query_graph.nodes))

    # add knowledge graph node
    knode = Node.parse_obj({"categories": ["biolink:FooBar"]})
    knode_id = "foo:bar"
    message.knowledge_graph.nodes[knode_id] = knode

    # add result
    result: Result = Result.parse_obj(
        {
            "node_bindings": {qnode_id: [{"id": knode_id}]}
        }
    )

    message.results.add(result)

    return message.json()


add_result_to_query({
    "message": {
        "query_graph": {"nodes": {"n0": {}}, "edges": {}},
        "knowledge_graph": {"nodes": {}, "edges": {}},
        "results" : []
    }
})

Validation Usage

Because of performance concerns, as well as how types are implemented in Python, there is no assignment validation enforced on these models. For example:

from reasoner_pydantic import KnowledgeGraph

# This will not throw an error
kg = KnowledgeGraph(nodes = "hi")

This is especially important to keep in mind when constructing objects that use containers. This library uses custom container types: HashableMapping, HashableSequence, HashableSet:

from reasoner_pydantic import KnowledgeGraph, Node, CURIE

# This is not correct and will not throw an error, but will cause problems later
kg = KnowledgeGraph(nodes = {})

# Instead, if you would like to build models this way, use a typed container constructor
kg = KnowledgeGraph(nodes = HashableMapping[CURIE, Node](__root__ = {}))

For this reason, we recommend one of the following options:

  1. Use parse_obj exclusively for constructing models. This will perform validation for you. This option is best if performance is not important.
  2. Use a static type checker to ensure that models are being constructed correctly. Constructing objects this way is more performant, and the static type checker will ensure that it is done correctly. We recommend using pyright in your editor.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reasoner-pydantic-4.0.2.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

reasoner_pydantic-4.0.2-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file reasoner-pydantic-4.0.2.tar.gz.

File metadata

  • Download URL: reasoner-pydantic-4.0.2.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for reasoner-pydantic-4.0.2.tar.gz
Algorithm Hash digest
SHA256 dda36516134fdbac073a049ac17c982fbfb7d5c1f96b3582a529d2173e0776b1
MD5 8d23c91e0636c451fb2d2d70f605cc8a
BLAKE2b-256 19300b0caf804aecea34f304dd45efa4d72e1e4c8c575135c47af8677711c5f4

See more details on using hashes here.

File details

Details for the file reasoner_pydantic-4.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for reasoner_pydantic-4.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b8414b239b6b240a19a3536ef1780d8dd1b75bc62d529427aa02504d76a1e3a9
MD5 ac4bdcb388efd6a41044f6ea75df1fae
BLAKE2b-256 d295fbb6fde8b63089a1f16c5dfc46e71e3c23b8911078681b08fe1b59c890a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page