{"record":{"id":"11a6c75234de621f","repo":"apache/beam","slug":"can-not-encode-as-a-64-bit-integer","errorCode":null,"errorMessage":"Can not encode {} as a 64-bit integer","messagePattern":"Can not encode (.+?) as a 64-bit integer","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/gcp/json_value.py","lineNumber":114,"sourceCode":"    json_object = extra_types.JsonObject()\n    for k, v in obj.items():\n      json_object.properties.append(\n          extra_types.JsonObject.Property(\n              key=k, value=to_json_value(v, with_type=with_type)))\n    return extra_types.JsonValue(object_value=json_object)\n  elif with_type:\n    return to_json_value(get_typed_value_descriptor(obj), with_type=False)\n  elif isinstance(obj, str):\n    return extra_types.JsonValue(string_value=obj)\n  elif isinstance(obj, bytes):\n    return extra_types.JsonValue(string_value=obj.decode('utf8'))\n  elif isinstance(obj, bool):\n    return extra_types.JsonValue(boolean_value=obj)\n  elif isinstance(obj, int):\n    if _MININT64 <= obj <= _MAXINT64:\n      return extra_types.JsonValue(integer_value=obj)\n    else:\n      raise TypeError('Can not encode {} as a 64-bit integer'.format(obj))\n  elif isinstance(obj, float):\n    return extra_types.JsonValue(double_value=obj)\n  elif isinstance(obj, ValueProvider):\n    if obj.is_accessible():\n      return to_json_value(obj.get())\n    return extra_types.JsonValue(is_null=True)\n  else:\n    raise TypeError('Cannot convert %s to a JSON value.' % repr(obj))\n\n\ndef from_json_value(v):\n  \"\"\"For internal use only; no backwards-compatibility guarantees.\n\n  Converts ``extra_types.JsonValue`` objects into Python objects.\n\n  Args:\n    v: ``JsonValue`` object to be converted.\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/gcp/json_value.py#L96-L132","documentation":"to_json_value encodes Python ints into the extra_types.JsonValue integer_value field, which is a signed 64-bit integer. Values outside [_MININT64, _MAXINT64] (about +/-9.22e18) raise TypeError 'Can not encode {} as a 64-bit integer'. Python ints are arbitrary precision, but the proto field is not.","triggerScenarios":"to_json_value on an int larger than 2^63-1 or smaller than -2^63 — huge counters, cryptographic-size integers, bit-manipulation results (1 << 64), or oversized IDs; also triggered recursively when encoding containers/ValueProviders holding such ints.","commonSituations":"Passing arbitrary-precision Python ints (hashes, big random numbers, snowflake-style IDs wider than 64 bits) into Beam APIs that JSON-encode values; shifting bits beyond 63; summing counters that overflow 64-bit range.","solutions":["Encode as a string: to_json_value(str(bigint)) and parse back after transport.","Convert to float if approximate magnitude suffices (to_json_value(float(obj))), accepting precision loss.","Clamp or split the value at the application level to fit int64.","Keep the value out of the JSON-encoded payload and pass it via a different channel."],"exampleFix":"// before\nto_json_value(1 << 70)  # TypeError: not a 64-bit integer\n// after\nto_json_value(str(1 << 70))  # encode as string, parse on receipt","handlingStrategy":"validation","validationCode":"_MININT64, _MAXINT64 = -(2**63), 2**63 - 1\ndef is_int64_safe(v) -> bool:\n    return not isinstance(v, int) or isinstance(v, bool) or _MININT64 <= v <= _MAXINT64","typeGuard":"def fits_int64(v) -> bool:\n    return isinstance(v, bool) or (isinstance(v, int) and -(2**63) <= v <= 2**63 - 1)","tryCatchPattern":"try:\n    return to_json_value(v)\nexcept TypeError as e:\n    if '64-bit integer' in str(e):\n        return to_json_value(str(v))  # encode big ints as strings\n    raise","preventionTips":["Check bit-shift and accumulation results stay within 2^63-1.","Encode oversized IDs/hashes as strings at the boundary.","Add an int64-range assertion where large integers enter encoded payloads."],"tags":["python","apache-beam","json","int64","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}