Contracts and Specs¶
Note
This is the API reference. The contract is built for you when you serve or connect: for the serving side see Serve an Environment, for the client side Connect a Remote Environment.
A contract is the handshake an endpoint hands a client: the env id, spaces, render mode, metadata, and instance count. You read one off a server or a remote client; you rarely build one by hand. SpaceSpec is the wire-safe description of a single space, and ServeOptions controls the server’s lifecycle.
EnvContract¶
- class rlmesh.specs.EnvContract¶
Immutable description of an environment endpoint.
EnvContract is returned by:
EnvServer.env_contractEnvServer.specRemoteEnv.env_contractRemoteEnv.specRemoteVectorEnv.env_contractRemoteVectorEnv.specsandbox session
env_contractandspecproperties
The server builds the contract when it wraps the Python environment. For Gymnasium environments, id comes from env.spec.id when available and falls back to "UnknownEnv-v1". Spaces are parsed from observation_space and action_space for single environments, or from single_observation_space and single_action_space for vector environments.
Attribute |
Type |
Meaning |
|---|---|---|
|
|
Environment id reported by the wrapped environment. |
|
|
Alias returning the same contract shape. |
|
|
Alias returning the same contract shape. |
|
|
Configured render mode, or |
|
|
Number of environment instances served by the endpoint. |
|
|
Optional environment metadata. |
|
|
Native spec for one observation. |
|
|
Native spec for one action. |
to_dict() returns a serializable dictionary containing the same fields, with nested space specs converted to dictionaries.
contract = env.env_contract # from a server or a remote client
print(contract.id, contract.num_envs)
print(contract.observation_space.kind)
payload = contract.to_dict() # JSON-serializable, nested specs included
SpaceSpec¶
- class rlmesh.specs.SpaceSpec¶
Immutable native description of a space.
SpaceSpec is the wire-safe description of a space. A Space wrapper is the Python object that can sample, seed, and validate values from that spec.
Attribute or method |
Type |
Meaning |
|---|---|---|
|
|
Space family such as |
|
|
Tensor-like shape for spaces where shape is meaningful. |
|
|
Element dtype reported by the native space spec. |
|
|
Native detail payload for kind-specific fields. |
|
|
Dictionary form used by higher-level wrappers. |
|
|
Native sampler and validator for the spec. |
|
|
Best-effort conversion to a Gymnasium space. |
Use rlmesh.spaces.space_from_spec(spec) for a Python wrapper around a SpaceSpec. Use rlmesh.spaces.to_gymnasium_space(spec) when code expects a Gymnasium space object.
ServeOptions¶
- class rlmesh.ServeOptions¶
Native options controlling endpoint lifecycle behavior.
Constructor argument |
Type |
Meaning |
|---|---|---|
|
|
Whether clients may request endpoint shutdown. |
|
|
Optional idle timeout before the server exits. |
|
|
Optional grace period for in-flight work during shutdown. |
|
|
Optional timeout for closing the wrapped environment. |
|
|
Optional auth token for the served endpoint. |
The constructor is keyword-only. Pass the result to EnvServer(..., options=options) or to a model-serving API that accepts serve options.
options = rlmesh.ServeOptions(
allow_remote_shutdown=True,
idle_timeout_seconds=300.0,
)
server = rlmesh.EnvServer(env, "127.0.0.1:5555", options=options)
Tensor¶
- class rlmesh.Tensor¶
Native tensor value used at the dependency-free RLMesh value boundary.
Tensor is a validated transport container: immutable element bytes plus shape, dtype, and stride metadata, with DLPack and buffer-protocol edges. It is not an ndarray. Compute, slicing, and broadcasting belong to the frameworks. The NumPy, Torch, and JAX backends convert tensor leaves to backend arrays or tensors.
Attribute or method |
Type |
Meaning |
|---|---|---|
|
|
Tensor dimensions. |
|
|
Element dtype name (for example |
|
|
Number of dimensions. |
|
|
Number of elements. |
|
|
Element data size in bytes. |
|
|
Byte strides per dimension, C order. |
|
|
Device holding the data; currently always |
|
|
Whether elements are laid out C-contiguously. |
|
|
Same elements, new shape; zero-copy view when contiguous. |
|
|
Deep copy backed by fresh storage. |
|
|
Read-only N-D typed memory view over the elements. |
|
|
Copy the element bytes (C order) into Python bytes. |
|
capsule |
DLPack export; negotiates legacy or v1.0 capsules. |
|
|
DLPack device tuple, |
|
|
Static method; imports (and copies) from any DLPack producer. |
Use rlmesh.numpy.asarray(tensor) to get a writable NumPy copy of an RLMesh tensor (numpy.from_dlpack(tensor) for a zero-copy, read-only view), rlmesh.torch.as_tensor(tensor) to view or copy it as a Torch tensor, and rlmesh.jax.asarray(tensor) to import it as a JAX array.