Skip to content

fix(py/anthropic): keep cache_control on system prompt parts - #159

Merged
huangjeff5 merged 1 commit into
genkit-ai:mainfrom
pcbeingused33:fix/anthropic-system-cache-control
Dec 1, 2026
Merged

huangjeff5 merged 1 commit into
genkit-ai:mainfrom
pcbeingused33:fix/anthropic-system-cache-control

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Fixes #149

AnthropicModel._extract_system joined the system text into a plain string and discarded metadata.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_system returns a list of text blocks (marker attached via get_cache_control). The JSON-fallback instruction in _build_params is then appended as its own trailing text block. Without cache_control the existing string form is unchanged, so current behaviour and tests stay as they are.

Test: test_extract_system_keeps_cache_control_on_system_parts and test_generate_claude_sends_system_cache_control fail on main and pass with the change; the full packages/genkit-anthropic/tests run is green apart from test_plugin_without_key_and_no_tenant_key_raises_naming_both, which also fails on main in this environment.


Disclosure: this change was prepared with AI assistance (Claude Code).

_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 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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!

@huangjeff5
huangjeff5 enabled auto-merge (squash) November 30, 2026 21:22
@huangjeff5
huangjeff5 merged commit 9aa731a into genkit-ai:main Dec 1, 2026
20 checks passed
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 2026
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.

py/anthropic: cache_control on system parts is dropped

2 participants