{"record":{"id":"a568a48244a9f9c5","repo":"apache/beam","slug":"f-failed-to-create-monitoringinfo-for-urn-urn-type-type-urn","errorCode":null,"errorMessage":"f'Failed to create MonitoringInfo for urn {urn} type {type_urn} labels {labels} and payload {payload}'","messagePattern":"f'Failed to create MonitoringInfo for urn (.+?) type (.+?) labels (.+?) and payload (.+?)'","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/metrics/monitoring_infos.py","lineNumber":399,"sourceCode":"      labels)\n\n\ndef create_monitoring_info(\n    urn, type_urn, payload, labels=None) -> metrics_pb2.MonitoringInfo:\n  \"\"\"Return the monitoring info for the URN, type, metric and labels.\n\n  Args:\n    urn: The URN of the monitoring info/metric.\n    type_urn: The URN of the type of the monitoring info/metric.\n        i.e. beam:metrics:sum_int_64, beam:metrics:latest_int_64.\n    payload: The payload field to use in the monitoring info.\n    labels: The label dictionary to use in the MonitoringInfo.\n  \"\"\"\n  try:\n    return metrics_pb2.MonitoringInfo(\n        urn=urn, type=type_urn, labels=labels or {}, payload=payload)\n  except TypeError as e:\n    raise RuntimeError(\n        f'Failed to create MonitoringInfo for urn {urn} type {type_urn} '\n        f'labels {labels} and payload {payload}') from e\n\n\ndef is_counter(monitoring_info_proto):\n  \"\"\"Returns true if the monitoring info is a coutner metric.\"\"\"\n  return monitoring_info_proto.type in COUNTER_TYPES\n\n\ndef is_gauge(monitoring_info_proto):\n  \"\"\"Returns true if the monitoring info is a gauge metric.\"\"\"\n  return monitoring_info_proto.type in GAUGE_TYPES\n\n\ndef is_distribution(monitoring_info_proto):\n  \"\"\"Returns true if the monitoring info is a distrbution metric.\"\"\"\n  return monitoring_info_proto.type in DISTRIBUTION_TYPES\n","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/metrics/monitoring_infos.py#L381-L417","documentation":"create_monitoring_info builds a metrics_pb2.MonitoringInfo protobuf. If the arguments (urn, type_urn, labels, payload) have types the protobuf constructor rejects, it raises TypeError, which is re-raised as a RuntimeError with the full argument values included for debugging. The library does this so callers get a descriptive message instead of a bare protobuf TypeError.","triggerScenarios":"Calling int64_counter/int64_distribution/int64_gauge (or their _user_ variants) with non-string labels keys/values, labels that are not a dict, a payload that is not bytes/str, or passing a labels dict containing non-primitive values into create_monitoring_info.","commonSituations":"Metric labels built from dynamic data (e.g. f-strings with None, ints, or bytes instead of str); a payload passed as a dict instead of a serialized bytes string; SDK-internal callers after a Beam version changed the MonitoringInfo proto field types.","solutions":["Ensure all label keys and values are str (cast with str() or decode bytes) before calling the metric helpers.","Ensure payload is bytes (payload.encode() for str, or the serialized proto bytes).","Inspect the chained exception ('from e') message from protobuf to see exactly which field type was rejected.","Ensure labels is a dict or None, not a list of tuples; use dict(labels) if converting."],"exampleFix":"# before\nmetrics = Metrics.get_namespace(step).counter(label_id, labels={\"step\": 3})\n# after\nmetrics = Metrics.get_namespace(step).counter(label_id, labels={\"step\": \"3\"})","handlingStrategy":"try-catch","validationCode":"assert isinstance(labels, (dict, type(None))) and all(isinstance(k, str) and isinstance(v, str) for k, v in (labels or {}).items())\nassert isinstance(payload, (bytes, bytearray, str, type(None)))","typeGuard":"def valid_labels(labels): return labels is None or (isinstance(labels, dict) and all(isinstance(k, str) and isinstance(v, str) for k, v in labels.items()))","tryCatchPattern":"try:\n    mi = create_monitoring_info(urn, type_urn, labels, payload)\nexcept RuntimeError as e:\n    logger.error(\"bad monitoring info args: %s\", e); labels = {k: str(v) for k, v in labels.items()}; mi = create_monitoring_info(urn, type_urn, labels, payload)","preventionTips":["Coerce all label values to str at metric-creation sites","Serialize payloads to bytes explicitly","Wrap dynamic label construction in a helper that validates types"],"tags":["python","metrics","protobuf","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}