sekai · 世界
A modern, flexible reinforcement learning environment framework
Built for high-dimensional, complex environments, especially financial markets
Documentation · Environments · Migrating from gymnasium · Changelog
sekai is a Python framework for reinforcement learning environments, designed as a modern
successor to gym / gymnasium.
It keeps the API you know (make, reset, step, spaces, wrappers, vector envs) and fixes
what gets painful at scale: structured return types instead of tuples, async-native
environments, built-in multi-agent support, typed episode statistics, and rendering that
lives outside the environment. It ships with gymnasium-identical classic control benchmarks,
a suite of financial-market environments and classic strategy games (chess, go, xiangqi,
poker, blackjack), and it plugs straight into the gymnasium ecosystem (Stable-Baselines3,
CleanRL, RLlib).
import sekai
env = sekai.make("CartPole-v1")
result = env.reset(seed=42)
while not result.done:
result = env.step(env.action_space.sample())
# result.observation, result.reward, result.terminated, result.truncated, result.info
Highlights
- Structured results.
step()returns a frozenStepResultdataclass, not a 5-tuple you can unpack in the wrong order. - Dynamic dimensionality.
MaskedBoxpadded sets andSequencespaces for observations whose size changes (a universe that gains and loses assets, the trades since the last decision), plus a standard action-mask convention thatcheck_env, wrappers and MaskablePPO understand. This is useful for applications such as financial markets, where the universe of tradeable instruments is constantly changing. - Time-aware. Frame stacking and normalisation over any (nested) space, exponentially weighted statistics for non-stationary data, observation and order latency wrappers, and
timestamp/dtin every financial markets env's info. - Fast vectorisation. Native vector environments that step thousands of sub-envs with array operations (over a million env-steps per second), plus sync, asyncio and multiprocessing backends with shared-memory observations, autoreset, and worker processes that restart after a crash or a hang. Memory-mapped market data is shared by every worker instead of copied, and any environment can be snapshotted and restored.
- Batched and differentiable markets.
Market-v1is one implementation that runs as a single env, a native vector env, or a batch of torch tensors on a GPU whose rewards are differentiable in the actions, so policies can be trained by backpropagating through the market. Explicit execution timing (no fills at the close the agent just observed), hard constraints by projection (gross and per-name caps, dollar and factor neutrality), holding costs, factor risk models, and scenarios from your own generative models. - A financial markets toolkit. Point-in-time
MarketDatawith walk-forward splits and a look-ahead detector, market simulators (stochastic volatility, jumps, regimes, listings), cost and impact models, account rules, reward functions, performance metrics and a limit order book, which the built-in environments are assembled from, and yours can be too. - Built-in environments. Classic Control with dynamics bit-for-bit identical to gymnasium; trading, portfolio, dynamic-universe, market-making, optimal-execution and order-book environments (single- and multi-agent), with closed-form baselines.
- Strategy Games. Chess, go (9×9 to 19×19), xiangqi, heads-up no-limit hold'em and blackjack, with the encodings of AlphaZero: the player to act sees the game from its side, legal moves come as a mask, rewards come at the end for the player who just moved, and the board games clone cheaply for tree search. Each game comes with its rules engine (chess's is native, with no GPL dependency), its notation (PGN, SGF, WXF, hand histories), and an RGB and a text renderer.
- From backtest to paper and live trading.
LiveMarketEnvgives a trained policy the same observations and actions over a data feed and a broker you plug in; replaying history through it with the paper broker reproducesMarket-v1exactly. Hard risk limits (position, gross, net and turnover caps, a drawdown kill switch) sit between any policy and any broker. - Honest evaluation. Reproducible benchmark tasks with classical baselines, and the statistics that account for how many things were tried: probabilistic and deflated Sharpe ratios, minimum track-record length, and the probability of backtest overfitting (CSCV).
- Renderers for every environment, in NumPy alone. The financial markets ones draw each episode as a dashboard: prices with positions and trades, equity, allocations, order-book depth with your orders in the queue. They are built from charts (
sekai.rendering.Dashboard,sekai.finance.rendering) that render your own environments as well. - Async-native. Every environment has
async_step()/async_reset();AsyncVecEnvruns I/O-bound envs (live feeds, remote simulators) concurrently. - Ecosystem-compatible.
sekai.compatconverts to and from gymnasium (Stable-Baselines3, CleanRL, RLlib) and PettingZoo. - Production quality. Fully typed (
mypy --strict), tested on Python 3.10–3.14 on Linux and on macOS and Windows, withcheck_envand asekaiCLI for validating and benchmarking environments.
Installation
pip install sekai
| Extra | Adds |
|---|---|
pip install "sekai[gymnasium]" |
gymnasium interop (sekai.compat) |
pip install "sekai[pettingzoo]" |
PettingZoo interop for multi-agent envs |
pip install "sekai[pandas]" |
building MarketData from pandas DataFrames |
pip install "sekai[render]" |
saving frames as GIF / MP4 |
pip install "sekai[all]" |
all of the above |
pip install "sekai[torch]" |
the torch backend: batched environments on tensors, with autograd (not in all: you choose PyTorch's build) |
sekai itself needs only NumPy and cloudpickle; import sekai never imports PyTorch.
Quick start
Use a built-in environment
import sekai
env = sekai.RecordEpisodeStatistics(sekai.make("CartPole-v1"))
result = env.reset(seed=0)
while not result.done:
result = env.step(env.action_space.sample())
stats = result.info["episode"] # a typed EpisodeStats, not an untyped dict
print(stats.episode_return, stats.episode_length)
Write your own
import numpy as np
import sekai
class RandomWalkEnv(sekai.Env[np.ndarray, np.ndarray]):
def __init__(self):
self.observation_space = sekai.Box(-np.inf, np.inf, shape=(1,))
self.action_space = sekai.Box(-1.0, 1.0, shape=(1,))
def reset(self, *, seed=None, options=None):
self._set_rng(seed) # seeds self.rng; seed=None continues the current stream
self.position = np.zeros(1, dtype=np.float32)
return sekai.ResetResult(observation=self.position.copy(), info={})
def step(self, action):
self.position += action + self.rng.normal(0, 0.1, size=1).astype(np.float32)
reward = -float(abs(self.position[0]))
return sekai.StepResult(self.position.copy(), reward, False, False, info={})
assert sekai.check_env(RandomWalkEnv()) == [] # validates the API, spaces, seeding, async path
sekai.register("RandomWalk-v0", RandomWalkEnv, max_episode_steps=100)
env = sekai.make("RandomWalk-v0")
Wrap and vectorise
import sekai
vec = sekai.make_vec("Pendulum-v1", num_envs=8) # also "async" and "proc" backends
vec = sekai.VecNormalizeObservation(sekai.VecRecordEpisodeStatistics(vec))
batch = vec.reset(seed=0)
for _ in range(500):
batch = vec.step(vec.batch_action_space.sample())
# finished sub-envs reset automatically; see batch.infos[i]["final_observation"]
print(batch.observations.shape) # (8, 3)
Trade a changing universe
import numpy as np
import sekai
from sekai.finance import (
GBM, ListingProcess, MarketData, RecordPerformance, SpreadCost, SquareRootImpact,
)
# closes: (T, N) prices with NaN while a stock is not listed; volumes: (T, N) shares traded.
# Simulated here; pass your own history the same way.
rng = np.random.default_rng(0)
sim = ListingProcess(GBM(mu=0.05, sigma=rng.uniform(0.15, 0.45, 80))).simulate(rng, 2519)
closes, volumes = sim.prices, rng.lognormal(12, 1, sim.prices.shape)
dates = np.busday_offset("2015-01-01", np.arange(2520), roll="forward") # trading days
tickers = [f"STK{i:02d}" for i in range(80)]
data = MarketData(closes, timestamps=dates, assets=tickers, fields={"volume": volumes})
train, test = data.split(0.8, embargo=5, history=20) # chronological, no leakage
env = RecordPerformance(sekai.make(
"Market-v0", data=train, max_assets=50, lookback=20,
transaction_cost=SpreadCost(half_spread=5e-4) + SquareRootImpact(0.3),
))
result = env.reset(seed=0)
while not result.done:
mask = env.unwrapped.action_mask() # only listed assets can be traded
result = env.step(env.action_space.sample(mask=mask))
print(result.info["performance"].sharpe_ratio)
The observation is a padded set of at most 50 assets (MaskedBox) with a validity mask, so
it batches, normalises and feeds a set encoder or transformer directly.
Vectorise natively
vec = sekai.make_vec("Portfolio-v0", num_envs=1024) # a PortfolioVecEnv: one array op per step
batch = vec.reset(seed=0)
batch = vec.step(vec.batch_action_space.sample())
Train through the market
import torch
from sekai.envs.finance import BatchMarketEnv
from sekai.finance import Constraints, ScoresLeverage, SpreadCost, SquareRootImpact
env = BatchMarketEnv(
1024, data=train, max_assets=50, # orders fill at the next close by default
action=ScoresLeverage(Constraints(max_gross=2.0, max_position=0.05, dollar_neutral=True)),
transaction_cost=SpreadCost() + SquareRootImpact(0.3),
backend="torch", device="cuda", autoreset=False,
)
env.differentiable = True # rewards are differentiable in the actions
result, loss = env.reset(seed=0), 0.0
while not bool(result.dones.all()):
result = env.step(policy(result.observations)) # policy: your torch module
loss = loss - result.rewards.mean()
loss.backward() # the exact (pathwise) policy gradient
Train with Stable-Baselines3
import sekai
from sekai.compat import to_gymnasium
from stable_baselines3 import PPO
model = PPO("MultiInputPolicy", to_gymnasium(sekai.make("Portfolio-v0")))
model.learn(total_timesteps=10_000)
to_sb3_vec_env gives SB3 a sekai vector env, a native one included, in place of
make_vec_env's single envs:
from sekai.compat import to_sb3_vec_env
model = PPO("MultiInputPolicy", to_sb3_vec_env(sekai.make_vec("Portfolio-v0", num_envs=64)))
Environments
| ID | Observation | Action | Description |
|---|---|---|---|
CartPole-v1 |
Box(4) | Discrete(2) | Balance a pole on a cart |
MountainCar-v0 |
Box(2) | Discrete(3) | Build momentum to climb a hill |
MountainCarContinuous-v0 |
Box(2) | Box(1) | Mountain car with continuous force |
Pendulum-v1 |
Box(3) | Box(1) | Swing up and balance a pendulum |
Acrobot-v1 |
Box(6) | Discrete(3) | Swing a two-link arm above a line |
Trading-v0 |
Box(lookback + 1) | Discrete(3) | Go short / flat / long on one asset |
Portfolio-v0 |
Dict | Box(n + 1) | Allocate across correlated assets and cash |
Market-v0 |
Dict (MaskedBox) | Box(max_assets + 1) | Trade a universe that lists and delists, with features, costs and financing |
Market-v1 |
Dict (MaskedBox) | any ActionMap |
Market-v0 batched on NumPy or torch: execution timing, constraints, holding costs, risk models |
MarketMaking-v0 |
Box(2) | Box(2) | Quote bid/ask, manage inventory (Avellaneda–Stoikov) |
OptimalExecution-v0 |
Box(3) | Box(1) | Liquidate a large order (Almgren–Chriss) |
LimitOrderBook-v0 |
Dict (MaskedBox) | MultiDiscrete([6, 6, 3]) | Make markets in an event-driven limit order book |
Chess-v0 |
Box(119, 8, 8) | Discrete(4672) | Chess, self-play; ChessVsRandom-v0 against a random opponent |
Go9x9-v0, Go13x13-v0, Go19x19-v0 |
Box(17, N, N) | Discrete(N² + 1) | Go with area scoring, komi and superko, self-play |
Xiangqi-v0 |
Box(131, 10, 9) | Discrete(8100) | Chinese chess, self-play |
HeadsUpHoldem-v0 |
Box(125) | Discrete(7) | One hand of heads-up no-limit hold'em, self-play |
Blackjack-v0 |
Box(43) | Discrete(9) | A round at a casino table; the shoe carries over, so counting pays |
The classic control environments match gymnasium's dynamics exactly, verified step by step
in the test suite. The financial markets environments simulate markets by default, and
Trading-v0 and Portfolio-v0 also accept your own price history:
import numpy as np
import sekai
prices = np.cumprod(1 + np.random.default_rng(0).normal(0, 0.01, (1000, 4)), axis=0)
env = sekai.make("Portfolio-v0", prices=prices, episode_length=126) # random 6-month windows
Market-v0, the general-purpose financial markets environment, takes a MarketData history
with listings and delistings (or simulates a regime-switching market whose assets come and
go), per-asset features, market-wide series, any cost model and reward function, leverage
and financing. sekai.envs.finance.LimitOrderBookMarket is a multi-agent order-book market
in which traders act on their own clocks. The four classic financial markets environments
also have native vector implementations, which make_vec() uses automatically.
The microstructure environments come with their textbook solutions as baselines, e.g.
env.almgren_chriss_actions() and env.avellaneda_stoikov_action(). On
OptimalExecution-v0 the Almgren–Chriss schedule scores about -320 bps against -420 bps for
TWAP, so a learned policy has a clear bar to beat.
The strategy games are played from both sides by the caller, the way AlphaZero trains:
import sekai
env = sekai.make("Chess-v0")
result = env.reset(seed=0)
while not result.done: # each move is by the player to act, who sees the board from its side
result = env.step(env.action_space.sample(mask=result.info["legal_action_mask"]))
print(result.info["result"], result.info["termination"])
env.clone() branches the board games for tree search,
sekai.envs.games.OpponentWrapper plays one side with a fixed policy for single-agent
training, sekai.envs.games.MultiAgentGame gives each side its own agent, and
illegal_action="lose" makes an illegal action lose the game instead of raising, for
learners without action masks.
Run sekai list to see every registered environment, or browse the
environment catalogue.
Why sekai?
| gymnasium | sekai | |
|---|---|---|
| Return types | obs, rew, term, trunc, info = env.step(a) |
StepResult frozen dataclass with .done |
| Async environments | Not supported | async_step / async_reset on every env, AsyncVecEnv |
| Multi-agent | Delegated to PettingZoo | MultiAgentEnv with per-agent typed spaces, event-driven turns, PettingZoo bridge |
| Variable-size observations | Sequence / Graph spaces (not batchable) |
MaskedBox padded sets that batch and flatten, Sequence with padding |
| Vectorised simulators | vector_entry_point |
NativeVecEnv base class, native financial markets envs, shared-memory ProcVecEnv |
| Rendering | render() on Env, mode fixed at construction |
Separate Renderer objects: swap, record, or skip freely |
| Episode statistics | Untyped info["episode"]["r"] |
Typed EpisodeStats with pluggable trackers |
| Vector env naming | AsyncVectorEnv means multiprocessing |
SyncVecEnv, AsyncVecEnv (asyncio), ProcVecEnv (processes) |
| Registry plugins | Entry-point plugins | register_namespace("qc", loader) for lazy plugin namespaces |
| Env checking | Passive checker wraps every env | check_env() in your tests; zero overhead in training |
| Financial Markets | Not in scope | sekai.finance toolkit and seven financial markets environments |
| Strategy Games | Toy blackjack | Chess, go, xiangqi, hold'em and blackjack with AlphaZero encodings |
Coming from gymnasium? The migration guide
maps every API, and sekai.compat lets you mix both while you move over.
Command line
sekai list # registered environments
sekai info Portfolio-v0 # spec, spaces, wrapper stack
sekai check MyEnv-v0 --determinism # run check_env (non-zero exit on errors)
sekai bench Portfolio-v0 --num-envs 1024 # native vector env when available
sekai -m my_package.envs list # import a plugin package first
Contributing
Contributions are welcome. See CONTRIBUTING.md for the development setup, and the examples for runnable scripts, from a pure-NumPy agent that solves CartPole in under a second to financial markets baselines.
Citing
If you use sekai in research, please cite it using the metadata in CITATION.cff.
License
Apache 2.0. See LICENSE. The glyph atlases the renderers draw text with are under the SIL Open Font License (OFL.txt, and for the xiangqi characters), and the chess pieces are Colin M.L. Burnett's, under the 3-clause BSD licence (LICENSE-chess-pieces.txt). The Qian Capital name and logo, which the financial markets dashboards show, are trademarks of Qian Capital and covered by none of these licences: see TRADEMARKS for what they may be used for.
Metadata
Release files for sekai 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sekai-0.3.2.tar.gz | 1.4 MB | With provenance | Details for sekai-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sekai-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for sekai-0.3.2-py3-none-any.whl |
Total release size: 2.4 MB
Release files / sekai-0.3.2.tar.gz
| Download URL | sekai-0.3.2.tar.gz |
|---|---|
| Size | 1.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b8a55212e414ed4eb5026568a96e753733fea4eaef937094b8efd736e18bb476
|
|
BLAKE2b-256 checksum How to use checksums |
7730e3bd18caaaa65d9bebe27c6ab46f20879a6df182b6f16d621a1f06579d51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logRelease files / sekai-0.3.2-py3-none-any.whl
| Download URL | sekai-0.3.2-py3-none-any.whl |
|---|---|
| Size | 1.0 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da85019b19179c8c32c105342bfc004af4ebafe6737945c8585cd201efaa5cf3
|
|
BLAKE2b-256 checksum How to use checksums |
52b5aa41659f3c4b52fc2ea61f8305e3d929ea36dd75f99f13884d43c0ad8f5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logSecurity for sekai 0.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for sekai-0.3.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.