{"record":{"id":"6ea6495488c57b08","repo":"BerriAI/litellm","slug":"completion-response-must-be-of-type-imageresponse","errorCode":null,"errorMessage":"completion_response must be of type ImageResponse for bedrock image cost calculation","messagePattern":"completion_response must be of type ImageResponse for bedrock image cost calculation","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/llm_cost_calc/utils.py","lineNumber":1265,"sourceCode":"            quality = completion_response.quality or \"standard\"\n        if n is None:\n            n = len(completion_response.data) if completion_response.data else 0\n\n        if custom_llm_provider == litellm.LlmProviders.VERTEX_AI.value:\n            if isinstance(completion_response, ImageResponse):\n                return vertex_ai_image_cost_calculator(\n                    model=model,\n                    image_response=completion_response,\n                )\n        elif custom_llm_provider == litellm.LlmProviders.BEDROCK.value:\n            if isinstance(completion_response, ImageResponse):\n                return bedrock_image_cost_calculator(\n                    model=model,\n                    size=size,\n                    image_response=completion_response,\n                    optional_params=optional_params,\n                )\n            raise TypeError(\"completion_response must be of type ImageResponse for bedrock image cost calculation\")\n        elif custom_llm_provider == litellm.LlmProviders.RECRAFT.value:\n            from litellm.llms.recraft.cost_calculator import (\n                cost_calculator as recraft_image_cost_calculator,\n            )\n\n            return recraft_image_cost_calculator(\n                model=model,\n                image_response=completion_response,\n            )\n        elif custom_llm_provider == litellm.LlmProviders.AIML.value:\n            from litellm.llms.aiml.image_generation.cost_calculator import (\n                cost_calculator as aiml_image_cost_calculator,\n            )\n\n            return aiml_image_cost_calculator(\n                model=model,\n                image_response=completion_response,\n            )","sourceCodeStart":1247,"sourceCodeEnd":1283,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/llm_cost_calc/utils.py#L1247-L1283","documentation":"The image cost-calculation dispatcher routes by provider; for bedrock it requires an ImageResponse (LiteLLM's image-generation return type, litellm.types.utils.ImageResponse) and passes it to bedrock_image_cost_calculator. Anything else - a chat ModelResponse, a plain dict, a mock object - raises this TypeError before any cost math runs, because bedrock pricing needs the generated image metadata.","triggerScenarios":"Invoking litellm's image cost calculator with custom_llm_provider=\"bedrock\" while completion_response is not an ImageResponse - e.g. a custom spend-tracking callback feeding chat responses, or tests substituting generic response fixtures for image responses.","commonSituations":"Custom cost/spend callbacks that assume every response is a ModelResponse; mocking image generation with chat-shaped fixtures; adapters converting ImageResponse to dict before cost tracking runs.","solutions":["Pass the object returned by litellm.image_generation(...) - it is an ImageResponse","Reconstruct from a dict: ImageResponse(**img_dict) before calling the cost helper","Type-guard in custom callbacks: only call the image cost path when isinstance(response, ImageResponse)","Branch callbacks on response type instead of assuming ModelResponse everywhere"],"exampleFix":"# before\ncost = litellm.response_cost_calculator(completion_response=chat_response, custom_llm_provider=\"bedrock\")\n# TypeError: completion_response must be of type ImageResponse...\n\n# after\nfrom litellm.types.utils import ImageResponse\nif isinstance(completion_response, ImageResponse):\n    cost = litellm.response_cost_calculator(completion_response=completion_response, custom_llm_provider=\"bedrock\")","handlingStrategy":"type-guard","validationCode":"from litellm.types.utils import ImageResponse\n\nif custom_llm_provider == \"bedrock\":\n    assert isinstance(completion_response, ImageResponse), \"bedrock image cost needs ImageResponse\"\ncost = calculate_image_cost(completion_response, custom_llm_provider=\"bedrock\")","typeGuard":"from litellm.types.utils import ImageResponse\n\ndef is_image_response(v: object) -> bool:\n    return isinstance(v, ImageResponse)","tryCatchPattern":"try:\n    cost = calculate_image_cost(completion_response, custom_llm_provider=\"bedrock\")\nexcept TypeError as e:\n    if \"must be of type ImageResponse\" in str(e):\n        cost = 0.0  # skip cost for non-image payloads\n    else:\n        raise","preventionTips":["Only feed image cost calculators objects returned by litellm.image_generation","In generic spend callbacks, branch on isinstance(response, ImageResponse) before provider-specific math","Make test fixtures use the real response types, not chat-shaped stand-ins"],"tags":["bedrock","image-generation","cost-calculation","litellm","type-validation"],"backgroundTag":"cost-calculation-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}