{"record":{"id":"6ca5db2f085c6d8e","repo":"BerriAI/litellm","slug":"data-field-is-required-for-openai-image-variation","errorCode":null,"errorMessage":"data field is required, for openai image variations. Got={data}","messagePattern":"data field is required, for openai image variations\\. Got=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/openai/image_variations/handler.py","lineNumber":149,"sourceCode":"            provider_config: Final = ProviderConfigManager.get_provider_image_variation_config(\n                model=model or \"\",  # openai defaults to dall-e-2\n                provider=LlmProviders.OPENAI,\n            )\n\n            if provider_config is None:\n                raise ValueError(f\"image variation provider not found: {custom_llm_provider}.\")\n\n            max_retries: Final = optional_params.pop(\"max_retries\", 2)\n\n            data: Final = provider_config.transform_request_image_variation(\n                model=model,\n                image=image,\n                optional_params=optional_params,\n                headers=headers or {},\n            )\n            json_data: Final = data.get(\"data\")\n            if not json_data:\n                raise ValueError(f\"data field is required, for openai image variations. Got={data}\")\n            ## LOGGING\n            logging_obj.pre_call(\n                input=\"\",\n                api_key=api_key,\n                additional_args={\n                    \"headers\": headers,\n                    \"api_base\": api_base,\n                    \"complete_input_dict\": data,\n                },\n            )\n            if litellm_params.get(\"async_call\", False):\n                return self.async_image_variations(\n                    api_base=api_base,\n                    data=json_data,\n                    headers=headers or {},\n                    model_response=model_response,\n                    api_key=api_key,\n                    logging_obj=logging_obj,","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/openai/image_variations/handler.py#L131-L167","documentation":"After the image-variation provider config transforms the request, litellm expects a dict containing a 'data' payload to send; if transform_request_image_variation returns a dict without a truthy 'data' field, this ValueError is raised with the returned dict echoed back. It indicates the request-transform step produced an unusable payload - an internal contract violation between the handler and the provider config.","triggerScenarios":"Calling litellm.image_variations() where the provider config's transform_request_image_variation fails to build the multipart/json payload - e.g. a malformed image input that defeats encoding, optional_params/metadata configurations that make the transform return only files and no data dict, or a custom/overridden variation config whose transform method does not implement the {'data': ..., 'files': ...} contract.","commonSituations":"Subclassing or monkey-patching variation configs and returning the wrong shape; passing unusual file-like objects (SpooledTemporaryFile edge cases, 0-byte reads) so the transform silently produces nothing; version drift where the transform contract changed but a local override did not.","solutions":["Ensure the image argument is a valid, readable file (open in binary mode, non-empty PNG/JPEG) before the call","If you override transform_request_image_variation, return {'data': <json-dict>, 'files': <list>} exactly - the handler requires the 'data' key","Reproduce with the plain OpenAI dall-e-2 path (no custom config); if it works, the defect is in your custom transform; if not, upgrade litellm / file an issue","Upgrade litellm - the transform contract and its error handling have evolved across versions"],"exampleFix":"# before (custom config violating the contract)\nclass MyConfig(OpenAIImageVariationConfig):\n    def transform_request_image_variation(self, **kwargs):\n        return {\"files\": [\"img.png\"]}  # missing \"data\" -> ValueError\n\n# after\nclass MyConfig(OpenAIImageVariationConfig):\n    def transform_request_image_variation(self, model, image, optional_params, headers):\n        data, files = super().transform_request_image_variation(\n            model=model, image=image, optional_params=optional_params, headers=headers\n        )\n        return {\"data\": data, \"files\": files}  # contract satisfied","handlingStrategy":"validation","validationCode":"import os\n\n# ensure the input yields actual bytes before transforming\ndef variation_input_ok(path: str) -> bool:\n    return os.path.exists(path) and os.path.getsize(path) > 0\n\nassert variation_input_ok(\"cat.png\"), \"image missing/empty - transform would return no data\"\n\nwith open(\"cat.png\", \"rb\") as img:  # keep the file OPEN through the call\n    resp = litellm.image_variations(image=img, model=\"dall-e-2\")","typeGuard":"def transform_result_valid(result) -> bool:\n    \"\"\"A variation transform result must carry a truthy 'data' payload.\"\"\"\n    return isinstance(result, dict) and bool(result.get(\"data\"))","tryCatchPattern":"try:\n    resp = litellm.image_variations(image=open(\"cat.png\", \"rb\"), model=\"dall-e-2\")\nexcept ValueError as e:\n    if \"data field is required\" in str(e):\n        # input/transform issue: retry once with a freshly-opened binary handle\n        with open(\"cat.png\", \"rb\") as img:\n            resp = litellm.image_variations(image=img, model=\"dall-e-2\")\n    else:\n        raise","preventionTips":["Open image files in binary mode and keep the handle open for the duration of the call - closed/consumed handles make transforms return empty payloads","Do not pre-read or seek file handles before passing them; litellm needs to stream the bytes","If overriding transform_request_image_variation, honor the {'data': ..., 'files': ...} return contract exactly","On upgrade, re-run your variation integration test - transform contracts have changed across litellm versions"],"tags":["image-variations","request-transform","internal-contract","openai"],"backgroundTag":"missing-required-argument","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}