Pydantic models for the Reasoner API data formats
Project description
Reasoner-Pydantic
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 usemodel_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:
- Use
model_validate()exclusively for constructing models. This will perform validation for you. This option is best if performance is not important. - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab44042afd826deecb6b8c05e5103364598c5d62e3e6ef586c602bf1ffd25fe4
|
|
| MD5 |
3d83063adc2643ef230740099c71c186
|
|
| BLAKE2b-256 |
e49453eaf0897ac0a2b6467a80767a89b2c01ae34302d739ed63cf5dcb536be4
|
File details
Details for the file reasoner_pydantic-6.0.0-py3-none-any.whl.
File metadata
- Download URL: reasoner_pydantic-6.0.0-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3847b0b9c96b946aeb834570689a2fb207b0d02c3d81feafc513c1b4f4bf768
|
|
| MD5 |
ae8aa6aace74fa512f7651a77a333653
|
|
| BLAKE2b-256 |
e37c570aede71494d86640aa69d2f8025151f0eb420b4f9e40505ff99c1313f1
|