Skip to content

Commit d98bd8f

Browse files
committed
fix: keep system prompt as plain string for Vertex SDK compatibility
The Vertex SDK does not support array-form system prompts with cache_control, unlike the regular Anthropic SDK. This fix ensures the system parameter remains a plain string, applying cache_control only to message content blocks. - Modified system prompt handling to maintain string format - Updated tests to match corrected behavior - Maintains caching functionality for message blocks only Fixes compatibility issue introduced in commit a3d8c0e
1 parent 2c8c140 commit d98bd8f

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

src/api/providers/__tests__/anthropic-vertex.spec.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,7 @@ describe("VertexHandler", () => {
163163
model: "claude-3-5-sonnet-v2@20241022",
164164
max_tokens: 8192,
165165
temperature: 0,
166-
system: [
167-
{
168-
type: "text",
169-
text: "You are a helpful assistant",
170-
cache_control: { type: "ephemeral" },
171-
},
172-
],
166+
system: "You are a helpful assistant", // System remains as plain string
173167
messages: [
174168
{
175169
role: "user",
@@ -364,16 +358,10 @@ describe("VertexHandler", () => {
364358
expect(textChunks[0].text).toBe("Hello")
365359
expect(textChunks[1].text).toBe(" world!")
366360

367-
// Verify cache control was added correctly
361+
// Verify cache control was added correctly - system remains string, only messages have cache_control
368362
expect(mockCreate).toHaveBeenCalledWith(
369363
expect.objectContaining({
370-
system: [
371-
{
372-
type: "text",
373-
text: "You are a helpful assistant",
374-
cache_control: { type: "ephemeral" },
375-
},
376-
],
364+
system: "You are a helpful assistant", // System remains as plain string
377365
messages: [
378366
expect.objectContaining({
379367
role: "user",

src/api/providers/anthropic-vertex.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,22 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
7575
* 1. Maximum of 4 blocks can have cache_control
7676
* 2. Only text blocks can be cached (images and other content types cannot)
7777
* 3. Cache control can only be applied to user messages, not assistant messages
78+
* 4. System prompt must remain as a plain string - Vertex SDK does not support array format
7879
*
7980
* Our caching strategy:
80-
* - Cache the system prompt (1 block)
81+
* - System prompt cannot be cached directly (must remain as plain string)
8182
* - Cache the last text block of the second-to-last user message (1 block)
8283
* - Cache the last text block of the last user message (1 block)
83-
* This ensures we stay under the 4-block limit while maintaining effective caching
84+
* This ensures compatibility with Vertex SDK while maintaining effective caching
8485
* for the most relevant context.
8586
*/
8687
const params: Anthropic.Messages.MessageCreateParamsStreaming = {
8788
model: id,
8889
max_tokens: maxTokens ?? ANTHROPIC_DEFAULT_MAX_TOKENS,
8990
temperature,
9091
thinking,
91-
// Cache the system prompt if caching is enabled.
92-
system: supportsPromptCache
93-
? [{ text: systemPrompt, type: "text" as const, cache_control: { type: "ephemeral" } }]
94-
: systemPrompt,
92+
// System must remain as plain string for Vertex SDK compatibility
93+
system: systemPrompt,
9594
messages: supportsPromptCache ? addCacheBreakpoints(messages) : messages,
9695
stream: true,
9796
}

0 commit comments

Comments
 (0)