{"record":{"id":"4d33028298753179","repo":"apache/beam","slug":"object-of-type-s-is-not-json-serializable","errorCode":null,"errorMessage":"Object of type '%s' is not JSON serializable","messagePattern":"Object of type '(.+?)' is not JSON serializable","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/gcp/bigquery_tools.py","lineNumber":162,"sourceCode":"  DEFLATE = 'DEFLATE'\n  SNAPPY = 'SNAPPY'\n  NONE = 'NONE'\n\n\ndef default_encoder(obj):\n  if isinstance(obj, decimal.Decimal):\n    return str(obj)\n  elif isinstance(obj, bytes):\n    # on python 3 base64-encoded bytes are decoded to strings\n    # before being sent to BigQuery\n    return obj.decode('utf-8')\n  elif isinstance(obj, (datetime.date, datetime.time)):\n    return str(obj)\n  elif isinstance(obj, datetime.datetime):\n    return obj.isoformat()\n\n  _LOGGER.error(\"Unable to serialize %r to JSON\", obj)\n  raise TypeError(\n      \"Object of type '%s' is not JSON serializable\" % type(obj).__name__)\n\n\ndef get_hashable_destination(destination):\n  \"\"\"Parses a table reference into a (project, dataset, table) tuple.\n\n  Args:\n    destination: Either a TableReference object from the bigquery API.\n      The object has the following attributes: projectId, datasetId, and\n      tableId. Or a string representing the destination containing\n      'PROJECT:DATASET.TABLE'.\n  Returns:\n    A string representing the destination containing\n    'PROJECT:DATASET.TABLE'.\n  \"\"\"\n  if isinstance(destination, TableReference):\n    return '%s:%s.%s' % (\n        destination.projectId, destination.datasetId, destination.tableId)","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L144-L180","documentation":"Raised by the module's default_encoder when JsonDotSerializator/JSON serialization encounters an object it has no branch for (it handles dates, datetimes, times, Decimal, etc., but not arbitrary types). The encoder logs the offending object and raises TypeError to prevent silently producing invalid JSON for BigQuery API requests.","triggerScenarios":"Calling json.dumps(obj, cls=JsonDotSerializator/default_encoder) or any Beam code path that serializes a request payload (query config, job config) containing an object of an unhandled type, e.g. a numpy value, bytes, set, or custom class.","commonSituations":"Passing numpy types from dataframe pipelines into query parameters; putting bytes or enums into job labels/parameters; inserting datetime subclasses or timezone objects not covered by the isoformat branch.","solutions":["Convert the offending value to a JSON-safe type before passing it (str(), float(), int(), isoformat()).","For Decimal/numpy values, cast explicitly: float(np_value) or str(decimal_value).","Encode bytes as base64 or a string before adding to the request.","Subclass the encoder and add a 'default' branch for your custom type if you own the serialization call site."],"exampleFix":"# before\nquery_params = {'value': numpy.int64(5)}\n\n# after\nquery_params = {'value': int(numpy.int64(5))}","handlingStrategy":"type-guard","validationCode":"import json\ndef ensure_json_safe(payload):\n    json.dumps(payload)  # raises TypeError early for unserializable values","typeGuard":"def is_json_safe(obj):\n    try:\n        json.dumps(obj)\n        return True\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    send_request(payload)\nexcept TypeError as e:\n    if 'not JSON serializable' in str(e):\n        payload = sanitize(payload)  # cast numpy/Decimal/bytes to str/int/float\n    else:\n        raise","preventionTips":["Cast numpy/pandas values to native Python types before adding them to BQ request payloads","Keep query parameter values to str/int/float/bool/datetime","Run a local json.dumps smoke test on job configs in CI"],"tags":["json","serialization","bigquery","python"],"backgroundTag":"json-serialization-failed","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"}