Skip to content

bug: preserve cache_control through the Vertex Anthropic chat transform - #1902

Open
bharathvemula wants to merge 1 commit into
Portkey-AI:mainfrom
bharathvemula:fix/1579-vertex-cache-control
Open

bharathvemula wants to merge 1 commit into
Portkey-AI:mainfrom
bharathvemula:fix/1579-vertex-cache-control

Conversation

@bharathvemula

Copy link
Copy Markdown

Fixes #1579 (chat-completions half).

Problem

When routing to Vertex AI Anthropic models via /v1/chat/completions, the OpenAI → Anthropic transform in src/providers/anthropic/chatComplete.ts (AnthropicChatCompleteConfig, spread into VertexAnthropicChatCompleteConfig) silently dropped cache_control in four shapes, so Vertex never created a prompt cache and responses showed zero cache_creation_input_tokens / cache_read_input_tokens:

  • string-content user messages with message-level cache_control (fell through to the passthrough branch, which only copied role + content)
  • string-content assistant messages with message-level cache_control (transformAssistantMessage rebuilt the block as plain text)
  • URL image_url content items (transformAndAppendImageContentItem only kept cache_control for base64 images)
  • file content items, both file_url and file_data (transformAndAppendFileContentItem never forwarded it)

I verified on current main (669825c) that the /v1/messages route is not affected — VertexAnthropicMessagesConfig passes system/messages/tools through verbatim, so block-level cache_control already reaches Vertex's rawPredict there (Google's docs confirm the marker lives in the JSON payload, no beta header needed). That path is locked in by regression tests below rather than changed.

Fix

Forward cache_control in the four spots above. The caller-supplied object is preserved as-is (including ttl: '1h'), matching the direction of #1592's base64-image hunk; the pre-existing { type: 'ephemeral' } normalisation in the other branches is left untouched.

Verification (all on current main, before/after)

  • Standalone repro driving the real transformToProviderRequest('vertex-ai', …) path over 11 cache_control shapes: 4 stripped pre-fix, 0 stripped post-fix.
  • New src/providers/anthropic/chatComplete.test.ts (8 tests): 4 fail pre-fix, 8 pass post-fix — including a no-cache_control negative test and a ttl preservation test, plus gates for the already-working /v1/messages system/message blocks.
  • npx jest src/providers/anthropic/chatComplete.test.ts src/providers/google-vertex-ai/utils.test.ts: 26 passed.
  • npx prettier --check on both touched files: clean. npm run build (rollup): succeeds (one pre-existing TS warning in src/providers/open-ai-base/index.ts, untouched).
  • No live Vertex call was made, so cache-token counts against the real endpoint are not verified here — the unit-level transform output is the evidence that the marker now reaches rawPredict.

Note for reviewers: the test mocks out the providers index (jest.mock('../../providers', …)) because importing it under this repo's jest setup currently fails on pre-existing issues (top-level await in src/utils/env.ts, type error in src/providers/bytez/api.ts). The transform harness and the Vertex configs under test are the real modules.

Related: #1592 (anthropic-beta header / cache_control.scope, direct Anthropic) and #1862 (Bedrock) touch adjacent code but do not fix the Vertex chat path; #1593 (top-level cache_control) was closed as already supported and is not re-done here.

AI assistance disclosure

This PR was prepared with AI assistance (code drafted with an AI coding agent, then verified by running the repro, tests, formatter, and build above); the author reviewed the final diff before submitting.

Fixes Portkey-AI#1579 (chat-completions half): the OpenAI -> Anthropic transform in
src/providers/anthropic/chatComplete.ts silently dropped cache_control in
four shapes, so requests routed to Vertex AI Anthropic models never created
a prompt cache (zero cache_creation/cache_read tokens):

- string-content user messages with message-level cache_control
- string-content assistant messages with message-level cache_control
- URL image_url content items
- file content items (both file_url and file_data)

The /v1/messages route already passes system/messages through verbatim;
regression tests lock that in too. The fix preserves the caller-supplied
cache_control object as-is (including ttl), matching the direction of
Portkey-AI#1592's base64-image hunk; the pre-existing { type: 'ephemeral' }
normalisation elsewhere is left untouched.

Tests: 7 new regression tests in src/providers/anthropic/chatComplete.test.ts
(4 fail pre-fix, 7 pass post-fix); existing Vertex utils suite still green;
prettier clean; rollup build succeeds.

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.

cache_control blocks stripped when routing to Vertex AI Anthropic models

1 participant