# RuntimeError: Trainer callback hook 'on_log' failed with AttributeError: 'NoneType' object has no attribute 'step'

- **ID:** `huggingface/trainer-callback-hook-failure`
- **Domain:** huggingface
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A custom callback's on_log method attempts to access trainer.state.global_step before the trainer state is fully initialized, typically when logging occurs during the first step.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| transformers>=4.35.0 | active | — | — |

## Workarounds

1. **Add a guard in the callback to check if trainer.state is initialized: `if trainer.state.global_step is not None:` before accessing step.** (95% success)
   ```
   Add a guard in the callback to check if trainer.state is initialized: `if trainer.state.global_step is not None:` before accessing step.
   ```
2. **Override the on_step_begin callback instead of on_log, as state is guaranteed to be initialized at step start.** (85% success)
   ```
   Override the on_step_begin callback instead of on_log, as state is guaranteed to be initialized at step start.
   ```
3. **Initialize the callback with a default step value: `self.step = 0` in __init__, then use self.step instead of trainer.state.global_step.** (90% success)
   ```
   Initialize the callback with a default step value: `self.step = 0` in __init__, then use self.step instead of trainer.state.global_step.
   ```

## Dead Ends

- **** — The root cause is accessing state before initialization; moving to another callback may still encounter the same issue if state is not ready. (70% fail)
- **** — This only postpones the error; it will still occur at the first log event regardless of step count. (90% fail)
- **** — The error is raised by the Trainer's callback execution wrapper; catching it inside the callback does not prevent the Trainer from propagating it. (85% fail)
