# Add step info dictionary to MLflowLoggerCallback with Tune

**URL:** <https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740>\
**Category:** RLlib\
**Created:** [July 6, 2022, 11:25am UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740 "2022-07-06T11:25:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedetask](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/fedetask/32/2233_2.png) [@fedetask](https://discuss.ray.io/u/fedetask)\
**Post date:** [July 6, 2022, 11:25am UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/1 "2022-07-06T11:25:54Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- Medium: It contributes to significant difficulty to complete my task, but I can work around it.

I am using RLlib with ML Flow, by using the `MLflowLoggerCallback` and passing it to

```python
tune.run(
    **config,  
    callback=MLflowLoggerCallback(experiment_name="test")
)

```

However, this makes the callback log only a few metrics that are generated by RLlib.

In my environment, I return several metrics of interest in the `info` returned at each environment step. How can I have them logged by MLFlow?

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [July 6, 2022, 12:39pm UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/2 "2022-07-06T12:39:59Z")

</div>

Hi @fedetask,

You need to add a callback that adds your custom metrics.

Here is an explanation:

[https://docs.ray.io/en/latest/rllib/rllib-training.html#callbacks-and-custom-metrics](https://docs.ray.io/en/latest/rllib/rllib-training.html#callbacks-and-custom-metrics)

Here is an example:

> <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_metrics_and_callbacks.py>

* * *

> **[Manny is helping people do cool things with RL](https://www.buymeacoffee.com/mannyv)**
>
> Hey, so glad we got to connect. If you found my suggestions useful feel free to buy me a coffee!

---

<div class="post-metadata">

**Author:** ![fedetask](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/fedetask/32/2233_2.png) [@fedetask](https://discuss.ray.io/u/fedetask)\
**Post date:** [July 6, 2022, 1:12pm UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/3 "2022-07-06T13:12:54Z")

</div>

So I should have both a `CustomCallback` and the `MLflowLoggerCallback`, right? And the custom callback will add the info to the `custom_metrics`, so they should be passed to `tune.run()` in this order:

```python
tune.run(callbacks=[CustomCallback(), MLflowLoggerCallback()]) 

```

am I correct?

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [July 6, 2022, 1:25pm UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/4 "2022-07-06T13:25:48Z")

</div>

@fedetask,

I do not think that is quite how it should work, but I could be wrong. In my understanding, the `MLflowLoggerCallback` is a tune callback and the `CustomCallback` is an rllib callback. I think it should look something like this.

```auto
tune.run(config={..., "callbacks": CustomCallback,},
         callbacks=[MLflowLoggerCallback()])

```

---

<div class="post-metadata">

**Author:** ![avnishn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/avnishn/32/1544_2.png) [@avnishn](https://discuss.ray.io/u/avnishn)\
**Post date:** [July 6, 2022, 9:37pm UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/5 "2022-07-06T21:37:20Z")

</div>

@xwjiang2010 can you please chime in here

---

<div class="post-metadata">

**Author:** ![xwjiang2010](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xwjiang2010/32/1476_2.png) [@xwjiang2010](https://discuss.ray.io/u/xwjiang2010)\
**Post date:** [July 7, 2022, 3:55am UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/6 "2022-07-07T03:55:24Z")

</div>

@mannyv’s understanding of the callbacks is correct. @fedetask did you get a chance to try it?

---

<div class="post-metadata">

**Author:** ![fedetask](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/fedetask/32/2233_2.png) [@fedetask](https://discuss.ray.io/u/fedetask)\
**Post date:** [July 7, 2022, 8:50am UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/7 "2022-07-07T08:50:37Z")

</div>

Hello,  
Unfortunately, I am using Ray version 1.8.0; I cannot upgrade it for now.

I did as @mannyv described but things still don’t work. I think the reason is that in Ray 1.8.0, the `MLflowLoggerCallback` in [mlflow.py](https://github.com/ray-project/ray/blob/ray-1.8.0/python/ray/tune/integration/mlflow.py), lines 148-160, logs stuff as follows:

```python
148 def log_trial_result(self, iteration: int, trial: "Trial", result: Dict):
149 step = result.get(TIMESTEPS_TOTAL) or result[TRAINING_ITERATION]
150 run_id = self._trial_runs[trial]
151 for key, value in result.items():
152 try:
153 value = float(value)
154 except (ValueError, TypeError):
155 logger.debug("Cannot log key {} with value {} since the "
156 "value cannot be converted to float.".format(
157 key, value))
158 continue
159 self.client.log_metric(
160 run_id=run_id, key=key, value=value, step=step)

```

and for `key='custom_metrics'`, `value` is a dictionary that cannot be cast to `float` in line 153 and therefore isn’t logged.

I solved it by creating a new class that extends `MLflowLoggerCallback` and overriding the `log_trial_result` to allow for the `custom_metrics` dictionary to be logged.

---

<div class="post-metadata">

**Author:** ![olipinski](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/olipinski/32/2903_2.png) [@olipinski](https://discuss.ray.io/u/olipinski)\
**Post date:** [July 19, 2022, 8:55am UTC](https://discuss.ray.io/t/add-step-info-dictionary-to-mlflowloggercallback-with-tune/6740/8 "2022-07-19T08:55:47Z")

</div>

(post deleted by author)
