# Info dict keys and then add them as new entries of SampleBatch

**URL:** <https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249>\
**Category:** RLlib\
**Created:** [November 25, 2021, 4:20pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249 "2021-11-25T16:20:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [November 25, 2021, 4:20pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249/1 "2021-11-25T16:20:33Z")

</div>

Hey folks,

4th component returned from env is the info dict. Let’s assume for simplictiy that the env returns an info dict of the following form: `{"a": ..., "b": ...}`. In the `postprocess_fn` of my policy I would like to use these data from the info dict to do some calculations. Via `sample_batch[SampleBatch.INFOS]["a"]` or `sample_batch[SampleBatch.INFOS]["b"]` one can access the data, but during the building process of the policy RLlib feeds some “dummy” batch with zero arrays to the `postprocess_fn` and `sample_batch[SampleBatch.INFOS]` is an array and not a dict.

Thus, can I disassemble the info dict entries and then store each as a new entry in SampleBatch (before call of postprocessing function)?

---

<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:** [November 25, 2021, 4:45pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249/2 "2021-11-25T16:45:32Z")

</div>

Hi @klausk55,

Could you just check the type of info and ignore it if it is not a dict?

```python
if not isinstance(info, dict):
    return
...

```

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [November 25, 2021, 5:07pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249/3 "2021-11-25T17:07:11Z")

</div>

Probably I could do it this way or find a similar workaround. But is there a way to add new keys to the SampleBatch and their values are the values from the info dict? Maybe there is a callback function which can do this (e.g. `on_episode_step`?)

---

<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:** [November 25, 2021, 5:48pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249/4 "2021-11-25T17:48:49Z")

</div>

@klausk55,

You could do this in on\_episode\_step or postprocess\_trajectory but it will not solve the issue you mentioned because those functions are not called during the initialization process. Which ever way you go you will need a special case to handle the initialization phase.

Another way that might work but I cannot look into it today is to set a ViewRequirement on a key in the info dictionary. I doubt it is set up to work like that currently but it would be worth a check.

---

<div class="post-metadata">

**Author:** ![Lars\_Simon\_Zehnder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lars_simon_zehnder/32/1185_2.png) [@Lars\_Simon\_Zehnder](https://discuss.ray.io/u/Lars_Simon_Zehnder)\
**Post date:** [November 26, 2021, 12:19pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249/5 "2021-11-26T12:19:07Z")

</div>

@klausk55 I found another topic here that deals with something similar and here is @sven1977 [answer](https://discuss.ray.io/t/accessing-info-dicts-in-postprocessing-callback/118/9):

> In the initial call to the loss function (during Policy setup), you should see an all-0s train\_batch being passed into the loss function (including all possible SampleBatch columns).  
> Then, if you access some column in your loss function, RLlib will detect this and provide that column in all subsequent calls.  
> So I just tried this:
> 
> - set breakpoint into PPOTorchPolicy’s loss function.
> - run the `rllib/agents/ppo/tests/test_ppo.py::test_ppo_compilation_and_lr_schedule` test case with `ray.init(local_mode=True)`
> - For the initial test call to the loss, I see “infos” in train\_batch properly initialized with 0s.
> - Then, if I access this column in the loss function to tell RLlib that “infos” are needed (e.g. by printing train\_batch[“infos”]), I do see this column also in all subsequent loss calls.

Following the @sven1977 's answer [here](https://discuss.ray.io/t/accessing-info-dicts-in-postprocessing-callback/118/6) the same should also hold for the `postprocess_trajectory()` function in your policy:

> You will get env infos automatically in your loss or postprocessing function (if these functions need this field, i.e. access it in a test pass).

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [November 26, 2021, 5:03pm UTC](https://discuss.ray.io/t/info-dict-keys-and-then-add-them-as-new-entries-of-samplebatch/4249/6 "2021-11-26T17:03:59Z")

</div>

> [@mannyv](#):
>
> @klausk55,
> 
> You could do this in on\_episode\_step or postprocess\_trajectory but it will not solve the issue you mentioned because those functions are not called during the initialization process. Which ever way you go you will need a special case to handle the initialization phase.
> 
> Another way that might work but I cannot look into it today is to set a ViewRequirement on a key in the info dictionary. I doubt it is set up to work like that currently but it would be worth a check.

@mannyv essentially the idea with a ViewRequirement is really good and I like it! But, as you said, currently it isn’t set up to work like that, at least that’s my opinion after I’d looked into it today 🙈

ViewRequirement has an argument `index` about which I’m not sure how this argument really works. I did some “dummy tests” to see the effect of this argument, but it seems to me that nothing had changed?!

I would say that `infos` dict values whose structure/content may vary from timestep to timestep probably make it difficult or even impossible to set a ViewRequirement on a key in the `infos` dict.

Since `on_postprocess_trajectory` isn’t affected by the initialization process of RLlib (thanks for this hint @mannyv), I guess one way I can go is to outsource my calculations regarding advantages and value targets to this callback function. Then accessing `postprocessed_batch["infos"]["actual_t"]` should be fine and I might recalculate advantages and value targets.
