Repository navigation
fix(py/anthropic): keep cache_control on system prompt parts - #159
Merged
huangjeff5 merged 1 commit intoDec 1, 2026
Merged
Conversation
_extract_system flattened system text into a plain string and dropped metadata.cache_control, so prompt caching never applied to system parts. When any system text part carries cache_control, send text blocks (with the marker) instead; the JSON fallback instruction becomes a trailing block. Without cache_control the string form is unchanged. Fixes genkit-ai#149
huangjeff5
approved these changes
Nov 30, 2026
huangjeff5
left a comment
Collaborator
There was a problem hiding this comment.
Verified unit tests in genkit-anthropic (209 passed). Preserves cache_control on system prompt parts as text blocks and safely appends the JSON output instruction when system is a list. Looks good!
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 #149
AnthropicModel._extract_systemjoined the system text into a plain string and discardedmetadata.cache_control, so prompt caching could never be applied to system parts, unlike user/model parts handled in_to_anthropic_messages.Fix: when any system text part carries
cache_control,_extract_systemreturns a list of text blocks (marker attached viaget_cache_control). The JSON-fallback instruction in_build_paramsis then appended as its own trailing text block. Withoutcache_controlthe existing string form is unchanged, so current behaviour and tests stay as they are.Test:
test_extract_system_keeps_cache_control_on_system_partsandtest_generate_claude_sends_system_cache_controlfail onmainand pass with the change; the fullpackages/genkit-anthropic/testsrun is green apart fromtest_plugin_without_key_and_no_tenant_key_raises_naming_both, which also fails onmainin this environment.Disclosure: this change was prepared with AI assistance (Claude Code).