fix: accept Anthropic system field as content-block array#47
Open
mikemolinet wants to merge 1 commit intoKochC:devfrom
Open
fix: accept Anthropic system field as content-block array#47mikemolinet wants to merge 1 commit intoKochC:devfrom
mikemolinet wants to merge 1 commit intoKochC:devfrom
Conversation
The Anthropic Messages API accepts the top-level `system` field as either a string OR an array of content blocks (per https://docs.anthropic.com/en/api/messages). The /v1/messages handler at index.js:1044-1047 only checks `typeof body.system === "string"` and silently drops the array form. Clients that follow the spec see their system prompt ignored by the proxy. Add and export a `normalizeAnthropicSystem` helper that accepts either form: for the array form, concatenates `type: "text"` content blocks (skipping falsy entries, non-text types, and non-string texts); returns null when no usable text is present so the call site can skip adding an empty system message. Use it at the call site in place of the inline string check. Adds 3 regression tests in index.test.js covering: - array-form system reaches buildSystemPrompt (discriminating) - multi-block text arrays are concatenated - helper edge cases (null/undefined, empty strings, non-text blocks, non-string/non-array inputs) Closes KochC#46
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The Anthropic Messages API accepts
systemas either a string OR an array of content blocks (per https://docs.anthropic.com/en/api/messages). The current/v1/messageshandler atindex.js:1044-1047checkstypeof body.system === "string"and silently drops the array form — clients sendingsystemper the spec see their system prompt ignored by the proxy.Add an exported
normalizeAnthropicSystem()helper that accepts both forms, concatenating text content blocks when the array form is used. Use it at the call site.Closes #46
Changes
index.js: add and exportnormalizeAnthropicSystem(system)helper (accepts string or array, filters totype: "text"blocks for the array case, mirrors the existingnormalizeAnthropicMessagesblock-handling style); replace the inlinetypeof system === "string"check at the/v1/messagescall site with the helperindex.test.js: add 3 regression tests — array form included in prompt (discriminating), multi-block concatenation, unit-level coverage of the helper's edge casesREADME.md: one-line clarification in the/v1/messagessection to note thatsystemaccepts both a string and the content-block array formTesting
npm test— 115 passed (112 existing + 3 new)npm run lint— cleanNotes
Anthropic API spec reference: https://docs.anthropic.com/en/api/messages (
systemparameter).Helper's exported-pure-function pattern matches the existing
normalizeAnthropicMessages,normalizeMessages,buildSystemPromptstyle called out in CONTRIBUTING.md ("Pure functions are exported for testability").