Skip to content

fix(bedrock): keep cache points off non-PDF document blocks on Converse - #50927

Open
habonlaci wants to merge 1 commit into
BerriAI:mainfrom
habonlaci:fix-bedrock-cache-point-after-document
Open

habonlaci wants to merge 1 commit into
BerriAI:mainfrom
habonlaci:fix-bedrock-cache-point-after-document

Conversation

@habonlaci

@habonlaci habonlaci commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

Bedrock Converse rejects a request with "Cache point cannot be inserted after the preceding content block" when a cache point directly follows a non-PDF document block such as txt or csv. This hapens whenever the last content block of a message is such a file and prompt caching puts a cache point on it. The fix moves the cache point in front of those documents, or drops it when nothing cacheable comes before them.

Details

TLDR

Problem this solves:

  • Bedrock Converse rejects a cache point right after a non-PDF document
  • Automatic prompt caching puts one there when a text file is last
  • Client cache_control on a txt, csv, md, html, docx or xlsx file fails too

How it solves it:

  • The cache point moves ahead of the trailing non-PDF documents
  • It is dropped when nothing cacheable precedes those documents
  • PDF, image and text blocks keep their cache point where it was

User Flow

Before: a chat app user attaches a text file after their question and the request to a Bedrock Claude model fails

  1. The proxy admin has enable_anthropic_prompt_caching: true in litellm_settings
  2. The app sends POST https://litellm-domain/v1/chat/completions with a text part followed by a text/plain file part, or the same on /v1/responses with input_text and input_file
  3. The response is 400 with Cache point cannot be inserted after the preceding content block for this model

After: the same request returns a completion

  1. The proxy admin has enable_anthropic_prompt_caching: true in litellm_settings
  2. The app sends the same POST https://litellm-domain/v1/chat/completions with a text part followed by a text/plain file part
  3. The response is 200 with a chat completion, and the prompt is cached up to the block before the file

Relevant issues

Fixes #50923

Related

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Live calls to Bedrock on 2026-11-10 through a local proxy, once from the merge base and once from the tip. The requests are synthetic and each one puts cache_control on a file that is the last content part.

Shared setup

config.yaml

model_list:
  - model_name: claude
    litellm_params:
      model: bedrock/converse/eu.anthropic.claude-haiku-4-5-20251001-v1:0
      aws_region_name: os.environ/AWS_REGION_NAME

Env vars set: AWS_PROFILE, AWS_REGION_NAME, LITELLM_MASTER_KEY

Proxy start, with PYTHONPATH pointing at the checkout under test

litellm --config config.yaml --port 4101

Request, one per case with the matching body file

curl -s http://localhost:4101/v1/chat/completions \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" \
  -d @txt-last.json
Request bodies

txt-last.json

{
 "model": "claude",
 "max_tokens": 32,
 "messages": [
  {
   "role": "user",
   "content": [
    {
     "type": "text",
     "text": "Summarize the attached file in one sentence."
    },
    {
     "type": "file",
     "file": {
      "file_data": "data:text/plain;base64,UXVhcnRlcmx5IG5vdGVzLiBSZXZlbnVlIHdlbnQgdXAgYnkgZm91ciBwZXJjZW50Lgo="
     },
     "cache_control": {
      "type": "ephemeral"
     }
    }
   ]
  }
 ]
}

csv-last.json is the same with file_data set to

data:text/csv;base64,cXVhcnRlcixyZXZlbnVlClExLDEwMApRMiwxMDQK

pdf-last.json is the same with file_data set to

data:application/pdf;base64,JVBERi0xLjQKMSAwIG9iajw8L1R5cGUvQ2F0YWxvZy9QYWdlcyAyIDAgUj4+ZW5kb2JqCjIgMCBvYmo8PC9UeXBlL1BhZ2VzL0tpZHNbMyAwIFJdL0NvdW50IDE+PmVuZG9iagozIDAgb2JqPDwvVHlwZS9QYWdlL1BhcmVudCAyIDAgUi9NZWRpYUJveFswIDAgMzAwIDE0NF0vQ29udGVudHMgNCAwIFIvUmVzb3VyY2VzPDwvRm9udDw8L0YxIDUgMCBSPj4+Pj4+ZW5kb2JqCjQgMCBvYmo8PC9MZW5ndGggNDQ+PnN0cmVhbQpCVCAvRjEgMTIgVGYgMjAgMTAwIFRkIChSZXZlbnVlIHVwIDQlKSBUaiBFVAplbmRzdHJlYW0gZW5kb2JqCjUgMCBvYmo8PC9UeXBlL0ZvbnQvU3VidHlwZS9UeXBlMS9CYXNlRm9udC9IZWx2ZXRpY2E+PmVuZG9iagp0cmFpbGVyPDwvUm9vdCAxIDAgUj4+CiUlRU9GCg==

Before (7bcc462)

txt file last

  1. POST txt-last.json
  2. 400 litellm.BadRequestError: BedrockException - {"message":"Cache point cannot be inserted after the preceding content block for this model. Please remove the invalid cache point and try again."}

csv file last

  1. POST csv-last.json
  2. The same 400 as for txt

pdf file last

  1. POST pdf-last.json
  2. 200, usage prompt_tokens: 1609, completion_tokens: 14

After (d31d23a)

txt file last

  1. POST txt-last.json
  2. 200, usage prompt_tokens: 91, completion_tokens: 13

csv file last

  1. POST csv-last.json
  2. 200, usage prompt_tokens: 92, completion_tokens: 32

pdf file last

  1. POST pdf-last.json
  2. 200, usage prompt_tokens: 1609, completion_tokens: 13

Unit tests, uv run pytest tests/unit/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py -k cache_point_never_follows -q, give 12 failed, 4 passed on 7bcc462 with only the new tests applied and 16 passed on d31d23a. The whole file gives 705 passed.

Type

🐛 Bug Fix

Caveats (if any)

Medium

Low

  • A cache point straight after a leading non-PDF file is dropped, moving it forward was not attempted
  • Only Claude models were tried, the rule is applied to every Converse model

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Note

Medium Risk
Touches Bedrock Converse prompt assembly and cache breakpoint placement; behavior changes for cached requests with file attachments but is narrowly scoped with thorough tests.

Overview
Fixes Bedrock Converse 400 errors when prompt caching places a cachePoint immediately after a non-PDF document (txt, csv, etc.).

During Bedrock message translation, a new post-processing step reorders or drops cachePoint blocks: they move before trailing non-PDF documents when cacheable content exists earlier, are omitted when only documents lead the message, and stay unchanged after PDFs and plain text. The same logic runs on sync and async Converse message builders.

New parametrized unit tests cover mixed file/text/caching layouts for both code paths.

Reviewed by Cursor Bugbot for commit d31d23a. Bugbot is set up for automated code reviews on this repo. Configure here.

Bedrock Converse rejects a cachePoint that directly follows a txt, csv, md,
html, docx or xlsx document block. Move it ahead of those documents, and drop
it when nothing cacheable precedes them.

Fixes BerriAI#50923
@habonlaci
habonlaci marked this pull request as ready for review November 10, 2026 15:57
@veria-ai

veria-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@habonlaci

Copy link
Copy Markdown
Contributor Author

@greptile

@habonlaci

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit d31d23a. Configure here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Bedrock Converse 400 "Cache point cannot be inserted after the preceding content block" when a cache point follows a non-PDF document block

1 participant