Core Exports¶
Note
This is the autodoc API reference. For authoring guides see Environments and Models.
The top-level rlmesh package re-exports the common entry points: environment serving and clients, model running, sandboxing, and the spaces, types, and adapters subpackages.
The top-level client and model classes are dependency-free wrappers around RLMesh-native values. Reach for them when you want native values and no framework dependency; use a backend module (Framework Backends) when you want tensor leaves decoded to NumPy arrays or Torch tensors.
Import |
Description |
|---|---|
|
Serve a Gymnasium-compatible environment endpoint (scalar or vector). |
|
Connect to one environment and preserve RLMesh-native values. |
|
Connect to a vector endpoint and preserve RLMesh-native values. |
|
Build an env image and own the container behind a single client. |
|
Build an env image and own the container behind a vector client. |
|
Wrap a Python prediction function as a native-value model. |
|
Connect to an already-served model and drive it against an env. |
|
Run a model policy in its own container (experimental). |
|
Native serve lifecycle options. |
|
Native tensor value used by dependency-free clients. |
|
Observation/action adapters and contract-based resolution. |
|
Space wrappers and Gymnasium conversion helpers. |
|
Structural protocols and value aliases. |
The detailed pages below describe the shared behavior:
Run and Evaluate¶
The eval surface binds a model to an environment. run() pumps whole episodes and returns a RunResult; session() returns a Session you drive by hand with reset / predict / step.
- rlmesh.run(model, env, *, seeds=None, max_episodes=None, max_episode_steps=None, max_episode_seconds=None, hooks=None, instruction=None, close_env=False, trust_entrypoints=None, execution_horizon=1, view=None)[source]¶
Drive
modelagainstenvto completion and return aRunResult.The auto-pump convenience over
rlmesh.session(): it creates the session internally and closes it (honoringclose_env) when the run ends – hold arlmesh.session()yourself to reuse one binding across runs. Works for a localModelor a servedRemoteModel/SandboxModel(a served model rejectsinstruction=; the env’s own instruction is used).max_episode_steps/max_episode_secondscap each episode (hitting a cap marks ittruncated), andhooks(rlmesh.RunHooks) observes the loop; all three pass through toSession.run.- Parameters:
- Return type:
- rlmesh.session(model: ModelBase[ObsT, ActT], env: LocalEnvTarget, *, instruction: str | None = None, close_env: bool = False, trust_entrypoints: bool | None = None, execution_horizon: int = 1, view: ViewArg = None) Session[ObsT, ActT][source]¶
- rlmesh.session(model: type[Any], env: EnvTarget, *, instruction: str | None = None, close_env: bool = False, trust_entrypoints: bool | None = None, execution_horizon: int = 1, view: ViewArg = None) Session[Any, Any]
- rlmesh.session(model: Callable[[ObsT], ActT], env: LocalEnvTarget, *, instruction: str | None = None, close_env: bool = False, trust_entrypoints: bool | None = None, execution_horizon: int = 1, view: ViewArg = None) Session[ObsT, ActT]
- rlmesh.session(model: object, env: EnvTarget, *, instruction: str | None = None, close_env: bool = False, trust_entrypoints: bool | None = None, execution_horizon: int = 1, view: ViewArg = None) Session[Any, Any]
Bind a model to an env and return a
Sessionto drive by hand or via run().modelis a localModel(instance, subclass class, or a bare predict callable) or a served handle (RemoteModel/SandboxModel);envis a local env, anEnvFactory, a remote-env handle, or an address string. A spec’d model resolves its adapter from the env’s tags – a local env must carry them (viarlmesh.adapters.tag()or anEnvFactory). A served model rejectsinstruction=(the env’s own instruction is used).The returned session is yours:
Session.run()leaves it open, so drive it (or re-run it) as often as you like, then close it viaclose()or thewithblock.Pass
rlmesh.RANDOM_SAMPLEasmodelfor a random baseline: each step samples the env’s action space, no spec or adapter involved.Typing: a
Modelinstance or an annotated predict callable flows its observation/action types onto the returnedSession(predict/stepare typed accordingly); a class source, duck-typed policy, or served handle yieldsSession[Any, Any].
- class rlmesh.Session[source]¶
Bases:
Generic[ObsT,ActT]A model bound to one env: drive it by hand, or pump whole episodes.
The neutral pair-driver returned by
rlmesh.session().reset/predict/stepdrive one step at a time –predictapplies the model’s adapter (resolved from the env’s published contract) around the model’s own predict, replaying an action chunk one action per step whenexecution_horizon> 1 and the model definespredict_chunk.runpumps whole episodes and returns a typedRunResult.The env connection is opened lazily on first
reset(manual driving);rundrives whole episodes through the same primitives and leaves the session open, so a caller-held session runs as often as you like. Close it yourself –close()or thewithblock; after that, any further use raises. (The one-shotrlmesh.run()/Model.runcreate their session internally and close it when the run ends.)- reset(*, seed=None)[source]¶
Begin a new episode: end the previous one, then reset the env and adapter.
Ending the previous episode fires the model’s on_episode_end (the local per-episode boundary), so a stateful model clears its state between episodes on the hand-driven path too, not only via run().
- Parameters:
seed (int | None)
- Return type:
tuple[ObsT, Mapping[str, Any]]
- predict(observation)[source]¶
Map one env observation to an env-ready action (the model’s adapter applied).
- Parameters:
observation (ObsT)
- Return type:
ActT
- step(action)[source]¶
Apply one action to the env; record reward and termination.
- Parameters:
action (ActT)
- Return type:
tuple[ObsT, float, bool, bool, Mapping[str, Any]]
- reader(*items)[source]¶
Build a read-only, role-addressed view over this env’s observations.
Each item is a role constant – kept in the env’s native encoding – or a model-input leaf declaring the encoding you want (
Image(IMAGE_PRIMARY, layout="hwc"),State(EEF_POS)). The returnedReadermaps a raw observation to{role: value}through the same adapter pipeline a model uses, so it is encoding-agnostic across envs and runs identically in the native core. Resolved once here, reused each step:read = sess.reader(Image(IMAGE_PRIMARY, layout="hwc"), EEF_POS) obs, _ = sess.reset() while not sess.done: screen.show(read(obs)[IMAGE_PRIMARY]) obs, *_ = sess.step(sess.predict(obs))
A bare role is desugared to the env-native leaf for that role (by the env’s own tag); pass an explicit leaf to override the encoding.
- Parameters:
items (object)
- Return type:
Reader
- read(observation, item)[source]¶
One-shot read of a single role from one observation.
The single-value convenience for
reader()–itemis a role constant or a model-input leaf. The reader is resolved once and cached per item, so calling this every step does not re-resolve:ee = sess.read(obs, EEF_POS) img = sess.read(obs, Image(IMAGE_PRIMARY, layout="hwc"))
The value is typed
Any(likeReader’s): its concrete shape is the leaf’s declared encoding, which the caller owns.- Parameters:
observation (object)
item (object)
- Return type:
Any
- observation_roles()[source]¶
The observation roles this session’s env declares, grouped by kind.
Connects if needed, reads the env contract’s published tags, and returns their
observation_roles. An env that publishes no tags yields empty groups – “none declared” is an answer, not an error.- Return type:
- run(*, seeds=None, max_episodes=None, max_episode_steps=None, max_episode_seconds=None, hooks=None)[source]¶
Drive whole episodes to completion and return a typed
RunResult.The single drive loop: pumps this session’s own
reset/predict/stepprimitives, soModel.runroutes through here.seedsgives a per-episode seed and sets the episode count unlessmax_episodesis given.max_episode_steps/max_episode_secondscap each episode – hitting a cap marks ittruncated, exactly like the built-in step bound (the wall-clock cap is checked at the top of the step loop).hooks(RunHooks) observes the loop; hook exceptions propagate and abort the run, andRunHooks.on_run_end()always fires exactly once with the completed episodes, even on an error or interrupt.Does not close the session: a caller-held session (from
rlmesh.session()/Model.session()) stays connected – viewer and hooks included – soruncan be called again or mixed with manual driving; close it viaclose()or thewithblock. (The one-shotrlmesh.run()/Model.run()own their internal session and close it for you.)
- close()[source]¶
Close this session: model close hook, served route (and owned source), env.
For a served model, closes the model client and shuts down a managed source it started (e.g. a
SandboxModelcontainer). For the env, shuts it down only on theclose_envopt-in and closes a connection this session dialed.Idempotent: the first call tears everything down (firing a local model’s
on_closeexactly once, whether the session was pumped viarunor driven by hand); later calls are no-ops, and any other use of a closed session raisesRuntimeError.- Return type:
None
- class rlmesh.RunResult[source]¶
Bases:
objectThe result of a
Model.run()eval.- property success_rate: float[source]¶
Fraction of episodes that succeeded.
Prefers the env-reported task outcome (Gymnasium
info["is_success"]/["success"], captured per episode inEpisodeResult.success). For an env that emits no such signal, falls back toterminatedfor that episode – so a time-limit env whose success is the truncation cap should report success viainforather than rely on this.
- __init__(episodes=())¶
- Parameters:
episodes (tuple[EpisodeResult, ...])
- Return type:
None
- class rlmesh.EpisodeResult[source]¶
Bases:
objectThe outcome of one evaluation episode.
- index¶
0-based episode index within the run.
- Type:
int
- seed¶
The seed the episode was reset with, or
None.- Type:
int | None
- steps¶
Number of env steps taken.
- Type:
int
- reward¶
Total reward accumulated over the episode.
- Type:
float
- terminated¶
Whether the env reported a terminal state.
- Type:
bool
- truncated¶
Whether the episode was cut short (env truncation, a
max_episode_steps/max_episode_secondscap, or the built-in step bound).- Type:
bool
- success¶
The env-reported task outcome from the final step’s
info(Gymnasium’sis_success/successkey), orNonewhen the env emits no such signal. Distinct fromterminated(which only says the episode reached a terminal state, not whether it succeeded).- Type:
bool | None
- duration_s¶
Wall time from reset-return to episode end, in seconds.
- Type:
float
- predict_ms¶
Mean per-step wall time of
predict, in milliseconds.- Type:
float
- step_ms¶
Mean per-step wall time of the env
stepround trip, in milliseconds.- Type:
float
- __init__(index, seed, steps, reward, terminated, truncated, success=None, duration_s=0.0, predict_ms=0.0, step_ms=0.0)¶
- Parameters:
index (int)
seed (int | None)
steps (int)
reward (float)
terminated (bool)
truncated (bool)
success (bool | None)
duration_s (float)
predict_ms (float)
step_ms (float)
- Return type:
None
- class rlmesh.RunHooks[source]¶
Bases:
objectObserver callbacks for
Session.run(); every default is a no-op.Subclass and override any subset, then pass an instance as
hooks=torlmesh.run(),Model.run, orSession.run(). Hook exceptions propagate and abort the run;on_run_end()still fires exactly once with the completed episodes.- on_episode_start(*, episode, seed)[source]¶
Called after each episode’s reset returns (no-op by default).
- Parameters:
episode (int) – 0-based episode index (equals
EpisodeResult.index).seed (int | None) – The seed the episode was reset with, or
None.
- Return type:
None
- on_step(event)[source]¶
Called after each env step with its
StepEvent(no-op by default).- Parameters:
event (StepEvent)
- Return type:
None
- on_episode_end(result)[source]¶
Called with each completed episode’s
EpisodeResult(no-op by default).- Parameters:
result (EpisodeResult)
- Return type:
None
- class rlmesh.StepEvent[source]¶
Bases:
objectOne env step of a
Session.run()eval, passed toRunHooks.on_step().- episode¶
0-based episode index (equals
EpisodeResult.index).- Type:
int
- seed¶
The episode’s reset seed, or
None.- Type:
int | None
- step¶
0-based step index within the episode.
- Type:
int
- observation¶
The raw observation the action was predicted from (pre-step).
- Type:
Any
- action¶
The env-ready action applied to the env.
- Type:
Any
- reward¶
The step’s reward.
- Type:
float
- terminated¶
Whether the env reported a terminal state on this step.
- Type:
bool
- truncated¶
Whether this step truncated the episode.
- Type:
bool
- info¶
The step’s
infomapping.- Type:
collections.abc.Mapping[str, Any]
- predict_ms¶
Raw wall time of this step’s
predict, in milliseconds; near zero on chunk-replay steps, where no model forward runs.- Type:
float
- step_ms¶
Raw wall time of the env
stepround trip, in milliseconds.- Type:
float
- read¶
Lazy role reader bound to
observation–event.read(item)delegates toSession.read(), so resolution is cached per item and never triggered unless called.- Type:
collections.abc.Callable[[object], object]
- __init__(episode, seed, step, observation, action, reward, terminated, truncated, info, predict_ms, step_ms, read)¶
- Parameters:
episode (int)
seed (int | None)
step (int)
observation (Any)
action (Any)
reward (float)
terminated (bool)
truncated (bool)
info (Mapping[str, Any])
predict_ms (float)
step_ms (float)
read (Callable[[object], object])
- Return type:
None
- class rlmesh.View[source]¶
Bases:
objectHow to show a live eval. Pass to
run/sessionasview=.The common cases are the string shorthands
"terminal"/"http"/"http:9000"/"both"; construct aViewdirectly only to tune.- backend¶
Where to draw –
"terminal"(in-place half-blocks),"http"(a local web page), or"both".- Type:
str
- port¶
HTTP port for the
"http"/"both"backends.- Type:
int
- fps¶
Target frame rate; frames produced faster are dropped.
- Type:
int
- source¶
Which source to show first by label (a render label or an image role);
Noneshows the first available.- Type:
str | None
- format¶
Encoding for HTTP frames,
"jpeg"or"png".- Type:
str
- quality¶
JPEG quality 1..100 (ignored for PNG).
- Type:
int
- __init__(backend='terminal', port=8008, fps=30, source=None, format='jpeg', quality=75)¶
- Parameters:
backend (str)
port (int)
fps (int)
source (str | None)
format (str)
quality (int)
- Return type:
None
- rlmesh.sanitize_metadata(info)[source]¶
Coerce an info/metadata mapping into what RLMesh metadata accepts.
Lossy by design: simulator handles and other rich objects (for example a
sapien.Pose) become theirstr()form rather than failing the serve path. Accepted values pass through unchanged:None,bool,int,float,str,bytes, and nested mappings/lists/tuples of them (tuples become lists, as in the codec). NumPy scalars/arrays and namedtuples are unwrapped the way the codec does (_asdict/tolist/item), enums resolve to theirvaluewith anamefallback, and non-string keys are stringified.- Parameters:
info (Mapping[str, Any]) – The mapping to sanitize (e.g. a Gymnasium
infodict).- Returns:
A plain
dictthe native metadata codec accepts verbatim.- Raises:
TypeError – If
infois not a mapping.ValueError – If
infocontains a reference cycle (the error names the key path).
- Return type:
dict[str, Any]
Sentinels¶
Two module-level sentinels change what run and session do:
rlmesh.NO_ADAPTER: pass as a model’sspecto explicitly skip adapter resolution; the model handles raw env observations and actions itself.rlmesh.RANDOM_SAMPLE: pass as the model torlmesh.run/rlmesh.sessionto sample the env’s action space each step, a random baseline with no spec or adapter involved.
Types¶
The rlmesh.types module defines the structural protocols that EnvServer accepts and the shared value aliases used by dependency-free clients. The protocols are structural, so any object with the right methods satisfies them; you do not subclass anything. Use them to type-annotate an environment or a value, or to check what EnvServer expects. For authoring an environment against these protocols see Environments.
Public structural protocols and shared value aliases.
- class rlmesh.types.EnvLike[source]¶
Bases:
Protocol[EnvObsT,EnvActT]Structural protocol for a single environment.
- property observation_space: SpaceLike[EnvObsT][source]¶
Space describing reset and step observations.
- __init__(*args, **kwargs)¶
- reset(*, seed=None, options=None)[source]¶
Reset the environment and return the initial observation.
- Parameters:
seed (int | None)
options (dict[str, Any] | None)
- Return type:
tuple[EnvObsT, dict[str, Any]]
- rlmesh.types.EnvTarget¶
What a served-model
session()(RemoteModel/SandboxModel) accepts as an env: everything inLocalEnvTarget, plus an env client exposing a publishedenv_contract(the contract is sent to the served model at bind).alias of
LocalEnvTarget|HasEnvContract
- class rlmesh.types.HasAddress[source]¶
Bases:
ProtocolAnything exposing a dialable endpoint
address(e.g. a server handle).- __init__(*args, **kwargs)¶
- class rlmesh.types.HasEnvContract[source]¶
Bases:
ProtocolAn env client exposing a published
env_contract(e.g. a served-env handle).- property env_contract: _EnvContract[source]¶
The env’s published
EnvContract.
- __init__(*args, **kwargs)¶
- rlmesh.types.InfoDict¶
User-owned diagnostic grab-bag returned by env
reset/step(the gymnasiuminfonorm).Values are typed
Anyon purpose: the env author owns what goes in, the SDK never validates it, so strict-mode consumers can writeinfo["success"] > 0.5without a cast. ContrastMetadata, which the SDK validates and therefore types asobject.alias of
dict[str,Any]
- rlmesh.types.LocalEnvTarget¶
What the local drive paths (
run()/session()/Model.run) accept as an env: a live env object, anEnvFactory, an object exposing anaddress, or an address string to dial. Matched structurally, exactly like the runtime’s own checks. A contract-only handle (env_contractwithoutreset/step) cannot be stepped locally, so it is deliberately absent.alias of
SupportsResetStep|SupportsMake|HasAddress|str
- rlmesh.types.Metadata¶
SDK-validated metadata mapping (e.g. adapter tags, describe envelopes).
Values are typed
objecton purpose: the SDK owns this surface and validates it, so consumers narrow explicitly instead of trusting anAny. ContrastInfoDict, the user-owned grab-bag.alias of
Mapping[str,object]
- class rlmesh.types.SpaceLike[source]¶
Bases:
Protocol[SpaceT]Structural protocol for RLMesh-compatible spaces.
- contains(value, /)[source]¶
Return whether
valuebelongs to the space.- Parameters:
value (Any)
- Return type:
bool
- seed(seed=None)[source]¶
Seed the space sampler.
- Parameters:
seed (int | None)
- Return type:
int | list[int] | dict[str, int] | None
- __init__(*args, **kwargs)¶
- rlmesh.types.SpecArg¶
a
ModelSpec,rlmesh.NO_ADAPTERto opt out on a tagged env, orNone.- Type:
A model’s adapter spec argument
alias of
_ModelSpec|_NoAdapter|None
- class rlmesh.types.SupportsMake[source]¶
Bases:
ProtocolAn env factory: anything with a
makecallable (e.g.EnvFactory).- __init__(*args, **kwargs)¶
- class rlmesh.types.SupportsResetStep[source]¶
Bases:
ProtocolA live env object: anything with
resetandstepcallables.- __init__(*args, **kwargs)¶
- class rlmesh.types.VectorEnvLike[source]¶
Bases:
Protocol[BatchActionT,VectorObsT,VectorActT]Structural protocol for vectorized environments.
- __init__(*args, **kwargs)¶
- property single_observation_space: SpaceLike[VectorObsT][source]¶
Observation space for one environment in the vector.
- property single_action_space: SpaceLike[VectorActT][source]¶
Action space for one environment in the vector.
- reset(*, seed=None, options=None)[source]¶
Reset all environments and return batched observations.
- Parameters:
seed (int | None)
options (dict[str, Any] | None)
- Return type:
tuple[object, dict[str, Any]]