Agent-R1
Agent-R1 copied to clipboard
Validation crashes with file logger because NumPy scalars are not JSON serializable`
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
- Enable validation.
- Include
"file"intrainer.logger, for example:
trainer.logger: ["console", "tensorboard", "file"]
- Start training so that
_validate()runs before training or attest_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 asnp.mean(...)- this returns NumPy scalar types
verl.utils.tracking.Tracking.log()uses plainjson.dumps(data)- plain
json.dumpsdoes not handle NumPy scalar types
So the combination of:
- validation metrics from
process_validation_metrics trainer.loggerincluding"file"
causes a crash.