Skip to main content

Reference lists

This page is for the practitioner who manages the code/label lookups that back constrained VI fields. All snippets below assume svi is an authenticated SVIClient.

A reference list is a named collection of items, each with a code and a label. Constrained entity fields (REFERENCE_DATA and enum fields) draw their valid values from one. Reference lists live under the configuration domain, so you reach them through svi.admin.reference_lists.

For an end-to-end validate-then-load workflow, see the recipe Validate input against a reference list. This page is the broader tour.

Before this example​

Reference-list names are deployment-specific. Before you adapt a snippet, use Discover Your Environment to find a real list name for "<your-list-name>".

List and get​

List reference lists a page at a time, or pull the full set:

for record in svi.admin.reference_lists.list(max_items=10):
print(record.name, record.label, record.archived)

everything = svi.admin.reference_lists.list_all()
print(len(everything))

Both take page_size= (how many the server returns per request, default 1000) and max_items= (how many you want back).

Fetch one by name. get(...) returns None when the list does not exist on the tenant:

record = svi.admin.reference_lists.get(name="<your-list-name>")
if record is None:
raise SystemExit("reference list does not exist on this tenant")

Create a reference list​

Create an empty list, or seed it with initial items. Items may be plain code strings or {"code": ..., "label": ...} mappings:

record = svi.admin.reference_lists.create(
name="priority_levels",
label="Priority Levels",
description="Triage priority codes",
items=[
{"code": "high", "label": "High"},
{"code": "medium", "label": "Medium"},
{"code": "low", "label": "Low"},
],
)
print(record.name, len(record.items.list()))

When you only have display labels, create_from_values(...) generates codes for you using a code strategy ("upper_slug", "label", "lower", or "numeric"):

record = svi.admin.reference_lists.create_from_values(
"review_outcomes",
["Confirmed Fraud", "False Positive", "Needs More Info"],
code_strategy="upper_slug",
)
print(record.to_lookup()) # {"CONFIRMED_FRAUD": "Confirmed Fraud", ...}

Add and change items​

A bound ReferenceListRecord reaches its items through the items manager. replace(...) writes the whole collection in one request. append(...) and upsert(...) are read-modify-write helpers built on top of it:

record = svi.admin.reference_lists.get(name="<your-list-name>")
if record is not None:
record.items.append({"code": "urgent", "label": "Urgent"})
record.items.upsert({"code": "high", "label": "High Priority"}) # replace-by-code or add
record.items.remove(code="low")
record.items.replace([
{"code": "open", "label": "Open"},
{"code": "closed", "label": "Closed"},
])

remove(...) leaves the item out of the list the SDK writes back, but the server keeps it and marks it archived rather than deleting it: items.list() still returns it with item.archived == True, and record.item(code) still finds it. to_lookup(...) and validate_values(...) leave archived items out unless you pass include_archived=True, so a retired code no longer validates. append(...) or upsert(...) of a retired code revives the archived item in place instead of adding a second entry. upsert(...) keeps the replaced item's position, id, display index, label and description unless the item you pass carries its own.

Read items back as bound records, or build a lookup for validation and import workflows:

if record is not None:
for item in record.items.list():
print(item.code, item.label)

code_to_label = record.to_lookup(by="code") # {code: label}
label_to_code = record.to_lookup(by="label") # {label: code}

update_metadata(...) changes editable list metadata such as label and description. Assigning to one of those attributes raises AttributeError instead of changing it silently:

if record is not None:
record.update_metadata(description="Updated triage codes")

Labels and locales​

Data Hub stores labels and descriptions per locale. create(...), create_from_values(...) and create_from_mapping(...) write them for locale="en-US". The server rejects a locale the tenant does not support, so pass your tenant's locale if it has no en-US. With locale=None the server ignores your labels.

Updates write the locale the server showed, and keep the other locales. The SDK reads that locale off the list's texts. When the list reads the same in several locales, it raises AdminOperationError without sending anything, and you name one: record.update_metadata(label="Phase", locale="de-DE"). items.replace(...), items.append(...) and items.upsert(...) take locale= too.

A list or item with no label gets one the server makes from its name or code: priority_levels becomes Priority Levels.

Order​

With alphabetical_sort=True, the default, the server sorts the items by itself and accepts only the display index 0, so the SDK sends 0. To keep the order you pass, create the list with alphabetical_sort=False.

Archive a list​

The API has no delete for reference lists. Retire one by archiving it. The list stays readable, list() still returns it, and record.archived reads True:

if record is not None:
record.update_metadata(archived=True)

Items are the same: items.remove(...) archives rather than deletes (see Add and change items).

Validate incoming values​

validate_values(...) checks incoming values against the list's codes or labels and reports every missing and duplicate input at once. It reads the record's loaded copy of the list. A record that has not loaded its items yet, or whose copy a successful write cleared, fetches the list first, so the call can block on the network:

incoming = ["HIGH", "MEDIUM", "URGENT", "LOW", "HIGH"] # one typo, one duplicate

result = record.validate_values(incoming, by="code", case_sensitive=True)

if not result: # bool(result) is False when validation failed
print("missing:", result.missing_values)
print("duplicates:", result.duplicate_values)

ReferenceListValidationResult gives you bool(result) (the valid flag), the compact missing_values and duplicate_values accessors, and raise_for_invalid() for a pipeline that must abort on any bad input.

The ReferenceListValidationError contract​

raise_for_invalid() raises sasvi.errors.ReferenceListValidationError when validation failed and returns None when it passed. The exception is a subclass of both ReferenceListError and the generic ValidationError, and it carries a structured details tuple with one entry per problem, so you never lose the full set of failures:

from sasvi import errors

result = record.validate_values(incoming, by="code")
try:
result.raise_for_invalid()
except errors.ReferenceListValidationError as exc:
for line in exc.details: # ("missing: 'URGENT'", "duplicate: 'HIGH'", ...)
print(line)
raise

ETag and optimistic concurrency​

Reference-list writes are protected against lost updates. When the SDK loads a list snapshot it captures the server's ETag, and sends it back as an If-Match precondition on writes that have one. If another writer changed the list since you loaded it, the server rejects the write with a precondition failure. The SDK turns that into an error telling you to refresh and retry, rather than clobbering the other change:

from sasvi import errors

try:
record.items.append({"code": "new", "label": "New"})
except errors.ReferenceListConcurrentModificationError:
# Someone else changed the list since we loaded it.
record.refresh()
record.items.append({"code": "new", "label": "New"})

ReferenceListConcurrentModificationError is both a ConflictError and a ReferenceListError, so you can catch it by either base.

After every successful write the SDK drops its cached ETag and refreshes the snapshot on the next operation, so a single bound record stays safe to reuse across sequential writes. Call record.refresh() explicitly when you want to re-read before validating.

The record that get() and create() return already holds the ETag of the response that produced it, so the first write carries If-Match with no extra request. When a create response has no ETag, the record starts empty and the first read or write costs one GET. When the server sends no ETag at all, the write goes out without If-Match and the SDK logs a WARNING on the sasvi.reference_lists.service logger.

Async​

Every method that sends a request has a native async twin: list_async, get_async, create_async, create_from_values_async, create_from_mapping_async, refresh_async, update_metadata_async, item_async, to_lookup_async, validate_values_async, and the item manager's list_async, replace_async, append_async, upsert_async and remove_async. item_async, to_lookup_async and validate_values_async read the list once when the record has not loaded its items, and answer from the loaded copy after that:

async def load_codes(svi, name, codes):
record = await svi.admin.reference_lists.get_async(name=name)
if record is None:
return None
result = await record.validate_values_async(codes)
if result:
await record.items.replace_async([{"code": c, "label": c.title()} for c in codes])
return record

Errors​

  • svi.admin.reference_lists.get(name=...) returns None for a missing list.
  • get(name=...) and record.refresh() raise sasvi.errors.MalformedResponseError when the server's body does not parse as a reference list. The message names the model the body parsed as.
  • ReferenceListValidationResult.raise_for_invalid() raises sasvi.errors.ReferenceListValidationError with a structured details tuple.
  • A concurrent modification raises sasvi.errors.ReferenceListConcurrentModificationError, which asks you to refresh and retry (see the ETag section above).
  • to_lookup(...) and validate_values(...) raise AdminOperationError if the list has duplicate codes or, with by="label", duplicate labels (labels are not unique on the server).

Go further​