Repository navigation
bug: preserve cache_control through the Vertex Anthropic chat transform - #1902
Open
bharathvemula wants to merge 1 commit into
Open
bharathvemula wants to merge 1 commit into
bharathvemula wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1579 (chat-completions half).
Problem
When routing to Vertex AI Anthropic models via
/v1/chat/completions, the OpenAI → Anthropic transform insrc/providers/anthropic/chatComplete.ts(AnthropicChatCompleteConfig, spread intoVertexAnthropicChatCompleteConfig) silently droppedcache_controlin four shapes, so Vertex never created a prompt cache and responses showed zerocache_creation_input_tokens/cache_read_input_tokens:cache_control(fell through to the passthrough branch, which only copiedrole+content)cache_control(transformAssistantMessagerebuilt the block as plain text)image_urlcontent items (transformAndAppendImageContentItemonly keptcache_controlfor base64 images)file_urlandfile_data(transformAndAppendFileContentItemnever forwarded it)I verified on current
main(669825c) that the/v1/messagesroute is not affected —VertexAnthropicMessagesConfigpassessystem/messages/toolsthrough verbatim, so block-levelcache_controlalready reaches Vertex'srawPredictthere (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_controlin the four spots above. The caller-supplied object is preserved as-is (includingttl: '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)
transformToProviderRequest('vertex-ai', …)path over 11cache_controlshapes: 4 stripped pre-fix, 0 stripped post-fix.src/providers/anthropic/chatComplete.test.ts(8 tests): 4 fail pre-fix, 8 pass post-fix — including a no-cache_controlnegative test and attlpreservation test, plus gates for the already-working/v1/messagessystem/message blocks.npx jest src/providers/anthropic/chatComplete.test.ts src/providers/google-vertex-ai/utils.test.ts: 26 passed.npx prettier --checkon both touched files: clean.npm run build(rollup): succeeds (one pre-existing TS warning insrc/providers/open-ai-base/index.ts, untouched).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 insrc/utils/env.ts, type error insrc/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-levelcache_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.