Skip to main content

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

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.

Metadata

Release files for reasoner-pydantic 6.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reasoner-pydantic 6.0.0
File Size Uploaded
reasoner-pydantic-6.0.0.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reasoner-pydantic 6.0.0
File Interpreter ABI Platform
reasoner_pydantic-6.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.3 kB

Release files / reasoner-pydantic-6.0.0.tar.gz

Download URL reasoner-pydantic-6.0.0.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ab44042afd826deecb6b8c05e5103364598c5d62e3e6ef586c602bf1ffd25fe4
BLAKE2b-256 checksum
How to use checksums
e49453eaf0897ac0a2b6467a80767a89b2c01ae34302d739ed63cf5dcb536be4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release files / reasoner_pydantic-6.0.0-py3-none-any.whl

Download URL reasoner_pydantic-6.0.0-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3847b0b9c96b946aeb834570689a2fb207b0d02c3d81feafc513c1b4f4bf768
BLAKE2b-256 checksum
How to use checksums
e37c570aede71494d86640aa69d2f8025151f0eb420b4f9e40505ff99c1313f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

6.0.0 This release

2 release files

5.1.1

2 release files

5.1.0

2 release files

5.0.6

2 release files

5.0.5

2 release files

5.0.4

2 release files

5.0.3

2 release files

5.0.2

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.1.6

2 release files

4.1.5

2 release files

4.1.4

2 release files

4.1.3

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.9

2 release files

4.0.8

2 release files

4.0.7

2 release files

4.0.6

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page