code: def _save_or_update_trials(
      experiment: Experiment,
      trials: List[BaseTrial],
      encoder: Encoder,
      decoder: Decoder,
      batch_size: Optional[int] = None,
      reduce_state_generator_runs: bool = False,
  ) -> None:
      """Add new trials to the experiment, or update if they already exist.
  
      Note that new data objects (whether attached to existing or new trials)
      will also be added to the experiment, but existing data objects in the
      database will *not* be updated or removed.
      """
      experiment_id = experiment._db_id
      if experiment_id is None:
          raise ValueError("Must save experiment first.")
  
      # pyre-fixme[53]: Captured variable `experiment_id` is not annotated.
      # pyre-fixme[3]: Return type must be annotated.
      def add_experiment_id(sqa: Union[SQATrial, SQAData]):
          sqa.experiment_id = experiment_id
  
      if reduce_state_generator_runs:
          latest_trial = trials[-1]
          trials_to_reduce_state = trials[0:-1]
  
          # pyre-fixme[3]: Return type must be annotated.
          def trial_to_reduced_state_sqa_encoder(t: BaseTrial):
              return encoder.trial_to_sqa(
                  t, generator_run_reduced_state=True
              )
  
          _bulk_merge_into_session(
              objs=trials_to_reduce_state,
              encode_func=trial_to_reduced_state_sqa_encoder,
              decode_func=decoder.trial_from_sqa,
              decode_args_list=[
                  {"experiment": experiment} for _ in range(len(trials))
              ],
              modify_sqa=add_experiment_id,
              batch_size=batch_size,
          )
  
          _bulk_merge_into_session(
              objs=[latest_trial],
              encode_func=encoder.trial_to_sqa,
              decode_func=decoder.trial_from_sqa,
              decode_args_list=[
                  {"experiment": experiment} for _ in range(len(trials))
              ],
              modify_sqa=add_experiment_id,
              batch_size=batch_size,
          )
      else:
          _bulk_merge_into_session(
              objs=trials,
              encode_func=encoder.trial_to_sqa,
              decode_func=decoder.trial_from_sqa,
              decode_args_list=[
                  {"experiment": experiment} for _ in range(len(trials))
              ],
              modify_sqa=add_experiment_id,
              batch_size=batch_size,
          )
  
      datas = []
      data_encode_args = []
      for trial in trials:
          trial_datas = experiment.data_by_trial.get(trial.index, {})
          for ts, data in trial_datas.items():
              if data.db_id is None:
                  # Only need to worry about new data, since it's not really possible
                  # or supported to modify or remove existing data.
                  datas.append(data)
                  data_encode_args.append(
                      {"trial_index": trial.index, "timestamp": ts}
                  )
  
      _bulk_merge_into_session(
          objs=datas,
          encode_func=encoder.data_to_sqa,
          decode_func=decoder.data_from_sqa,
          encode_args_list=data_encode_args,
          decode_args_list=[
              {"data_constructor": experiment.default_data_constructor}
              for _ in range(len(datas))
          ],
          modify_sqa=add_experiment_id,
          batch_size=batch_size,
      )
  
imports: import os
  from logging import Logger
  from typing import Any, Callable, Dict, List, Optional, Sequence, Union
  from ax.core.base_trial import BaseTrial
  from ax.core.experiment import Experiment
  from ax.core.generator_run import GeneratorRun
  from ax.core.metric import Metric
  from ax.core.outcome_constraint import ObjectiveThreshold, OutcomeConstraint
  from ax.core.runner import Runner
  from ax.exceptions.core import UserInputError
  from ax.exceptions.storage import SQADecodeError
  from ax.modelbridge.generation_strategy import GenerationStrategy
  from ax.storage.sqa_store.db import session_scope, SQABase
  from ax.storage.sqa_store.decoder import Decoder
  from ax.storage.sqa_store.encoder import Encoder
  from ax.storage.sqa_store.sqa_classes import SQAData, SQAGeneratorRun, SQAMetric, SQARunner, SQATrial
  from ax.storage.sqa_store.sqa_config import SQAConfig
  from ax.storage.sqa_store.utils import copy_db_ids
  from ax.utils.common.base import Base
  from ax.utils.common.logger import get_logger
  from ax.utils.common.typeutils import checked_cast, not_none
label_desc: Use image node_id 8 for calling a global function with example usage: _save_or_update_trials(experiment, trials, encoder, decoder, batch_size, reduce_state_generator_runs) and returns: encoder