Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(prompt-cache): repair Bedrock Converse mapping and gate injection by model capability#76

Merged
veerareddyvishal144 merged 2 commits into
Fast-Editor:main from
sumleo:fix/bedrock-converse-and-capability-gate
Jun 18, 2026
Merged

fix(prompt-cache): repair Bedrock Converse mapping and gate injection by model capability #76
veerareddyvishal144 merged 2 commits into
Fast-Editor:main from
sumleo:fix/bedrock-converse-and-capability-gate

Conversation

@sumleo

@sumleo sumleo commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Description

Two related prompt-caching defects on the Bedrock / aggregating-provider paths. invokeModel runs injectPromptCaching(body, provider) before dispatch, which rewrites string system fields and message content into Anthropic cache_control blocks. Two downstream problems follow from that:

  1. Bedrock Converse mapping is broken (src/clients/databricks.js, invokeBedrock). The message map (content: ... c.text ...) copies only c.text, dropping cache_control so Bedrock never actually caches. Worse, the system field is wrapped as converseBody.system = [{ text: bedrockBody.system }] while bedrockBody.system is now an array of blocks — producing system: [{ text: [ ... ] }], which violates the Converse contract (system: [{ text: "<string>" }]) and triggers a ValidationException.

  2. Injection is not gated by model capability (src/clients/prompt-cache-injection.js, needsCacheInjection / injectPromptCaching). needsCacheInjection only checks provider membership in EXPLICIT_CACHE_PROVIDERS. But bedrock and openrouter route to many model families, and most (Llama, Mistral, Titan, Cohere, GPT, ...) don't support cache_control. Markers were injected regardless, yielding request shapes those models reject or silently ignore.

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Performance improvement
  • Refactoring

Changes Made

  • Bedrock Converse shape fix (src/clients/databricks.js): added normalizeBodyForConverse(), called at the top of invokeBedrock before the Converse request is built. It strips cache_control and flattens any array-shaped system / message content back to plain strings (the Converse API has no cache_control concept). It works on a shallow copy, so the caller's body is untouched.
  • Model-capability gate (src/clients/prompt-cache-injection.js): added modelSupportsCacheControl(body, provider) and call it inside injectPromptCaching. It inspects the resolved model id (body._tierModel / body.model) and skips injection for families known not to support cache_control. It is intentionally fail-open: Anthropic-only providers and unknown/absent model ids still inject, so the existing provider-only needsCacheInjection(provider) signature and behavior are preserved.

Testing

  • Existing tests pass (test/prompt-cache-injection.test.js, unchanged assertions still green)
  • Added new tests
    • test/databricks-bedrock-converse.test.js — asserts that after injectPromptCaching(body, 'bedrock') the normalized Converse payload has a string system and { text: string } content blocks.
    • Extended test/prompt-cache-injection.test.js with capability-gate cases (Claude allowed, Llama/Titan/Mistral/Cohere/GPT skipped, unknown model id still injects).
  • Manual testing performed (ran the affected suites locally with the project's node --test runner)
node --test test/prompt-cache-injection.test.js test/databricks-bedrock-converse.test.js
# tests 32 pass 32 fail 0

Checklist

  • Code follows style guidelines (2-space, CommonJS, existing conventions)
  • Documentation updated (no user-facing behavior/config change)
  • Tests added/updated
  • No new warnings (eslint over the touched files reports no new errors)

Notes for reviewers

npm install against the default registry fails in my environment (the lockfile resolves to a private registry I can't reach), so I installed dev deps from the public npm registry to run eslint and the tests; the committed package-lock.json is unchanged. Linting the two touched source files adds zero new errors over the current main baseline.

sumleo added 2 commits June 17, 2026 09:03
invokeModel runs injectPromptCaching() before dispatch, which rewrites a
string `system` field and message `content` into Anthropic cache_control
blocks (arrays of `{ type, text, cache_control }`). The Bedrock Converse
path then copied only `c.text` from message blocks (dropping cache_control,
so Bedrock never caches) and wrapped the now-array `system` as
`{ text: <array> }`, which violates the Converse contract
(`system: [{ text: "<string>" }]`) and triggers a ValidationException.
Normalize the body for Converse before building the request: strip
cache_control and flatten any array-shaped system/content back to plain
strings. The Converse API has no cache_control concept, so this is the
correct shape. Normalization operates on a copy and leaves the caller's
body untouched.
Add test/databricks-bedrock-converse.test.js covering the post-injection
Converse shape.
needsCacheInjection() only checks provider membership, so cache_control
markers were injected for every model behind an explicit-cache provider.
Aggregating providers like bedrock and openrouter route to many model
families, and most (Llama, Mistral, Titan, Cohere, GPT, ...) do not
support cache_control — injecting markers there produces request shapes
the upstream model rejects or ignores.
Add a model-capability check inside injectPromptCaching, which has access
to the resolved model id (body._tierModel / body.model). The check is
fail-open: Anthropic-only providers and unknown/absent model ids still
inject, so the provider-only needsCacheInjection signature stays
backward-compatible. Only families known not to support cache_control are
skipped.
Extend test/prompt-cache-injection.test.js with capability-gate coverage.

sumleo commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Hi @veerareddyvishal144, gentle nudge on this when you have a moment. It's a small, self-contained prompt-caching fix, and I'm happy to rebase or tweak anything if that would make review easier. Thanks for the project and your time!

vishalveerareddy123 reacted with thumbs up emoji

Copy link
Copy Markdown
Collaborator

Hey
Thank you so much for your contribution
Give me sometime I shall review it once

@veerareddyvishal144 veerareddyvishal144 merged commit 09977a5 into Fast-Editor:main Jun 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /