Entities
This page is for the practitioner who wants a typed Python shape for Visual Investigator
payloads and needs to read entity-type metadata before working with documents. All snippets
below assume svi is an authenticated SVIClient.
The sasvi.entities package has two jobs:
BaseVIEntitygives common VI JSON payloads a stable, validated Python shape.EntityMetadataFacade(surfaced onsvi.admin.entity_types) reads entity-type definitions so you know a field's name, data type, and constraints before you write to it.
Entity types describe the schema; documents are the instances of those types. This guide covers the metadata and modeling layer; the Documents guide covers the bound document objects you create against it.
Before this example
Entity type names and field names are deployment-specific. Before you adapt a snippet, use Discover your environment to find:
- a real entity type name for
"<your-entity-type>" - the real field names behind placeholders like
"<your-field-name>"
What BaseVIEntity is
BaseVIEntity is the shared Pydantic base for the handwritten VI entity models. It is configured
to allow extra fields (so unknown VI keys are preserved) and to populate by field name or alias.
Every model in the feature layer that maps a VI payload inherits from it: insight cells, network
nodes, and your own typed models.
Two methods do the alias-aware round trip:
from sasvi.entities import BaseVIEntity
class InvestigationEntity(BaseVIEntity):
investigation_name: str
investigation_summary: str | None = None
# Validate and coerce a raw VI payload (mapping or existing model).
entity = InvestigationEntity.from_vi_dict({
"investigation_name": "Review Required",
"extra_vi_key": "kept",
})
# Serialize back out using VI field aliases.
payload = entity.to_vi_dict()
print(payload)
from_vi_dict(...) is a thin wrapper over model_validate, and to_vi_dict() serializes with
by_alias=True. Because the base sets extra="allow", keys you did not model (like
extra_vi_key above) survive the round trip instead of being dropped.
Defining a typed entity model
Give your model a field per VI attribute you care about. Use Pydantic Field(alias=...) when the
VI wire name does not match a valid Python identifier or your preferred attribute name:
from pydantic import Field
from sasvi.entities import BaseVIEntity
class SupplierEntity(BaseVIEntity):
supplier_id: str
display_name: str = Field(alias="displayLabel")
risk_score: float | None = None
supplier = SupplierEntity.from_vi_dict({
"supplier_id": "S-1",
"displayLabel": "Acme Ltd",
"risk_score": 72.5,
})
print(supplier.display_name) # "Acme Ltd"
print(supplier.to_vi_dict()) # {"supplier_id": ..., "displayLabel": ..., ...}
Typed models are optional, because the document layer accepts plain dictionaries. They buy you editor completion, validation, and a single place to document each field.
Reading entity-type metadata
Entity-type metadata reads go through EntityMetadataFacade, which is the base class behind
svi.admin.entity_types. Use it to list the entity types on the tenant or fetch one by name or
stored-object id:
for entity_type in svi.admin.entity_types.list():
print(entity_type.name, entity_type.label)
entity_type = svi.admin.entity_types.get(name="<your-entity-type>")
if entity_type is not None:
print(entity_type.name)
get(...) takes exactly one of name= or entity_type_id=; passing both (or neither) raises
sasvi.errors.ValidationError. It returns None when the type is not found on the tenant.
Once you have an entity type, inspect its fields. This is the same metadata the document layer uses to serialize your values correctly:
if entity_type is not None:
field = entity_type.field("<your-field-name>")
if field is not None:
print(field.data_type)
print(field.logical_data_type)
print(field.writable)
print(field.required)
print(field.allow_multiple_selections)
print(field.constraining_list_name)
You can also list only the writable fields when you are building an update payload:
if entity_type is not None:
writable = entity_type.fields(writable_only=True)
print([f.name for f in writable])
Hydrate a payload through the facade
When you already hold a raw VI payload and a typed model, hydrate(...) validates one into the
other. It is a convenience wrapper over your model's from_vi_dict:
supplier = svi.admin.entity_types.hydrate(
SupplierEntity,
{"supplier_id": "S-1", "displayLabel": "Acme Ltd"},
)
print(supplier.display_name)
How this relates to documents
The metadata you read here is exactly what the Documents guide leans on when it
serializes field values. A bound document exposes its entity type name as document.document_type,
so the two layers connect directly:
document = svi.documents.get(
entity_type_name="<your-entity-type>",
document_id="<your-document-id>",
)
if document is not None:
entity_type = svi.admin.entity_types.get(name=document.document_type)
field = entity_type.field("<your-field-name>") if entity_type is not None else None
if field is not None and field.writable:
document.update_fields(**{field.name: "<your-updated-value>"})
Read the entity type first; write the document second. The metadata tells you whether a field is writable, what data type it expects, and whether it is constrained by reference data. See the Reference lists guide for constrained fields.
The same names drive filtered reads. svi.documents.list(where={...}) and filter_= address
entity field names (field.name above), never the table column names behind them, and only
STRING, INTEGER, SMALLINT, LONG, NUMERIC, BOOLEAN, and REFERENCE_DATA fields can be filtered:
if entity_type is not None:
filterable = [
f.name
for f in entity_type.fields()
if f.data_type in {"STRING", "INTEGER", "SMALLINT", "LONG", "NUMERIC", "BOOLEAN"}
]
print(filterable)
Async
Every method that sends a request has an _async twin, fields and child_fields included.
hydrate sends nothing and stays sync. Use a twin when the lookup is part of an async ingest
flow:
async def list_type_names(svi):
types = await svi.admin.entity_types.list_async()
return [t.name for t in types]
async def get_type(svi, name):
return await svi.admin.entity_types.get_async(name=name)
BaseVIEntity.from_vi_dict and to_vi_dict are pure in-process validation, so they have no
async variant. Call them directly from either context.
Errors
svi.admin.entity_types.get(...)returnsNonefor a missing type; it raisessasvi.errors.ValidationErrorif you pass bothname=andentity_type_id=(or neither). That class is not aValueError: catchsasvi.errors.ValidationErrororsasvi.errors.SVIError.BaseVIEntity.from_vi_dict(...)raisespydantic.ValidationErrorwhen a payload does not match the model (for example a required field is missing or a value has the wrong type).- The lower-level
filter_entity_types(...)helper raisessasvi.errors.NotFoundErrorwhen a requested include name is not present in the tenant's entity types, andsasvi.errors.ValidationErrorwhen an entity type carries no name.
Go further
- Working with the bound instances: Documents guide
- Constrained (reference-data) fields: Reference lists guide
- Need real type and field names first: Discover your environment
- Exact methods: Generated
sasvi.entitiesreference - Design model: Client model