{"record":{"id":"2013d8a91da319f8","repo":"openai/openai-python","slug":"multipart-extra-body-must-be-a-mapping","errorCode":null,"errorMessage":"Multipart extra_body must be a mapping","messagePattern":"Multipart extra_body must be a mapping","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/openai/_multipart.py","lineNumber":22,"sourceCode":"\nfrom typing import Mapping, cast\n\nfrom ._types import Body, Omit, NotGiven, FileTypes, RequestFiles\nfrom ._utils._json import openapi_dumps\n\n\ndef encode_multipart(\n    body: object,\n    extra_body: Body | None,\n    encodings: Mapping[str, tuple[str, bool]],\n    raw_body_field: str | None = None,\n    existing_files: RequestFiles | None = None,\n) -> tuple[dict[str, object] | None, RequestFiles | None, bytes | None, str]:\n    \"\"\"Prepare explicitly encoded form fields without flattening their JSON contents.\"\"\"\n    if not isinstance(body, Mapping):\n        raise TypeError(\"Multipart request body must be a mapping\")\n    if extra_body is not None and not isinstance(extra_body, Mapping):\n        raise TypeError(\"Multipart extra_body must be a mapping\")\n    original = cast(Mapping[str, object], body)\n    overrides = cast(Mapping[str, object], extra_body or {})\n    merged = {key: value for key, value in {**original, **overrides}.items() if not isinstance(value, (Omit, NotGiven))}\n    # A raw request alternative is safe only when there is no other payload to lose.\n    # Explicit null remains a JSON part; only an omitted field selects the raw body.\n    if not existing_files and raw_body_field is not None and set(merged) == {raw_body_field}:\n        value = merged[raw_body_field]\n        if not isinstance(value, str):\n            raise TypeError(\"Raw multipart alternative must be a string\")\n        return None, None, value.encode(\"utf-8\"), encodings[raw_body_field][0]\n\n    files: list[tuple[str, FileTypes]] = list(\n        existing_files.items() if isinstance(existing_files, Mapping) else (existing_files or [])\n    )\n    for name, (content_type, as_json) in encodings.items():\n        if name not in merged:\n            continue\n        value = merged.pop(name)","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_multipart.py#L4-L40","documentation":"When extra_body is supplied to a multipart request, it must be a Mapping so its keys can override/merge with the body fields. Passing a non-mapping extra_body (string, list, None-like objects that aren't None) makes the merge impossible and raises TypeError.","triggerScenarios":"Calling a multipart endpoint with extra_body set to a JSON string, a list, or any non-dict value; programmatic construction of extra_body that defaults to a non-mapping.","commonSituations":"Developers reusing extra_body='{{\"key\": \"value\"}}' style serialized JSON from other tooling; forwarding a user-supplied parameter into extra_body without type checking.","solutions":["Pass extra_body as a dict: extra_body={'key': 'value'}","If extra_body comes from JSON text, json.loads() it first","Omit extra_body entirely (pass None/NotGiven) when not needed"],"exampleFix":"// before\nclient.audio.transcriptions.create(..., extra_body='{\"language\": \"en\"}')\n// after\nclient.audio.transcriptions.create(..., extra_body={\"language\": \"en\"})","handlingStrategy":"type-guard","validationCode":"if extra_body is not None and not isinstance(extra_body, Mapping):\n    extra_body = json.loads(extra_body)  # or reject","typeGuard":"from collections.abc import Mapping\nfrom typing import TypeGuard\n\ndef is_valid_extra_body(value: object) -> TypeGuard[Mapping[str, object]]:\n    return value is None or isinstance(value, Mapping)","tryCatchPattern":null,"preventionTips":["Pass extra_body only as a dict","json.loads() any JSON-text before using it as extra_body"],"tags":["python","multipart","extra-body","validation"],"backgroundTag":"invalid-extra-body-type","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}