{"record":{"id":"d3c46fbbf6679283","repo":"apache/beam","slug":"expected-int-metric-type-but-received-s-with-value-s-type","errorCode":null,"errorMessage":"'Expected int metric type but received %s with value %s' % (type(metric), metric)","messagePattern":"'Expected int metric type but received (.+?) with value (.+?)' % \\(type\\(metric\\), metric\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/metrics/monitoring_infos.py","lineNumber":320,"sourceCode":"  return create_monitoring_info(\n      USER_GAUGE_URN, LATEST_INT64_TYPE, payload, labels)\n\n\ndef int64_gauge(urn, metric, ptransform=None) -> metrics_pb2.MonitoringInfo:\n  \"\"\"Return the gauge monitoring info for the URN, metric and labels.\n\n  Args:\n    urn: The URN of the monitoring info/metric.\n    metric: An int representing the value. The current time will be used for\n            the timestamp.\n    ptransform: The ptransform id used as a label.\n  \"\"\"\n  labels = create_labels(ptransform=ptransform)\n  if isinstance(metric, int):\n    value = metric\n    time_ms = int(time.time()) * 1000\n  else:\n    raise TypeError(\n        'Expected int metric type but received %s with value %s' %\n        (type(metric), metric))\n  coder = coders.VarIntCoder()\n  payload = coder.encode(time_ms) + coder.encode(value)\n  return create_monitoring_info(urn, LATEST_INT64_TYPE, payload, labels)\n\n\ndef user_set_string(namespace, name, metric, ptransform=None):\n  \"\"\"Return the string set monitoring info for the URN, metric and labels.\n\n  Args:\n    namespace: User-defined namespace of StringSet.\n    name: Name of StringSet.\n    metric: The StringSetData representing the metrics.\n    ptransform: The ptransform id used as a label.\n  \"\"\"\n  labels = create_labels(ptransform=ptransform, namespace=namespace, name=name)\n  if isinstance(metric, StringSetData):","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/metrics/monitoring_infos.py#L302-L338","documentation":"int64_gauge encodes a LATEST_INT64 gauge MonitoringInfo where the payload is (timestamp_ms, value), so the metric argument must be a plain int. Passing any non-int raises TypeError immediately. The timestamp is generated internally, so callers only supply the integer value.","triggerScenarios":"Calling int64_gauge with a GaugeData instance, float, string, or None, e.g. int64_gauge('urn', metric=GaugeData(42)) — the mirror image of error 3148.","commonSituations":"Mixing up int64_gauge and int64_user_gauge, which have opposite metric-type requirements; passing numpy integers or Decimal values in code that assumes int compatibility; reading gauge values from config/JSON where they arrive as strings.","solutions":["Pass a plain Python int: int64_gauge(urn, metric=int(value)).","If you have a GaugeData with its own timestamp, call int64_user_gauge instead.","Coerce with isinstance(metric, int) check (and int() cast) before calling."],"exampleFix":"// before\nmi = int64_gauge(urn, metric=str(value))\n// after\nmi = int64_gauge(urn, metric=int(value))","handlingStrategy":"type-guard","validationCode":"if not isinstance(metric, int):\n    metric = int(metric)","typeGuard":"def is_int_metric(m):\n    return isinstance(m, int) and not isinstance(m, bool)","tryCatchPattern":"try:\n    mi = int64_gauge(urn, metric=metric)\nexcept TypeError as e:\n    logger.error('int64_gauge requires an int: %s', e)\n    raise","preventionTips":["Remember: int64_gauge takes a plain int; int64_user_gauge takes GaugeData.","Coerce numpy/Decimal/string values with int() before passing.","Centralize gauge creation in one helper that does the type coercion."],"tags":["python","apache-beam","metrics","type-mismatch","gauge"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}