Skip to content

fix(memory): merge memory into top-level system field when present - #13549

Closed
KooshaPari wants to merge 1 commit into
cheaper-inference:release/v3.8.51from
KooshaPari:pr/13425-claude-memory-injection
Closed

KooshaPari wants to merge 1 commit into
cheaper-inference:release/v3.8.51from
KooshaPari:pr/13425-claude-memory-injection

Conversation

@KooshaPari

Copy link
Copy Markdown
Contributor

Fixes #13425

Problem

When memory injection is enabled and the body has a top-level system field (Anthropic-shaped requests), injectSystemFirst() unshifted a new {role:'system'} at messages[0]. Anthropic rejects this with 400: use the top-level 'system' parameter for the initial system prompt.

Fix

Check request.system before inspecting messages[0]. When a top-level system field is present (string), merge memory text into it. Fall back to the existing messages-based strategy otherwise.

Changes

  • src/lib/memory/injection.ts: Added top-level system field check in injectSystemFirst()

…ixes cheaper-inference#13425)

When memory injection is enabled and the body has a top-level 'system'
field (Anthropic-shaped requests), injectSystemFirst() unshifted a new
{role:'system'} at messages[0]. Anthropic rejects this with 400:
'use the top-level system parameter for the initial system prompt'.

- Check request.system before inspecting messages[0]
- Merge memory text into the top-level system field when present
- Fall back to the existing messages-based strategy otherwise
Copilot AI lite review requested due to automatic review settings September 13, 2026 10:31

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@diegosouzapw

Copy link
Copy Markdown
Collaborator

Thanks! #13427, also from you, fixes #13425 for both string and block-array system fields, on both injection paths, with tests. It is being merged, so closing this as a duplicate.

diegosouzapw pushed a commit that referenced this pull request Sep 15, 2026
…ifting at messages[0] (#13425) (#13427)

Memory injection merges into an Anthropic-shaped top-level `system` field (string or text-block array) instead of prepending a `role: "system"` message at `messages[0]`, which Anthropic rejects with a 400 (#13425). Handled on both the system-first path (xiaomi-mimo) and the general path. Chosen over #13549, which covered only the string case.

Maintainer fix: widened `ChatRequest.system` to `string | Array<{ type; text?; … }>`. Assigning the block array to the `string`-typed field raised 2× TS2322 under `check:open-sse-typecheck`.

Validated in one consolidated batch of this series (37 PRs boarded together on `release/v3.8.51`): `typecheck:core`, `check:open-sse-typecheck` and `check:dashboard-typecheck` clean; ESLint clean on every changed file; file-size, complexity, cognitive-complexity, changelog-integrity, docs-counts, docs-sync and migration-numbering gates green (only the pre-existing `open-sse/utils/stream.ts` file-size red remains, inherited from the base); 3,743 focused `node:test` cases plus 34 vitest cases green.

Thanks @KooshaPari!
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…ifting at messages[0] (cheaper-inference#13425) (cheaper-inference#13427)

Memory injection merges into an Anthropic-shaped top-level `system` field (string or text-block array) instead of prepending a `role: "system"` message at `messages[0]`, which Anthropic rejects with a 400 (cheaper-inference#13425). Handled on both the system-first path (xiaomi-mimo) and the general path. Chosen over cheaper-inference#13549, which covered only the string case.

Maintainer fix: widened `ChatRequest.system` to `string | Array<{ type; text?; … }>`. Assigning the block array to the `string`-typed field raised 2× TS2322 under `check:open-sse-typecheck`.

Validated in one consolidated batch of this series (37 PRs boarded together on `release/v3.8.51`): `typecheck:core`, `check:open-sse-typecheck` and `check:dashboard-typecheck` clean; ESLint clean on every changed file; file-size, complexity, cognitive-complexity, changelog-integrity, docs-counts, docs-sync and migration-numbering gates green (only the pre-existing `open-sse/utils/stream.ts` file-size red remains, inherited from the base); 3,743 focused `node:test` cases plus 34 vitest cases green.

Thanks @KooshaPari!
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.

fix(claude): memory injection unshifts system message at messages[0] despite top-level system field (400)

3 participants