BerriAI/litellm · error · TypeError

completion_response must be of type ImageResponse for…

Error message

completion_response must be of type ImageResponse for bedrock image cost calculation

What it means

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.

Solutions

  1. Pass the object returned by litellm.image_generation(...) - it is an ImageResponse
  2. Reconstruct from a dict: ImageResponse(**img_dict) before calling the cost helper
  3. Type-guard in custom callbacks: only call the image cost path when isinstance(response, ImageResponse)
  4. Branch callbacks on response type instead of assuming ModelResponse everywhere

Example fix

# before
cost = litellm.response_cost_calculator(completion_response=chat_response, custom_llm_provider="bedrock")
# TypeError: completion_response must be of type ImageResponse...

# after
from litellm.types.utils import ImageResponse
if isinstance(completion_response, ImageResponse):
    cost = litellm.response_cost_calculator(completion_response=completion_response, custom_llm_provider="bedrock")
Defensive patterns

Strategy: type-guard

Validate before calling

from litellm.types.utils import ImageResponse

if custom_llm_provider == "bedrock":
    assert isinstance(completion_response, ImageResponse), "bedrock image cost needs ImageResponse"
cost = calculate_image_cost(completion_response, custom_llm_provider="bedrock")

Type guard

from litellm.types.utils import ImageResponse

def is_image_response(v: object) -> bool:
    return isinstance(v, ImageResponse)

Try / catch

try:
    cost = calculate_image_cost(completion_response, custom_llm_provider="bedrock")
except TypeError as e:
    if "must be of type ImageResponse" in str(e):
        cost = 0.0  # skip cost for non-image payloads
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/6ea6495488c57b08. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/llm_cost_calc/utils.py:1265

            quality = completion_response.quality or "standard"
        if n is None:
            n = len(completion_response.data) if completion_response.data else 0

        if custom_llm_provider == litellm.LlmProviders.VERTEX_AI.value:
            if isinstance(completion_response, ImageResponse):
                return vertex_ai_image_cost_calculator(
                    model=model,
                    image_response=completion_response,
                )
        elif custom_llm_provider == litellm.LlmProviders.BEDROCK.value:
            if isinstance(completion_response, ImageResponse):
                return bedrock_image_cost_calculator(
                    model=model,
                    size=size,
                    image_response=completion_response,
                    optional_params=optional_params,
                )
            raise TypeError("completion_response must be of type ImageResponse for bedrock image cost calculation")
        elif custom_llm_provider == litellm.LlmProviders.RECRAFT.value:
            from litellm.llms.recraft.cost_calculator import (
                cost_calculator as recraft_image_cost_calculator,
            )

            return recraft_image_cost_calculator(
                model=model,
                image_response=completion_response,
            )
        elif custom_llm_provider == litellm.LlmProviders.AIML.value:
            from litellm.llms.aiml.image_generation.cost_calculator import (
                cost_calculator as aiml_image_cost_calculator,
            )

            return aiml_image_cost_calculator(
                model=model,
                image_response=completion_response,
            )

View on GitHub (pinned to 77b7c6c40c)