Skip to content

fix(guardrails/azure): guard undefined systemMessages in shieldPrompt#1715

Open
ankit25bcs10610 wants to merge 1 commit into
Portkey-AI:mainfrom
ankit25bcs10610:fix/azure-shieldprompt-undefined-guard
Open

fix(guardrails/azure): guard undefined systemMessages in shieldPrompt#1715
ankit25bcs10610 wants to merge 1 commit into
Portkey-AI:mainfrom
ankit25bcs10610:fix/azure-shieldprompt-undefined-guard

Conversation

@ankit25bcs10610

Copy link
Copy Markdown

What

In the Azure shieldPrompt guardrail:

const systemMessages = context.request?.json?.messages?.filter(
  (message) => message.role === 'system'
);
...
// guarded correctly here:
if (Array.isArray(systemMessages) && systemMessages.length > 0) { ... }
...
const request = {
  userPrompt,
  ...(systemMessages.length > 0 ? { documents: textArray } : {}), // NOT guarded
};

systemMessages is undefined whenever the request has no messages array (e.g. a completion-style payload). The documents spread uses a bare systemMessages.length, throwing TypeError: Cannot read properties of undefined (reading 'length') — and this line is outside the surrounding try, so the handler rejects instead of returning a clean verdict.

Fix

Apply the same Array.isArray(systemMessages) guard already used a few lines above.

npm run format:check passes.

`systemMessages` is `context.request?.json?.messages?.filter(...)`, so it
is `undefined` when the request has no `messages` array (e.g. a
completion-style payload). The earlier branch guards this with
`Array.isArray(systemMessages)`, but the `documents` spread used a bare
`systemMessages.length > 0`, throwing
`TypeError: Cannot read properties of undefined (reading 'length')`.

Apply the same `Array.isArray` guard here.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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