Skip to content

fix: accept Anthropic system field as content-block array#47

Open
mikemolinet wants to merge 1 commit intoKochC:devfrom
mikemolinet:fix/anthropic-system-array
Open

fix: accept Anthropic system field as content-block array#47
mikemolinet wants to merge 1 commit intoKochC:devfrom
mikemolinet:fix/anthropic-system-array

Conversation

@mikemolinet
Copy link
Copy Markdown

Summary

The Anthropic Messages API accepts system as either a string OR an array of content blocks (per https://docs.anthropic.com/en/api/messages). The current /v1/messages handler at index.js:1044-1047 checks typeof body.system === "string" and silently drops the array form — clients sending system per 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 export normalizeAnthropicSystem(system) helper (accepts string or array, filters to type: "text" blocks for the array case, mirrors the existing normalizeAnthropicMessages block-handling style); replace the inline typeof system === "string" check at the /v1/messages call site with the helper
  • index.test.js: add 3 regression tests — array form included in prompt (discriminating), multi-block concatenation, unit-level coverage of the helper's edge cases
  • README.md: one-line clarification in the /v1/messages section to note that system accepts both a string and the content-block array form

Testing

  • npm test — 115 passed (112 existing + 3 new)
  • npm run lint — clean

Notes

Anthropic API spec reference: https://docs.anthropic.com/en/api/messages (system parameter).
Helper's exported-pure-function pattern matches the existing normalizeAnthropicMessages, normalizeMessages, buildSystemPrompt style called out in CONTRIBUTING.md ("Pure functions are exported for testability").

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant