code: def proto_encode_to_dict(
      pb_obj: Union["tpb.TelemetryRecord", "pb.MetricRecord"]
  ) -> Dict[int, Any]:
      data: Dict[int, Any] = dict()
      fields = pb_obj.ListFields()
      for desc, value in fields:
          if desc.name.startswith("_"):
              continue
          if desc.type == desc.TYPE_STRING:
              data[desc.number] = value
          elif desc.type == desc.TYPE_INT32:
              data[desc.number] = value
          elif desc.type == desc.TYPE_ENUM:
              data[desc.number] = value
          elif desc.type == desc.TYPE_MESSAGE:
              nested = value.ListFields()
              bool_msg = all(d.type == d.TYPE_BOOL for d, _ in nested)
              if bool_msg:
                  items = [d.number for d, v in nested if v]
                  if items:
                      data[desc.number] = items
              else:
                  # TODO: for now this code only handles sub-messages with strings
                  md = {}
                  for d, v in nested:
                      if not v or d.type != d.TYPE_STRING:
                          continue
                      md[d.number] = v
                  data[desc.number] = md
      return data
  
file_path: wandb/wandb/sdk/lib/proto_util.py
imports: import json
  from typing import TYPE_CHECKING, Any, Dict, Union
  from wandb.proto import wandb_internal_pb2
label_desc: Use image node_id 5 for calling a global function with example usage: proto_encode_to_dict(pb_obj) and returns: data