BerriAI/litellm · error · TypeError

completion_response must be of type ImageResponse for bedroc

Error message

completion_response must be of type ImageResponse for bedrock image cost calculation

What it means

Raised by litellm's generic cost-calculator dispatch when a Bedrock image-generation cost is requested but the completion_response argument is not an instance of litellm.types.utils.ImageResponse. The dispatcher (used by response_cost calculation and proxy spend tracking) branches per provider; only ImageResponse objects carry the Bedrock image metadata needed for pricing. Passing a ModelResponse, a raw dict, or None triggers this TypeError before any pricing math runs.

Source

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

            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 6c2dcb801b)

Solutions

  1. Ensure the value passed as completion_response for bedrock image calls is a litellm.types.utils.ImageResponse (e.g. built via LiteLLMResponseObjectHandler.convert_to_image_response).
  2. If you call the dispatcher directly, branch on isinstance(completion_response, ImageResponse) before selecting the bedrock image path, mirroring utils.py:1196.
  3. In custom handler code, convert raw provider JSON into ImageResponse before returning it so downstream cost tracking sees the right type.
  4. If you never intend image pricing, route the call through the standard completion/embedding cost calculators instead of the image-generation branch.

Example fix

// before
resp = await client.images.generate(...)  # raw dict kept
cost = litellm.completion_cost(completion_response=resp, model='bedrock/titan-image')

# after
from litellm.types.utils import ImageResponse
img = ImageResponse(**resp)  # or convert_to_image_response(...)
cost = litellm.completion_cost(completion_response=img, model='bedrock/titan-image')
Defensive patterns

Strategy: type-guard

Validate before calling

from litellm.types.utils import ImageResponse
def is_image_response(resp) -> bool:
    return isinstance(resp, ImageResponse)

Type guard

from litellm.types.utils import ImageResponse
from typing import TypeGuard

def is_image_response(resp: object) -> TypeGuard[ImageResponse]:
    return isinstance(resp, ImageResponse)

Try / catch

try:
    cost = litellm.completion_cost(completion_response=resp, model=model)
except TypeError as e:
    if 'ImageResponse' in str(e):
        resp = ImageResponse(**resp) if isinstance(resp, dict) else resp
        cost = litellm.completion_cost(completion_response=resp, model=model)
    else:
        raise

Prevention

When it happens

Trigger: Calling litellm.completion_cost() / cost calculation internals with custom_llm_provider='bedrock' and response_type='image_generation' where completion_response is not an ImageResponse; building custom image-generation handler code that returns a dict or ModelResponse instead of ImageResponse; calling bedrock_image_cost_calculator via the generic dispatcher with a mocked/wrong-type response object.

Common situations: Custom providers or wrappers that intercept image generation and forward a plain dict to cost tracking; unit tests that mock cost calculation with fake objects; version upgrades where the image cost API was tightened to require ImageResponse (previously accepted dicts silently).

Related errors


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