Stable Baselines3: Reliable Reinforcement Learning in PyTorch

Jun 15, 2025

Introduction

Reinforcement learning (RL) often feels like a black box, where small changes in hyperparameters or implementation details lead to wildly different results. For developers and researchers, the struggle is not just in designing the agent, but in ensuring the implementation is reliable and reproducible. Stable Baselines3 is a set of reliable implementations of reinforcement learning algorithms based on PyTorch, providing a standardized way to train agents without getting bogged down in the minutiae of algorithm internals. With its high code coverage and benchmarked performance, it transforms RL from an experimental art into a predictable engineering discipline.

What Is Stable Baselines3?

Stable Baselines3 is a PyTorch-based library that provides reliable, benchmarked implementations of popular reinforcement learning algorithms. It is the successor to the original Stable Baselines (which was based on TensorFlow) and is designed to be the “scikit-learn of reinforcement learning.”

Maintained by a dedicated community of researchers and developers, the library is licensed under the MIT License, making it highly accessible for both academic research and industrial applications. It focuses on providing a consistent API across different algorithms, allowing users to swap one RL method for another with minimal code changes.

Why Stable Baselines3 Matters

In the early days of RL, most researchers implemented their own versions of algorithms like PPO or DQN from scratch. This led to a reproducibility crisis where results reported in papers were often impossible to replicate because of undocumented implementation details. Stable Baselines3 solves this by providing a “gold standard” implementation that has been rigorously tested against reference codebases.

The library’s significance lies in its commitment to stability. By maintaining 95% code coverage and strict PEP8 compliance, it ensures that the tools used for benchmarking new ideas are not flawed. This allows developers to focus on their environment design and reward functions rather than debugging the gradient descent of a specific algorithm.

Furthermore, the integration with Gymnasium (formerly OpenAI Gym) makes it the industry standard for prototyping RL agents. Whether you are training a robot to walk or an AI to optimize a supply chain, Stable Baselines3 provides the reliable foundation needed to move from a conceptual model to a functional agent.

Key Features

  • Standardized RL Algorithms: Provides high-quality implementations of PPO (Proximal Policy Optimization), DQN (Deep Q-Network), A2C (Advantage Actor-Critic), SAC (Soft Actor-Critic), and TD3 (Twin Delayed DDPG).
  • Consistent API: Uses a scikit-learn-like syntax (.learn() and .predict()), making it incredibly intuitive for anyone familiar with traditional machine learning.
  • High Code Coverage: Boasts 95% automated unit test coverage, ensuring that the algorithms behave as expected and are free from silent bugs.
  • Gymnasium Integration: Fully compatible with Gymnasium environments, allowing users to access a vast library of pre-made environments or create their own custom ones.
  • TensorBoard Support: Built-in integration with TensorBoard for real-time monitoring of reward curves, episode lengths, and other critical training metrics.
  • Custom Policy Networks: Allows users to define their own neural network architectures (policies) to better suit the specific observation and action spaces of their problem.
  • Custom Callbacks: Provides a flexible callback system to implement custom logic during training, such as early stopping or saving the best model based on a specific metric.
  • Dict Observation Space Support: Supports complex observation spaces, including dictionaries, which is essential for agents that need to process multiple types of data (e.g., images and sensor readings).
  • Type Hints and PEP8: Written with strict adherence to Python type hints and PEP8 style, making the source code easy to read, maintain, and extend.
  • RL Baselines3 Zoo: A companion project that provides a collection of pre-trained agents and scripts for hyperparameter tuning and evaluation.

How Stable Baselines3 Compares

When choosing an RL library, developers typically choose between a high-level “stable” library and a low-level “flexible” library. Stable Baselines3 occupies the middle ground, providing reliability without sacrificing too much customization.

Feature Stable Baselines3 Ray RLlib CleanRL
Primary Goal Reliability & Ease of Use Scalability & Industrial Use Transparency & Learning
API Complexity Low (Sklearn-like) High (Complex Configs) Medium (Single-file)
Distributed Training Limited (SubprocVecEnv) Native (Ray Cluster) No
Code Coverage Very High (95%) Medium N/A (Single-file)
Learning Curve Gentle Steep Moderate

Stable Baselines3 is the ideal choice for researchers and developers who need a reliable baseline to compare their new algorithms against. If you are starting a project and want to get an agent running in minutes, SB3 is the best tool. However, if you are deploying an RL agent to a massive cluster of 100+ machines for industrial-scale training, Ray RLlib is a more appropriate choice due to its native distributed architecture.

CleanRL, on the other hand, is designed for those who want to see every single line of code in the algorithm implementation. While SB3 abstracts the complexity into classes and modules, CleanRL keeps everything in a single file. This makes CleanRL better for learning how PPO works internally, but SB3 is far superior for building actual applications where stability and maintenance are key.

Getting Started: Installation

Stable Baselines3 requires Python 3.10+ and PyTorch. Depending on your needs, there are several ways to install the library.

Stable Release

For most users, the stable release via pip is the recommended method. You can install the core library alone or with optional dependencies for TensorBoard, OpenCV, and Atari games.

pip install stable-baselines3[extra]

Bleeding-edge Version

If you need the latest features or bug fixes that haven’t been released to PyPI yet, you can install directly from the GitHub master branch.

pip install git+https://github.com/DLR-RM/stable-baselines3

Development Version

To contribute to the project, you should clone the repository and install it in editable mode.

git clone https://github.com/DLR-RM/stable-baselines3 && cd stable-baselines3
pip install -e .

Prerequisites: If you are using Windows, the maintainers recommend using Miniforge for easier package management. If you are training on Atari games, ensure you have ale-py installed.

How to Use Stable Baselines3

The workflow in Stable Baselines3 is designed to be as simple as possible. It follows a three-step process: create an environment, instantiate an algorithm, and train the agent.

First, you define your environment using Gymnasium. This environment provides the observation space (what the agent sees) and the action space (what the agent can do). The agent then interacts with this environment through a series of steps, receiving rewards for desired behaviors.

Once the environment is set up, you choose an RL algorithm from the library. For example, PPO is a general-purpose algorithm that is often the first choice for many users. You instantiate the PPO class, passing in the policy type (e.g., MlpPolicy for multi-layer perceptrons) and the environment.

Finally, you call the .learn() method. This starts the training process, where the agent interacts with the environment and updates its neural network weights based on the the rewards it receives. You can monitor this progress in real-time using TensorBoard.

Code Examples

The following examples demonstrate the core functionality of Stable Baselines3, from basic training to advanced model management.

Basic Training Example

This example shows how to train a PPO agent on the classic CartPole environment.

import gymnasium as gym
from stable_baselines3 import PPO

# Create environment
env = gym.make("CartPole-v1", render_mode="human")

# Instantiate the agent
model = PPO("MlpPolicy", env, verbose=1)

# Train the agent
model.learn(total_timesteps=10000)

# Save the model
model.save("ppo_cartpole")

Saving and Loading Models

Stable Baselines3 makes it easy to persist your trained agents so you can deploy them later without retraining.

from stable_baselines3 import PPO

# Load the trained agent
model = PPO.load("ppo_cartpole", env=env)

# Test the agent
obs, _ = env.reset()
for _ in range(1000):
    action, _states = model.predict(obs, deterministic=True)
    obs, reward, done, truncated, info = env.step(action)
    env.render()
    if done or truncated:
        obs, _ = env.reset()

Evaluating a Policy

You can use the built-in evaluation helper to get a precise measure of the agent’s performance.

from stable_baselines3.common.evaluation import evaluate_policy

# Evaluate the agent
mean_reward, std_reward = evaluate_policy(model, eval_env=env, n_eval_episodes=10)
print(f"Mean reward: {mean_reward:.2f} +/- {std_reward:.2f}")

Real-World Use Cases

Stable Baselines3 is used across a variety of domains where decision-making under uncertainty is required.

  • Robotics Control: Engineers use SB3 to train agents that can perform precise movements, such as grasping objects or walking, by training in simulation (e.g., PyBullet) and then transferring the policy to a real robot.
  • Financial Trading: Quant traders use RL to optimize portfolio management and trade execution. SB3’s support for dictionary observation spaces allows them to integrate multiple data streams (e.g., price history, sentiment analysis) into a single agent.
  • Industrial Process Optimization: Plant managers use RL to optimize the cooling systems of data centers or the temperature control of chemical reactors, reducing energy consumption while maintaining stability.
  • Game AI: Developers create sophisticated NPCs that can adapt to player behavior by training agents in custom Gymnasium environments that mirror the game’s logic.

Contributing to Stable Baselines3

The project is open-source and welcomes contributions from the community. Because the project focuses on stability, the maintainers have strict standards for new code.

If you want to contribute, you should first read the CONTRIBUTING.md guide. The general flow is to report bugs via GitHub Issues and submit Pull Requests for fixes or enhancements. For those interested in experimental features, the maintainers provide a separate repository called sb3-contrib, where niche or experimental algorithms (like RecurrentPPO) are hosted to keep the core library stable.

The project also adheres to a Code of Conduct to ensure a professional and an inclusive environment for all contributors.

Community and Support

Stable Baselines3 has a large and active community of RL practitioners. Support is available through several official channels.

The primary source of truth is the official documentation site, which is hosted on Read the Docs. This site provides comprehensive guides, examples, and a detailed API reference.

For real-time discussion and troubleshooting, the project maintainers encourage users to use GitHub Discussions. GitHub Issues are reserved for bug reports and feature requests. The RL Baselines3 Zoo is another critical community resource, providing a pre-trained agents and a set of scripts for hyperparameter tuning using Optuna.

Conclusion

Stable Baselines3 is the essential foundation for anyone serious about reinforcement learning. By prioritizing reliability and a consistent API, it removes the technical debt associated with algorithm implementation and allows developers to focus on the actual problem they are solving.

Whether you are a researcher benchmarking a new approach or an engineer building a production-ready AI agent, SB3 provides the tools needed to ensure your results are reproducible and your agents are stable. While it may not be the most scalable for massive distributed training, its ease of use and high quality make it the first choice for most RL tasks.

Star the repo, try the quickstart, and join the community to start building reliable RL agents today.

What is Stable Baselines3 and what problem does it solve?

Stable Baselines3 is a PyTorch-based library that provides reliable, benchmarked implementations of reinforcement learning algorithms. It solves the reproducibility crisis in RL by providing a standardized, high-quality implementation of algorithms like PPO and DQN, ensuring that developers don’t have to implement them from scratch.

How do I install Stable Baselines3?

The easiest way to install Stable Baselines3 is via pip using the command pip install stable-baselines3[extra]. This includes optional dependencies like TensorBoard and OpenCV for monitoring and rendering.

How does Stable Baselines3 compare to Ray RLlib?

Stable Baselines3 focuses on reliability, ease of use, and a gentle learning curve, making it ideal for prototyping and research. Ray RLlib is designed for industrial-scale distributed training across many machines, which has a much steeper learning curve and more complex configuration.

Can I use Stable Baselines3 for custom environments?

Stable Baselines3 is fully compatible with Gymnasium (formerly OpenAI Gym) and allows you to create your own custom environments that follow the Gymnasium API, which the agent can then interact with.

What are the main algorithms implemented in Stable Baselines3?

The main algorithms include PPO (Proximal Policy Optimization), DQN (Deep Q-Network), and A2C (Advantage Actor-Critic) for discrete action spaces, and SAC (Soft Actor-Critic) and TD3 (Twin Delayed DDPG) for continuous action spaces.

Is Stable Baselines3 open source?

Yes, Stable Baselines3 is licensed under the MIT License, allowing for free use, modification, and distribution in both academic and research and industrial settings.

Can I use Stable Baselines3 for multi-agent RL?

Stable Baselines3 is primarily designed for single-agent RL. For multi-agent reinforcement learning (MARL), alternatives like Ray RLlib or PettingZoo are more suitable.