How to Implement Your Callback with OptunaHub

This recipe shows how to implement and register your own callback with OptunaHub.

Callbacks are used when you want to insert custom processing after each trial completes. Typical use cases include:

  • Uploading the current best result to an external server (e.g., W&B, MLflow).

  • Sending a notification when a new best value is found.

  • Stopping the study early based on custom criteria by calling stop().

A callback is simply a callable with the following signature:

def callback(study: optuna.Study, trial: optuna.trial.FrozenTrial) -> None:
    ...

Optuna calls each registered callback once per trial, after the objective function returns and the trial state has been recorded. At that point Study already reflects the updated best value / best params, and trial is a FrozenTrial (immutable).

The simplest way to implement a callback is to define a plain function:

from __future__ import annotations

import optuna


def my_callback(study: optuna.Study, trial: optuna.trial.FrozenTrial) -> None:
    print(f"Trial {trial.number} finished.")
    print(f"  params     : {trial.params}")
    print(f"  value      : {trial.value}")
    if trial.state == optuna.trial.TrialState.COMPLETE:
        print(f"  best_value : {study.best_value}")

If your callback needs to hold internal state (e.g., a connection to an external service), you can implement it as a class with a __call__ method instead.

class MyCallback:
    """A callback that prints trial information after each completed trial.

    Args:
        verbose: If ``True``, also print the full ``params`` dict.
    """

    def __init__(self, verbose: bool = True) -> None:
        self._verbose = verbose

    def __call__(self, study: optuna.Study, trial: optuna.trial.FrozenTrial) -> None:
        # This method is called after every trial regardless of its state.
        if trial.state != optuna.trial.TrialState.COMPLETE:
            return

        print(f"Trial {trial.number} finished.")
        if self._verbose:
            print(f"  params     : {trial.params}")
        print(f"  value      : {trial.value}")
        print(f"  best_value : {study.best_value}")

The callback is passed to optimize() via the callbacks argument. Multiple callbacks can be specified as a list; they are called in order after each trial.

def objective(trial: optuna.trial.Trial) -> float:
    x = trial.suggest_float("x", -10, 10)
    y = trial.suggest_int("y", -5, 5)
    return x**2 + y**2

Run the study with a plain function callback.

study = optuna.create_study()
study.optimize(objective, n_trials=5, callbacks=[my_callback])
Trial 0 finished.
  params     : {'x': -7.564764879909534, 'y': -2}
  value      : 61.225667688312704
  best_value : 61.225667688312704
Trial 1 finished.
  params     : {'x': 8.69973140913433, 'y': -5}
  value      : 100.6853265910784
  best_value : 61.225667688312704
Trial 2 finished.
  params     : {'x': -9.967339061081564, 'y': 3}
  value      : 108.34784795856231
  best_value : 61.225667688312704
Trial 3 finished.
  params     : {'x': 2.4555606013596947, 'y': 5}
  value      : 31.029777866949985
  best_value : 31.029777866949985
Trial 4 finished.
  params     : {'x': 4.958054421841862, 'y': -2}
  value      : 28.582303649945636
  best_value : 28.582303649945636

Run another study with the class-based callback.

study = optuna.create_study()
study.optimize(objective, n_trials=5, callbacks=[MyCallback(verbose=True)])
Trial 0 finished.
  params     : {'x': -5.123900495302283, 'y': 4}
  value      : 42.25435628575897
  best_value : 42.25435628575897
Trial 1 finished.
  params     : {'x': -1.8372672516003323, 'y': 3}
  value      : 12.375550953803039
  best_value : 12.375550953803039
Trial 2 finished.
  params     : {'x': -3.6102169929372945, 'y': -3}
  value      : 22.033666736093203
  best_value : 12.375550953803039
Trial 3 finished.
  params     : {'x': -0.25100413776547725, 'y': 4}
  value      : 16.06300307717539
  best_value : 12.375550953803039
Trial 4 finished.
  params     : {'x': -4.182610242536757, 'y': 3}
  value      : 26.49422844097339
  best_value : 12.375550953803039

After implementing your own callback, you can register it with OptunaHub. See How to Register Your Package with OptunaHub for how to register your callback with OptunaHub. The category name to use when placing your package in the registry is callbacks:

Total running time of the script: (0 minutes 0.009 seconds)

Gallery generated by Sphinx-Gallery