SAM Pydantic Base Classes

These are the base Pydantic models used as the foundations for all Smarter API Manifest (SAM) models.

Pydantic models for Smarter API Manifests.

class smarter.lib.manifest.models.AbstractSAMBase(*, apiVersion, kind, metadata, spec, status=None)[source]

Bases: SmarterBasePydanticModel, ABC

Abstract base class for all Smarter API Manifest (SAM) models.

This class serves as the foundational Pydantic model for representing Smarter API manifests. It is intended to be subclassed by concrete manifest classes that define specific resource types within the Smarter API ecosystem.

The AbstractSAMBase class provides a strongly-typed structure for manifest data, ensuring that all manifests adhere to a consistent schema and validation logic. It includes built-in validation for core manifest fields and supports structured access to manifest data.

Subclasses should implement or extend this class to define the specific data and behaviors required for their respective manifest types. This design promotes code reuse, type safety, and robust validation across all Smarter API manifests.

The class also provides methods for validating manifest data and for representing the manifest as a string for debugging or logging purposes.

Note

Do not instantiate this class directly. Instead, create subclasses that define the required fields and any additional validation or methods specific to your manifest type.

apiVersion: str
kind: str
metadata: AbstractSAMMetadataBase
model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

spec: AbstractSAMSpecBase
status: AbstractSAMStatusBase | None
classmethod validate_apiVersion(v)[source]

Validates the apiVersion field for a manifest.

This method ensures that the apiVersion attribute is present and matches one of the supported API versions defined in SmarterApiVersions. If the value is missing or invalid, a SAMValidationError is raised.

Parameters:

v (str) – The value of the apiVersion field to validate.

Raises:

smarter.lib.manifest.exceptions.SAMValidationError – If the value is missing or not a supported version.

Return type:

str

Returns:

The validated apiVersion string.

classmethod validate_metadata(v)[source]

Validates the metadata field for a manifest.

This method ensures that the metadata attribute is an instance of AbstractSAMMetadataBase. If a dictionary is provided, it will be coerced into an AbstractSAMMetadataBase object. This guarantees that the manifest metadata is always properly structured and validated.

Parameters:

v (dict or AbstractSAMMetadataBase) – The value of the metadata field to validate.

Return type:

AbstractSAMMetadataBase

Returns:

The validated metadata object.

class smarter.lib.manifest.models.AbstractSAMMetadataBase(*, name, description, version, tags=<factory>, annotations=<factory>)[source]

Bases: SmarterBasePydanticModel, ABC

Abstract base class for manifest metadata in the Smarter API.

This class defines the required structure and validation logic for metadata associated with Smarter API manifests. It is designed to be subclassed by concrete manifest metadata classes, which may extend or customize the metadata fields as needed for specific resource types.

The AbstractSAMMetadataBase enforces strong typing and validation for core metadata fields, such as resource name, description, version, tags, and annotations. It ensures that all metadata adheres to expected formats and constraints, promoting consistency and reliability across all manifest definitions.

Subclasses should inherit from this class to implement metadata for their specific manifest types. This approach encourages code reuse, enforces validation, and provides a unified interface for working with manifest metadata throughout the Smarter API ecosystem.

Note

This class is abstract and should not be instantiated directly. Instead, create subclasses that define any additional fields or validation required for your manifest’s metadata.

annotations: dict[str, str] | None
description: str | None
model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

name: str
classmethod normalize_annotations(v)[source]

Validate the annotations field, and return it as a map of strings to strings.

Accepts a map, the list of one-key maps of older manifests, or either as a JSON string. See smarter.lib.annotation.validation.normalize_annotations().

Parameters:

v (AnnotationsInput) – The value of the annotations field.

Raises:

smarter.lib.manifest.exceptions.SAMValidationError – If a key or value is invalid.

Return type:

dict[str, str] | None

Returns:

The annotations.

tags: List[str] | None
classmethod validate_description(v)[source]

Validates the description field for a manifest.

A string is stripped of its whitespace. A missing or blank description is None: it is not an error.

Parameters:

v (str) – The value of the description field to validate.

Return type:

str | None

Returns:

The stripped description string, or None if it is missing, blank, or not a string.

classmethod validate_name(v)[source]

Validates the name field for a manifest.

Ensures the value is a non-empty string of at most 50 characters, without characters that are not URL friendly. A name that is not URL friendly, or not in snake_case, is converted, with a warning.

Parameters:

v (str) – The value of the name field to validate.

Raises:

smarter.lib.manifest.exceptions.SAMValidationError – If the value is not a string, is empty, is longer than 50 characters, or has characters that are not URL friendly.

Return type:

str

Returns:

The validated, and possibly converted, name.

classmethod validate_tags(v)[source]

Validates the tags field for a manifest.

This method ensures that each tag in the tags list adheres to URL-friendly character rules. If any tag is invalid, a SAMValidationError is raised.

Parameters:

v (Optional[List[str]]) – The value of the tags field to validate.

Raises:

smarter.lib.manifest.exceptions.SAMValidationError – If any tag is invalid.

Return type:

List[str] | None

Returns:

The validated list of tags.

classmethod validate_version(v)[source]

Validates the version field for a manifest.

A version follows semantic versioning rules. A missing or empty version is None: it is not an error.

Parameters:

v (str) – The value of the version field to validate.

Raises:

smarter.lib.manifest.exceptions.SAMValidationError – If the version is not a semantic version.

Return type:

str | None

Returns:

The validated version string, or None if it is missing or empty.

version: str | None
class smarter.lib.manifest.models.AbstractSAMSpecBase[source]

Bases: SmarterBasePydanticModel, ABC

Pydantic Spec base class.

Expected to be subclassed by specific manifest classes.

model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

class smarter.lib.manifest.models.AbstractSAMStatusBase(*, recordLocator, created, modified, dependencies=None)[source]

Bases: SmarterBasePydanticModel, ABC

Pydantic Status base class.

Expected to be subclassed by specific manifest classes.

created: datetime
dependencies: List[SAMDependency] | None
model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

modified: datetime
recordLocator: str
class smarter.lib.manifest.models.SAMDependency(*, kind, name)[source]

Bases: SmarterBasePydanticModel

A resource that depends on the resource that a manifest describes.

kind: str
model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

name: str
class smarter.lib.manifest.models.SAMDependencyTreeNode(*, kind, icon, name=None, owner=None, recordLocator=None, url=None, detailUrl=None, readable=True, children=<factory>)[source]

Bases: SmarterBasePydanticModel

A resource in a dependency tree: the resource, and, recursively, every resource that it uses.

It is the opposite direction of SAMDependency, which lists the resources that depend on a resource. An LLMClient’s tree has its Provider, its plugins, their Connections, and those Connections’ Secrets. See smarter.lib.manifest.broker.AbstractBroker.dependency_tree().

A resource that the user may not read keeps its kind and record locator, so that the tree still shows that something is there, but not its name, owner or urls, or what it uses in turn.

Each readable resource has two urls: url, its manifest page in the web console, where its manifest can be edited, and detailUrl, the api of its detail (its manifest, what it uses and what uses it), which a resource’s detail dialog in the web console shows when its card is chosen.

Example:

{
  "kind": "LLMClient",
  "icon": "artificial-intelligence",
  "name": "stackademy_sql",
  "owner": "Ada Lovelace (ada)",
  "recordLocator": "llmclient-rMTAwMDAxMwx",
  "url": "/workbench/llm-clients/rMTAwMDAxMwx/manifest/",
  "detailUrl": "/api/web/dashboard/resource/LLMClient/llmclient-rMTAwMDAxMwx/",
  "readable": true,
  "children": [
    {
      "kind": "SqlPlugin",
      "icon": "file-sheet",
      "name": "stackademy_sql",
      "owner": "Ada Lovelace (ada)",
      "recordLocator": "pluginmeta-rMTEx",
      "url": "/llm-tools/plugins/rMTEx/",
      "detailUrl": "/api/web/dashboard/resource/SqlPlugin/pluginmeta-rMTEx/",
      "readable": true,
      "children": []
    }
  ]
}
children: List[SAMDependencyTreeNode]
detailUrl: str | None
icon: str
kind: str
model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True, 'json_schema_serialization_defaults_required': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

name: str | None
owner: str | None
readable: bool
recordLocator: str | None
url: str | None
class smarter.lib.manifest.models.SmarterBasePydanticModel[source]

Bases: BaseModel, SmarterHelperMixin

Smarter API Base Pydantic Model.

__init__(**data)[source]

Initialize the model, with an optional owner that is kept in private attributes.

Parameters:

data – The model’s fields, and optionally user_profile or user, which are not fields. A user profile sets both user_profile and user. A user without a user profile sets them only if the user is authenticated, from the user’s cached user profile.

classmethod coerce_none_strings(data)[source]

Replace the strings "None" and "" with None in the raw input, before validation.

Parameters:

data – The raw input; only a dict is changed, in place.

Returns:

The input.

model_config = {'arbitrary_types_allowed': True, 'from_attributes': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None

property user: User | None

Get the user associated with this manifest, if any.

Returns:

The user of the model’s user profile, or None.

property user_profile: UserProfile | None

Get the user profile associated with this manifest, if any.

Returns:

The user profile that the model was initialized with, or the authenticated user’s, or None.

smarter.lib.manifest.models.client_manifest_validation()[source]

Validate, inside this context, a manifest that a client sent, e.g. to apply or validate it.

Brokers validate a client’s manifest inside it. Models that they build from the database are validated outside of it. Validators that check something outside of the manifest, such as that a url is available, run only inside it (see is_client_manifest()), so that describing a resource never depends on the network.

A context variable is used rather than Pydantic’s validation context, because SmarterBasePydanticModel overrides __init__, through which Pydantic validates nested models without the validation context.

Example:

with client_manifest_validation():
    spec = SAMLLMClientSpec(**manifest["spec"])
Return type:

Iterator[None]

smarter.lib.manifest.models.is_client_manifest()[source]

Whether the manifest that is being validated is one that a client sent.

Return type:

bool

Returns:

True inside client_manifest_validation().