{"id":"69d22efdf8683a2c","repo":"aio-libs/aiohttp","slug":"can-not-serialize-value-type-r-headers-r-val","errorCode":null,"errorMessage":"Can not serialize value type: %r\n headers: %r\n value: %r","messagePattern":"Can not serialize value type: %r\n headers: %r\n value: %r","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/formdata.py","lineNumber":143,"sourceCode":"        )\n\n    def _gen_form_data(self) -> multipart.MultipartWriter:\n        \"\"\"Encode a list of fields using the multipart/form-data MIME format\"\"\"\n        for dispparams, headers, value in self._fields:\n            try:\n                if hdrs.CONTENT_TYPE in headers:\n                    part = payload.get_payload(\n                        value,\n                        content_type=headers[hdrs.CONTENT_TYPE],\n                        headers=headers,\n                        encoding=self._charset,\n                    )\n                else:\n                    part = payload.get_payload(\n                        value, headers=headers, encoding=self._charset\n                    )\n            except Exception as exc:\n                raise TypeError(\n                    \"Can not serialize value type: %r\\n \"\n                    \"headers: %r\\n value: %r\" % (type(value), headers, value)\n                ) from exc\n\n            if dispparams:\n                part.set_content_disposition(\n                    \"form-data\", quote_fields=self._quote_fields, **dispparams\n                )\n                # FIXME cgi.FieldStorage doesn't likes body parts with\n                # Content-Length which were sent via chunked transfer encoding\n                assert part.headers is not None\n                part.headers.popall(hdrs.CONTENT_LENGTH, None)\n\n            self._writer.append_payload(part)\n\n        self._fields.clear()\n        return self._writer\n","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/formdata.py#L125-L161","documentation":"Raised by FormData._gen_form_data (multipart path) wrapping any exception from payload.get_payload in a TypeError. It means none of aiohttp's registered Payload subclasses could handle the value type for the given headers/content_type. The message echoes type(value), headers, and value to aid diagnosis.","triggerScenarios":"Appending a field whose value is an arbitrary Python object (e.g. a dict, custom class, or set) with no registered payload handler in multipart mode. Also passing a bytes-like with an unsupported content_type that bypasses defaults.","commonSituations":"Trying to JSON-serialize by passing a raw dict instead of json.dumps(...); passing a custom object without registering a Payload family; passing a memoryview with a content_type that has no payload adapter.","solutions":["Convert the value to a supported type (str/bytes/IOBase) before add_field.","For JSON, pass json.dumps(obj) as a str with content_type='application/json'.","Register a custom payload with payload.register_payload if you need a reusable adapter."],"exampleFix":"// before\nform.add_field('data', {'a': 1}, content_type='application/json')\n// after\nimport json\nform.add_field('data', json.dumps({'a': 1}), content_type='application/json')","handlingStrategy":"validation","validationCode":"import json\nvalue = {'a': 1}\nserialized = json.dumps(value) if isinstance(value, (dict, list)) else value\nform.add_field('data', serialized, content_type='application/json')","typeGuard":"import io\nfrom aiohttp.payload import PAYLOAD_REGISTRY\ndef is_supported_payload_value(v) -> bool:\n    return isinstance(v, (str, bytes, bytearray, memoryview, io.IOBase))","tryCatchPattern":"try:\n    form.add_field('data', value, content_type=ct)\nexcept TypeError:\n    form.add_field('data', json.dumps(value), content_type='application/json')","preventionTips":["Convert dicts/lists to JSON strings before adding to a form.","Wrap file-like objects in io.BytesIO rather than passing raw objects.","Register a custom Payload family if you need reusable serialization."],"tags":["formdata","serialization","multipart","payload"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}