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.

These models provide validation for TRAPI messages, as well as useful utilities based on architectural decisions, such as edge merging, result merging, and analysis combination.

Example usage

from reasoner_pydantic import (
    Query,
    Message,
    Node,
    Result,
    CURIE,
)


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

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

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

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

    message.results.add(result)

    return message.model_dump_json()


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

Validation Usage

Typically, you'll want to instantiate models using <ModelName>.model_validate(<data>):

from reasoner_pydantic import KnowledgeGraph

# In Python dict format
kg = KnowledgeGraph.model_validate(<your kg dict here>)
# In raw JSON
kg = KnowledgeGraph.model_validate_json(<your kg JSON string here>)

You can also directly instantiate models, however it's not quite as clean:

from reasoner_pydantic import KnowledgeGraph

kg = KnowledgeGraph(nodes=<your nodes here>, edges=<your edges here>)

In all of these cases, validation is automatically done. In most use-cases it's recommended to use model_validate() as the performance cost is much lower than in pydantic v1. However, if you're absolutely certain your data is valid, you can construct a model without validation:

from reasoner_pydantci import KnowledgeGraph

kg = KnowledgeGraph.model_construct(nodes="fake")
# Won't throw errors

[!WARNING] All values passed to model_construct() will not be transformed into their appropriate models. Pydantic's documentation states that it's often more performant to use model_validate(), so it's highly recommended to avoid using this method.

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]())

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

  1. Use model_validate() 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-6.0.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reasoner_pydantic-6.0.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reasoner-pydantic-6.0.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for reasoner-pydantic-6.0.0.tar.gz
Algorithm Hash digest
SHA256 ab44042afd826deecb6b8c05e5103364598c5d62e3e6ef586c602bf1ffd25fe4
MD5 3d83063adc2643ef230740099c71c186
BLAKE2b-256 e49453eaf0897ac0a2b6467a80767a89b2c01ae34302d739ed63cf5dcb536be4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for reasoner_pydantic-6.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3847b0b9c96b946aeb834570689a2fb207b0d02c3d81feafc513c1b4f4bf768
MD5 ae8aa6aace74fa512f7651a77a333653
BLAKE2b-256 e37c570aede71494d86640aa69d2f8025151f0eb420b4f9e40505ff99c1313f1

See more details on using hashes here.

Supported by

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