code: def validate_coerce_fig_to_dict(fig, validate):
      from plotly.basedatatypes import BaseFigure
  
      if isinstance(fig, BaseFigure):
          fig_dict = fig.to_dict()
      elif isinstance(fig, dict):
          if validate:
              # This will raise an exception if fig is not a valid plotly figure
              fig_dict = plotly.graph_objs.Figure(fig).to_plotly_json()
          else:
              fig_dict = fig
      elif hasattr(fig, "to_plotly_json"):
          fig_dict = fig.to_plotly_json()
      else:
          raise ValueError(
              """
  The fig parameter must be a dict or Figure.
      Received value of type {typ}: {v}""".format(
                  typ=type(fig), v=fig
              )
          )
      return fig_dict
  
imports: import plotly
  import plotly.graph_objs
  from plotly.offline import get_plotlyjs_version
label_desc: Use image node_id 1 for calling a global function with example usage: validate_coerce_fig_to_dict(fig, validate) and returns: fig_dict