Skip to main content

Chainlet classes

APIs for creating user-defined Chainlets.

class truss_chains.ChainletBase

Base class for all chainlets. Inheriting from this class adds validations to make sure subclasses adhere to the chainlet pattern and facilitates remote chainlet deployment. Refer to the docs and this example chainlet for more guidance on how to create subclasses.

class truss_chains.TrussChainlet

Declares an existing Truss directory as a chain member that only receives calls. Unlike ChainletBase, the framework does not generate a model.py or a typed StubBase for this declaration — the Truss directory (a model.py implementation or a docker_server Truss) is deployed as-is. TrussChainlets cannot be entrypoints and cannot declare deps — they are only depended on by ChainletBase chainlets via chains.depends(...), which yields a TrussHandle to the caller.

truss_dir : ClassVar[str]

The Truss directory to wrap. Relative paths resolve against the file that declares the class.

class truss_chains.ModelBase

Base class for all standalone models. Inheriting from this class adds validations to make sure subclasses adhere to the truss model pattern.

class truss_chains.EngineBuilderLLMChainlet

method final async run_remote(llm_input)

Parameters:
  • Returns: AsyncIterator[str]

function truss_chains.depends

Sets a “symbolic marker” to indicate to the framework that a chainlet is a dependency of another chainlet. The return value of depends is intended to be used as a default argument in a chainlet’s __init__-method. When deploying a chain remotely, a corresponding stub to the remote is injected in its place. In run_local mode an instance of a local chainlet is injected. Refer to the docs and this example chainlet for more guidance on how make one chainlet depend on another chainlet.
Despite the type annotation, this does not immediately provide a chainlet instance. Only when deploying remotely or using run_local a chainlet instance is provided.
Parameters:
  • Returns: A “symbolic marker” to be used as a default argument in a chainlet’s initializer.

function truss_chains.depends_context

Sets a “symbolic marker” for injecting a context object at runtime. Refer to the docs and this example chainlet for more guidance on the __init__-signature of chainlets.
Despite the type annotation, this does not immediately provide a context instance. Only when deploying remotely or using run_local a context instance is provided.
  • Returns: A “symbolic marker” to be used as a default argument in a chainlet’s initializer.

class truss_chains.DeploymentContext

Bases: pydantic.BaseModel Bundles config values and resources needed to instantiate Chainlets. The context can optionally be added as a trailing argument in a Chainlet’s __init__ method and then used to set up the chainlet (for example, using a secret as an access token for downloading model weights). Parameters:

method get_baseten_api_key()

  • Returns: str

method get_service_descriptor(chainlet_name)

Parameters:

class truss_chains.Environment

Bases: pydantic.BaseModel The environment the chainlet is deployed in.
  • Parameters: name (str) – The name of the environment.

class truss_chains.ChainletOptions

Bases: pydantic.BaseModel Parameters:

class truss_chains.RPCOptions

Bases: pydantic.BaseModel Options to customize RPCs to dependency chainlets. Parameters:

function truss_chains.mark_entrypoint

Decorator to mark a chainlet as the entrypoint of a chain. This decorator can be applied to one chainlet in a source file and then the CLI push command simplifies: only the file, not the class within, must be specified. Optionally a display name for the Chain (not the Chainlet) can be set (effectively giving a custom default value for the name arg of the CLI push command). Example usage:

Remote Configuration

These data structures specify for each chainlet how it gets deployed remotely, for example, dependencies and compute resources.

class truss_chains.RemoteConfig

Bases: pydantic.BaseModel Bundles config values needed to deploy a chainlet remotely. This is specified as a class variable for each chainlet class, for example, :
Parameters:

class truss_chains.DockerImage

Bases: pydantic.BaseModel Configures the docker image in which a remote chainlet is deployed.
Any paths are relative to the source file where DockerImage is defined and must be created with the helper function [make_abs_path_here] (#function-truss_chains-make_abs_path_here). This allows you for example organize chainlets in different (potentially nested) modules and keep their requirement files right next their python source files.
Parameters:

class truss_chains.BasetenImage

Bases: Enum Default images, curated by baseten, for different python versions. If a Chainlet uses GPUs, drivers will be included in the image.

class truss_chains.CustomImage

Bases: pydantic.BaseModel Configures the usage of a custom image hosted on dockerhub. Parameters:

class truss_chains.Compute

Specifies which compute resources a chainlet has in the remote deployment.
Not all combinations can be exactly satisfied by available hardware, in some cases more powerful machine types are chosen to make sure requirements are met or over-provisioned. Refer to the baseten instance reference.
Parameters: Concurrency concepts are explained in the autoscaling guide. It is important to understand the difference between predict_concurrency and the concurrency target (used for autoscaling, that is, adding or removing replicas). Furthermore, the predict_concurrency of a single instance is implemented in two ways:
  • Via python’s asyncio, if run_remote is an async def. This requires that run_remote yields to the event loop.
  • With a threadpool if it’s a synchronous function. This requires that the threads don’t have significant CPU load (due to the GIL).

class truss_chains.Assets

Specifies which assets a chainlet can access in the remote deployment. For example, model weight caching can be used like this:
Parameters:

Core

General framework and helper functions.

function truss_chains.push

Deploys a chain remotely (with all dependent chainlets). Parameters:
  • Returns: ChainService: A chain service handle to the deployed chain.

class truss_chains.deployment.deployment_client.ChainService

Handle for a deployed chain. A ChainService is created and returned when using push. It bundles the individual services for each chainlet in the chain, and provides utilities to query their status, invoke the entrypoint etc.

method get_info()

Queries the statuses of all chainlets in the chain.
  • Returns: List of DeployedChainlet, (name, is_entrypoint, status, logs_url) for each chainlet.

property name : str

method run_remote(json)

Invokes the entrypoint with JSON data. Parameters:
  • Returns: The JSON response.

property run_remote_url : str

URL to invoke the entrypoint.

property status_page_url : str

Link to status page on Baseten.

function truss_chains.make_abs_path_here

Helper to specify file paths relative to the immediately calling module. For example, in you have a project structure like this:
You can now in root/sub_package/chainlet.py point to the requirements file like this:
This helper uses the directory of the immediately calling module as an absolute reference point for resolving the file location. Therefore, you MUST NOT wrap the instantiation of make_abs_path_here into a function (for example, applying decorators) or use dynamic code execution.Ok:
Not Ok:
Parameters:
  • Returns: AbsPath

function truss_chains.run_local

Context manager local debug execution of a chain. The arguments only need to be provided if the chainlets explicitly access any the corresponding fields of DeploymentContext. Parameters: Example usage (as trailing main section in a chain file):
Refer to the local debugging guide for more details.

class truss_chains.DeployedServiceDescriptor

Bases: pydantic.BaseModel Bundles values to establish an RPC session to a dependency chainlet, specifically with StubBase. Parameters:

class truss_chains.StubBase

Bases: BasetenSession, ABC Base class for stubs that invoke remote chainlets. Extends BasetenSession with methods for data serialization, de-serialization and invoking other endpoints. It is used internally for RPCs to dependency chainlets, but it can also be used in user-code for wrapping a deployed truss model into the Chains framework. It flexibly supports JSON and pydantic inputs and output. Example usage:
Parameters:

classmethod from_url(predict_url, context_or_api_key, options=None)

Factory method, convenient to be used in chainlet’s __init__-method. Parameters:

Invocation Methods

  • async predict_async(inputs: PydanticModel, output_model: Type[PydanticModel]) → PydanticModel
  • async predict_async(inputs: JSON, output_model: Type[PydanticModel]) → PydanticModel
  • async predict_async(inputs: JSON) → JSON
  • async predict_async_stream(inputs: PydanticModel | JSON) -> AsyncIterator[bytes]
Deprecated synchronous methods:
  • predict_sync(inputs: PydanticModel, output_model: Type[PydanticModel]) → PydanticModel
  • predict_sync(inputs: JSON, output_model: Type[PydanticModel]) → PydanticModel
  • predict_sync(inputs: JSON) → JSON

class truss_chains.remote_chainlet.truss_chainlet.TrussHandle

Handle for calling a TrussChainlet sibling. Returned by chains.depends() on a TrussChainlet. Build once (e.g. in __init__), then get call arguments per request and pass them to your own HTTP or WebSocket client. Parameters:

http_call_args(*, prefer_internal=False, sync_path=None, api_key=None)

Returns the URL and headers for an HTTP call to the sibling. prefer_internal uses the internal cluster URL with the matching Host header if available. sync_path rewrites the URL to the /sync/<sync_path> passthrough. api_key overrides the platform-injected chain API key. Parameters:
  • Return type: CallArgs, a named tuple of (url, headers).

ws_call_args(*, sync_path=None, api_key=None)

Returns a wss:// URL and auth-only headers for a WebSocket call to the sibling. WebSocket clients reject Host-header overrides, so this has no prefer_internal kwarg. Parameters:
  • Return type: CallArgs, a named tuple of (url, headers).

class truss_chains.RemoteErrorDetail

Bases: pydantic.BaseModel When a remote chainlet raises an exception, this pydantic model contains information about the error and stack trace and is included in JSON form in the error response. Parameters:

method format()

Format the error for printing, similar to how Python formats exceptions with stack traces.
  • Returns: str

class truss_chains.GenericRemoteException

Bases: Exception Raised when calling a remote chainlet results in an error and it is not possible to re-raise the same exception that was raise remotely in the caller.