Agent-R1 icon indicating copy to clipboard operation
Agent-R1 copied to clipboard

Validation crashes with file logger because NumPy scalars are not JSON serializable`

Open lqzxt opened this issue 7 months ago • 0 comments

Bug description

When validation metrics are logged with the file backend enabled, training crashes with:

TypeError: Object of type float32 is not JSON serializable

I hit this while running Agent-R1 on top of verl, but the underlying issue appears to be in verl's validation-metrics path.

Environment

  • verl==0.7.0
  • Python 3.12
  • CUDA 12.8
  • torch 2.8.0+cu128
  • vllm 0.11.0
  • transformers 4.56.1
  • 4x NVIDIA A800 80GB

Reproduction

  1. Enable validation.
  2. Include "file" in trainer.logger, for example:
trainer.logger: ["console", "tensorboard", "file"]
  1. Start training so that _validate() runs before training or at test_freq.

Actual behavior

Training crashes during logging with a stack trace like this:

TypeError: Object of type float32 is not JSON serializable

  File ".../verl/utils/tracking.py", line 250, in log
    self.fp.write(json.dumps(data) + "\n")
  File ".../agent_r1/ray_agent_trainer.py", line 631, in fit
    logger.log(data=val_metrics, step=self.global_steps)

Expected behavior

Validation metrics should be logged successfully, even if they are produced as NumPy scalar types such as np.float32 / np.float64.

Root cause

The issue seems to be:

  • verl.trainer.ppo.metric_utils.process_validation_metrics(...) uses NumPy reductions such as np.mean(...)
  • this returns NumPy scalar types
  • verl.utils.tracking.Tracking.log() uses plain json.dumps(data)
  • plain json.dumps does not handle NumPy scalar types

So the combination of:

  • validation metrics from process_validation_metrics
  • trainer.logger including "file"

causes a crash.

lqzxt avatar Apr 20 '26 13:04 lqzxt