Skip to content

fix(js/plugins/vertexai): send Model Garden Anthropic system parts as text blocks - #7214

Open
abhiramshaji-cyber wants to merge 1 commit into
genkit-ai:mainfrom
abhiramshaji-cyber:fix/vertexai-anthropic-system-join
Open

abhiramshaji-cyber wants to merge 1 commit into
genkit-ai:mainfrom
abhiramshaji-cyber:fix/vertexai-anthropic-system-join

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor

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

toAnthropicRequest in js/plugins/vertexai/src/modelgarden/v2/anthropic.ts (and its copy in legacy/anthropic.ts) turns a Genkit GenerateRequest into the Anthropic Messages request that defineModel sends 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.

  1. Genkit's injectInstructions (js/ai/src/formats/index.ts) pushes the format instructions as a second text part onto the system message.
  2. toAnthropicRequest maps the system parts to their text and calls .join().
  3. .join() with no argument uses ,, so Claude gets system: "You are terse.,Output should be in JSON format...".
  4. If the request has two system messages, system = runs again and only the last one is sent.

After this PR

Same developer, same call.

  1. Same as above through step 1.
  2. toAnthropicRequest pushes every non empty system text part as { type: 'text', text } into one system array, across all system messages.
  3. Claude gets system: [{ type: 'text', text: 'You are terse.' }, { type: 'text', text: 'Output should be in JSON format...' }], no commas added.
  4. A single part system prompt still works: it is sent as one text block.

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:#111827
Loading

toAnthropicRequest focus view
The v2 toAnthropicRequest, called from defineModel, Functions view in Architect

The change

In both v2/anthropic.ts and legacy/anthropic.ts, system is now a TextBlockParam[] 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 builds system in js/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 now part.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

  • No stray commas in the system prompt, and the format instructions stay separate from the user's own system text.
  • Every system message reaches Claude, not just the last one.
  • Single part system prompts behave the same, now sent as one text block instead of a plain string.
  • Media or tool parts in a system message still throw the same error.

I went with text blocks over join('') because join('') 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.ts and tests/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) and pnpm build (tsc) is clean.

Fixes #7167

Checklist:

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor Author

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 google-genai, and #7212 for compat-oai, which fixes #7211), are green and ready whenever someone has a moment. Is there anything else that would help, or anything you'd like changed?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JS] vertexai Model Garden Anthropic joins system prompt parts with commas

1 participant