# How to save RLlib model as Onnx

**URL:** <https://discuss.ray.io/t/how-to-save-rllib-model-as-onnx/2298>\
**Category:** RLlib\
**Created:** [May 26, 2021, 1:47pm UTC](https://discuss.ray.io/t/how-to-save-rllib-model-as-onnx/2298 "2021-05-26T13:47:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![geekyneuro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/geekyneuro/32/964_2.png) [@geekyneuro](https://discuss.ray.io/u/geekyneuro)\
**Post date:** [May 26, 2021, 1:47pm UTC](https://discuss.ray.io/t/how-to-save-rllib-model-as-onnx/2298/1 "2021-05-26T13:47:36Z")

</div>

After training the model with tune, how to save it as ONNX model ?

---

<div class="post-metadata">

**Author:** ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)\
**Post date:** [May 27, 2021, 6:43am UTC](https://discuss.ray.io/t/how-to-save-rllib-model-as-onnx/2298/2 "2021-05-27T06:43:29Z")

</div>

Hey @geekyneuro , we are actually working on an example script for this right now.

In general, you can get to the model after tune training like so:

```auto
results = tune.run(..., checkpoint_at_end=True)

best_checkpoint = results.get_best_checkpoint(
    results.trials[0], mode="max")
print(f".. best checkpoint was: {best_checkpoint}")

new_trainer = PPOTrainer(config=config)
new_trainer.restore(best_checkpoint)
policy = new_trainer.get_policy([policy ID or empty for "default_policy (single agent)"])
model = policy.model

# For torch, `model` is now a torch.nn.Module; for tf, try `model.base_model` to get the tf.keras.Model.

```

---

<div class="post-metadata">

**Author:** ![geekyneuro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/geekyneuro/32/964_2.png) [@geekyneuro](https://discuss.ray.io/u/geekyneuro)\
**Post date:** [May 27, 2021, 5:35pm UTC](https://discuss.ray.io/t/how-to-save-rllib-model-as-onnx/2298/3 "2021-05-27T17:35:19Z")

</div>

Thanks for sharing the sample code. Once the access to model object has been established, the dummy input is required to save it as onnx. How to figure out the dummy input ? Would the dummy input be same as the output of step function from the env ? Or would it be in some pre-processed format ?

Sincerely

Armando

---

<div class="post-metadata">

**Author:** ![RETELLIGENCE-IWEN](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/retelligence-iwen/32/8157_2.png) [@RETELLIGENCE-IWEN](https://discuss.ray.io/u/RETELLIGENCE-IWEN)\
**Post date:** [August 12, 2025, 1:59pm UTC](https://discuss.ray.io/t/how-to-save-rllib-model-as-onnx/2298/4 "2025-08-12T13:59:22Z")

</div>

I know this thread is probably too old, however for anyone looking for such answer \>\>

dummy input should be in size of observation space.

here is an example.

```auto
observation_size = model.observation_space.shape
dummy_input = torch.randn(1, *observation_size)

```
