BerriAI/litellm · error · BadRequestError

BedrockException - {error_str}

Error message

BedrockException - {error_str}

What it means

Bedrock-specific: BadRequestError raised when the error text contains 'Malformed input request'. The request body does not conform to the Bedrock Converse/InvokeModel schema for that model — a structural problem with the payload, not its content size.

Source

Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:845

        or "Input is too long" in error_str
        or "prompt is too long" in error_str
        or "prompt: length: 1.." in error_str
        or "Too many input tokens" in error_str
    ):
        raise ContextWindowExceededError(
            message=f"BedrockException: Context Window Error - {error_str}",
            model=model,
            llm_provider="bedrock",
        )
    elif "Conversation blocks and tool result blocks cannot be provided in the same turn." in error_str:
        raise BadRequestError(
            message=f"BedrockException - {error_str}\n. Enable 'litellm.modify_params=True' (for PROXY do: `litellm_settings::modify_params: True`) to insert a dummy assistant message and fix this error.",
            model=model,
            llm_provider="bedrock",
            response=getattr(original_exception, "response", None),
        )
    elif "Malformed input request" in error_str:
        raise BadRequestError(
            message=f"BedrockException - {error_str}",
            model=model,
            llm_provider="bedrock",
            response=getattr(original_exception, "response", None),
        )
    elif "A conversation must start with a user message." in error_str:
        raise BadRequestError(
            message=f"BedrockException - {error_str}\n. Pass in default user message via `completion(..,user_continue_message=)` or enable `litellm.modify_params=True`.\nFor Proxy: do via `litellm_settings::modify_params: True` or user_continue_message under `litellm_params`",
            model=model,
            llm_provider="bedrock",
            response=getattr(original_exception, "response", None),
        )
    elif (
        "Unable to locate credentials" in error_str
        or "The security token included in the request is invalid" in error_str
    ):
        raise AuthenticationError(
            message=f"BedrockException Invalid Authentication - {error_str}",

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Inspect the full error text after 'BedrockException - ' for the specific schema violation.
  2. Let litellm build the payload (call litellm.completion with model='bedrock/...') instead of hand-crafting the body, and update litellm to the latest version.
  3. Validate structure: first message system-like content in system=, alternating user/assistant, tool results paired with tool_use.
  4. Remove content types the model family does not support (e.g. image blocks to text-only models).

Example fix

# before
resp = litellm.completion(model="bedrock/meta.llama3-8b-instruct-v1:0", messages=[{"role": "system", "content": "..."}, {"role": "user", "content": [{"type": "image_url", "image_url": {...}}]}])

# after
resp = litellm.completion(model="bedrock/meta.llama3-8b-instruct-v1:0", messages=[{"role": "user", "content": "describe this: ..."}])  # text-only model: no image blocks
Defensive patterns

Strategy: validation

Validate before calling

def bedrock_messages_well_formed(messages: list) -> bool:
    if not messages:
        return False
    non_system = [m for m in messages if m.get("role") != "system"]
    if not non_system or non_system[0].get("role") != "user":
        return False
    for m in non_system:
        content = m.get("content")
        if content is None:
            return False
        if isinstance(content, list):
            for b in content:
                if b.get("type") not in {"text", "image_url", "tool_use", "tool_result"}:
                    return False
    return True

Type guard

import litellm

def is_bedrock_malformed(e: BaseException) -> bool:
    return isinstance(e, litellm.BadRequestError) and "Malformed input request" in str(e)

Try / catch

try:
    resp = litellm.completion(model="bedrock/...", messages=msgs)
except litellm.BadRequestError as e:
    if "Malformed input request" in str(e):
        log.error("payload rejected by bedrock: %s", msgs)  # fix structure; do not retry unchanged
    raise

Prevention

When it happens

Trigger: Invalid roles or role ordering, tool_use/toolResult blocks that do not pair up, unsupported content block types for the model (e.g. images sent to a text-only Bedrock model), or wrong inference-specific fields (inferenceConfig, toolConfig) for the model family.

Common situations: Using an OpenAI-format feature the Bedrock model does not support (system as a non-first message, images, json schema), version-specific schema differences between Bedrock model families (Titan vs Claude vs Llama), or hand-rolled request builders.

Related errors


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