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.
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:
- Use
parse_obj
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
File details
Details for the file reasoner-pydantic-4.1.0.tar.gz
.
File metadata
- Download URL: reasoner-pydantic-4.1.0.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | faba0af0cf85414f820b942aa095926e77c1be3a6edf32be4afcd52b419ec03e |
|
MD5 | 93249cd4a020031bb8b4e74958c095d2 |
|
BLAKE2b-256 | 673abaf17ca2517f59fbfbb6411ed3a305de16368805746d94de6f40181fb5a5 |
File details
Details for the file reasoner_pydantic-4.1.0-py3-none-any.whl
.
File metadata
- Download URL: reasoner_pydantic-4.1.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18df9118f455bb7fb759d74ed29b30895c6498c7f9565c487deb79e754b5f3b6 |
|
MD5 | b9dc305102f2d17531535f7e73978e33 |
|
BLAKE2b-256 | e1a280c5ed64e43a9b6c59c915bc02c1838b5990edb5438a34f154d6f8a97d5e |