{"record":{"id":"d97bc89b2dd6e20e","repo":"psf/requests","slug":"cannot-encode-objects-that-are-not-2-tuples","errorCode":null,"errorMessage":"cannot encode objects that are not 2-tuples","messagePattern":"cannot encode objects that are not 2-tuples","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/requests/utils.py","lineNumber":365,"sourceCode":"\n    ::\n\n        >>> from_key_val_list([('key', 'val')])\n        OrderedDict([('key', 'val')])\n        >>> from_key_val_list('string')\n        Traceback (most recent call last):\n        ...\n        ValueError: cannot encode objects that are not 2-tuples\n        >>> from_key_val_list({'key': 'val'})\n        OrderedDict([('key', 'val')])\n\n    :rtype: OrderedDict\n    \"\"\"\n    if value is None:\n        return None\n\n    if isinstance(value, (str, bytes, bool, int)):\n        raise ValueError(\"cannot encode objects that are not 2-tuples\")\n\n    return OrderedDict(value)\n\n\n@overload\ndef to_key_val_list(value: None) -> None: ...\n@overload\ndef to_key_val_list(\n    value: _t.SupportsItems[_KT, _VT] | Iterable[tuple[_KT, _VT]],\n) -> list[tuple[_KT, _VT]]: ...\ndef to_key_val_list(\n    value: _t.SupportsItems[_KT, _VT] | Iterable[tuple[_KT, _VT]] | None,\n) -> list[tuple[_KT, _VT]] | None:\n    \"\"\"Take an object and test to see if it can be represented as a\n    dictionary. If it can be, return a list of tuples, e.g.,\n\n    ::\n","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/psf/requests/blob/8068356288978c4f54661ae6f95afe0e0831885e/src/requests/utils.py#L347-L383","documentation":"Raised by from_key_val_list() when the input is a scalar type (str, bytes, bool, or int) that cannot be interpreted as an iterable of key-value pairs. This utility builds an OrderedDict from mappings or 2-tuple iterables. Scalar types are explicitly rejected because they would produce a confusing error inside OrderedDict().","triggerScenarios":"Calling from_key_val_list('a string'), from_key_val_list(42), from_key_val_list(True), or from_key_val_list(b'bytes'); passing a scalar where a dict or list-of-tuples is expected.","commonSituations":"This function is used internally (e.g., in cookie and data encoding). Users typically hit it indirectly by passing a string where a dict is expected to params/data/cookies kwargs; or by calling this utility directly with wrong types.","solutions":["Pass a dict or list of tuples instead of a scalar: from_key_val_list({'key': 'val'}).","If you have a single string value, wrap it in a structure: from_key_val_list([('key', 'value')]).","Check the type before calling: if not isinstance(value, (str, bytes, bool, int))."],"exampleFix":"# before\nfrom requests.utils import from_key_val_list\nresult = from_key_val_list('my string')  # ValueError\n\n# after\nresult = from_key_val_list({'my': 'string'})","handlingStrategy":"type-guard","validationCode":"if isinstance(value, (str, bytes, bool, int)):\n    raise ValueError(f'Expected dict or iterable of tuples, got {type(value)}')\nresult = from_key_val_list(value)","typeGuard":"from collections.abc import Mapping, Iterable\ndef is_key_val_capable(v) -> bool:\n    return v is None or isinstance(v, (Mapping,)) or (\n        isinstance(v, Iterable) and not isinstance(v, (str, bytes))\n    )","tryCatchPattern":null,"preventionTips":["Always pass dict or list-of-tuples to functions expecting key-value data.","Use type annotations and a type checker to catch scalar inputs at development time.","Wrap config-derived values in dict structures before passing to requests utilities."],"tags":["value-error","encoding","key-value","type-error","utils"],"backgroundTag":null,"analyzedSha":"8068356288978c4f54661ae6f95afe0e0831885e","analyzedAt":"2026-08-11T20:11:09.238Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}