Repository navigation
fix(js/plugins/vertexai): send Model Garden Anthropic system parts as text blocks - #7214
abhiramshaji-cyber wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the Anthropic request transformation in both the legacy and v2 Vertex AI Model Garden plugins to format system messages as an array of text block objects instead of a single joined string. It also adds corresponding unit tests to verify that system text parts are correctly preserved as individual blocks and that non-text system parts are rejected. No review comments were provided, and the implementation is clean and well-tested.
|
Hey @MichaelDoyle, hope you're doing well! Thanks for pointing #7218 over here and for assigning #7167 to me, that was really kind. I know the review queue is busy, so no rush at all. This one, plus two tool result fixes (#7210 for |
Claude models on Vertex AI Model Garden were getting stray commas glued into their system prompt, and this PR sends each system part as its own text block instead.
Hi team, hope you're doing well! This follows up on #7167, where I traced it. Huge thanks to the folks behind #7123 and #7124 for spotting the same pattern in Ollama, that's what got me looking here.
What this code is for
toAnthropicRequestinjs/plugins/vertexai/src/modelgarden/v2/anthropic.ts(and its copy inlegacy/anthropic.ts) turns a GenkitGenerateRequestinto the Anthropic Messages request thatdefineModelsends to Model Garden. Every Model Garden Claude call goes through it.Before this PR
A developer calls
ai.generate({ system: 'You are terse.', prompt, output: { schema } })on a Model Garden Claude model.injectInstructions(js/ai/src/formats/index.ts) pushes the format instructions as a second text part onto the system message.toAnthropicRequestmaps the system parts to their text and calls.join()..join()with no argument uses,, so Claude getssystem: "You are terse.,Output should be in JSON format...".system =runs again and only the last one is sent.After this PR
Same developer, same call.
toAnthropicRequestpushes every non empty system text part as{ type: 'text', text }into onesystemarray, across all system messages.system: [{ type: 'text', text: 'You are terse.' }, { type: 'text', text: 'Output should be in JSON format...' }], no commas added.The diagram
flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: one string, parts joined by commas"] direction TB B1["ai.generate with system and output schema"] --> B2["injectInstructions adds a second system part"] B2 --> B3["toAnthropicRequest"] B3 --> B4{"map to text, then .join()"} B4 -->|"two parts"| B5["'You are terse.,Output should be...'"] B4 -->|"one part"| B6["'Talk like a pirate.'"] end subgraph AFTER["AFTER: one text block per part, no commas"] direction TB A1["ai.generate with system and output schema"] --> A2["injectInstructions adds a second system part"] A2 --> A3["toAnthropicRequest"] A3 --> A4{"THE CHANGE: push each part as a text block"} A4 -->|"two parts"| A5["two text blocks, text unchanged"] A4 -->|"one part"| A6["one text block 'Talk like a pirate.'"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827The v2
toAnthropicRequest, called fromdefineModel, Functions view in ArchitectThe change
In both
v2/anthropic.tsandlegacy/anthropic.ts,systemis now aTextBlockParam[]that each system text part is pushed into, and it is set on the request only when it has blocks. This matches how the official anthropic plugin buildssysteminjs/plugins/anthropic/src/runner/base.ts. That is the fix. Because it appends instead of assigning, a second system message no longer replaces the first.Non text system parts still throw
Only text context is supported for system messages.The check is nowpart.text === undefined, so an empty text part is skipped rather than throwing, since the Anthropic API rejects empty text blocks.What changes for a user
I went with text blocks over
join('')becausejoin('')would run"You are terse.Output should be..."together. Would you prefer it a different way, maybe a plain string joined with\n? Happy to switch, and any tips are very welcome. Thanks so much!Tests: updated the existing system message case and added a multi part, multi message case plus a non text rejection case to
tests/modelgarden/v2/anthropic_test.tsandtests/modelgarden/legacy/anthropic_test.ts. The new cases fail on main and pass with the fix; the full vertexai suite passes (112 passed, 0 failed) andpnpm build(tsc) is clean.Fixes #7167
Checklist: