-
Notifications
You must be signed in to change notification settings - Fork 131
fix: always use array content for system messages with block-level cache control #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
de4c04a
fix: convert system message to array content with block-level cache c…
devin-ai-integration[bot] 8b162ae
refactor: always use array format for system messages, no cache_contr…
devin-ai-integration[bot] 9fa3497
chore: update changeset to minor
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@openrouter/ai-sdk-provider": patch | ||
| --- | ||
|
|
||
| Fix system message cache control to use block-level format | ||
|
|
||
| When cache control is specified on a system message via `providerOptions`, the content is now converted to array format with `cache_control` on the text block, matching the existing behavior for user messages. This ensures consistent Anthropic prompt caching behavior across all message types. | ||
|
|
||
| Before (message-level cache_control): | ||
| ```json | ||
| { "role": "system", "content": "...", "cache_control": { "type": "ephemeral" } } | ||
| ``` | ||
|
|
||
| After (block-level cache_control): | ||
| ```json | ||
| { "role": "system", "content": [{ "type": "text", "text": "...", "cache_control": { "type": "ephemeral" } }] } | ||
| ``` | ||
|
|
||
| Fixes #389 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /** | ||
| * Regression test for GitHub issue #389 | ||
| * https://github.com/OpenRouterTeam/ai-sdk-provider/issues/389 | ||
| * | ||
| * Issue: "Anthropic prompt caching not applied when `system` is a string | ||
| * in AI SDK (`ModelMessage[]`); only block content works" | ||
| * | ||
| * The user reported that prompt caching does not work when a system message | ||
| * is provided as a plain string with cache_control at the message level via | ||
| * providerOptions. Caching only worked when content was an array of text | ||
| * blocks with cache_control on each block. | ||
| * | ||
| * The fix converts system message content to array format with block-level | ||
| * cache_control when cache control is present, matching the existing behavior | ||
| * for user messages. | ||
| */ | ||
| import { generateText } from 'ai'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { createOpenRouter } from '@/src'; | ||
|
|
||
| vi.setConfig({ | ||
| testTimeout: 120_000, | ||
| }); | ||
|
|
||
| describe('Issue #389: System message cache control with string content', () => { | ||
| const openrouter = createOpenRouter({ | ||
| apiKey: process.env.OPENROUTER_API_KEY, | ||
| baseUrl: `${process.env.OPENROUTER_API_BASE}/api/v1`, | ||
| }); | ||
|
|
||
| const model = openrouter('anthropic/claude-sonnet-4'); | ||
|
|
||
| const longSystemPrompt = `You are a helpful assistant. Here is some context that should be cached: | ||
| ${Array(50) | ||
| .fill( | ||
| 'This is padding text to ensure the prompt meets the minimum token threshold for Anthropic prompt caching. ' + | ||
| 'Prompt caching requires a minimum number of tokens in the prompt prefix. ' + | ||
| 'This text is repeated multiple times to reach that threshold. ', | ||
| ) | ||
| .join('\n')} | ||
| Remember to be helpful and concise in your responses.`; | ||
|
|
||
| it('should trigger cache write on first request with system message cache control', async () => { | ||
| const response = await generateText({ | ||
| model, | ||
| messages: [ | ||
| { | ||
| role: 'system', | ||
| content: longSystemPrompt, | ||
| providerOptions: { | ||
| anthropic: { | ||
| cacheControl: { type: 'ephemeral' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: 'What is 2+2? Answer with just the number.', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(response.text).toBeDefined(); | ||
| expect(response.text.length).toBeGreaterThan(0); | ||
| expect(response.finishReason).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should trigger cache read on second request with system message cache control', async () => { | ||
| const makeRequest = () => | ||
| generateText({ | ||
| model, | ||
| messages: [ | ||
| { | ||
| role: 'system', | ||
| content: longSystemPrompt, | ||
| providerOptions: { | ||
| anthropic: { | ||
| cacheControl: { type: 'ephemeral' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: 'What is 2+2? Answer with just the number.', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| await makeRequest(); | ||
|
|
||
| const response = await makeRequest(); | ||
|
|
||
| const openrouterMetadata = response.providerMetadata?.openrouter as { | ||
| usage?: { | ||
| promptTokensDetails?: { cachedTokens?: number }; | ||
| }; | ||
| }; | ||
|
|
||
| const cachedTokens = | ||
| openrouterMetadata?.usage?.promptTokensDetails?.cachedTokens; | ||
|
|
||
| expect(cachedTokens).toBeDefined(); | ||
| expect(cachedTokens).toBeGreaterThan(0); | ||
| }); | ||
| }); |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue to rid this union and just use an array for this