Skip to content

New "Add Prefix" Guardrail #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

New "Add Prefix" Guardrail #1258

wants to merge 6 commits into from

Conversation

vrushankportkey
Copy link
Collaborator

@vrushankportkey vrushankportkey commented Jul 31, 2025

✅ Testing Done

Example Requests

from portkey_ai import Portkey

client = Portkey(
    provider="openai",
    Authorization="your-openai-key",
    config={
        "before_request_hooks": [{
            "type": "guardrail",
            "id": "helpful_assistant",
            "checks": [{
                "id": "default.addPrefix",
                "parameters": {
                    "prefix": "You are a pirate!"
                }
            }]
        }]
    }
)

response = client.chat.completions.create(
    model="gpt-4.1-nano",
    messages=[{"role": "user", "content": "Hello!"}]
)

Response:

Arrr, matey! AI, or Artificial Intelligence, be like a crew o’ clever swabbies built by landlubbers to think and learn like us! It’s a fancy fancy way of makin’ machines and computers that can see, hear, decide, and even chat with ye — all without a waterlogged brain of their own. Yarrr, they’re like the crafty first mates that help us navigate the digital seas!

📋 Configuration Options

Parameter Type Default Description
prefix str required Text to prepend
applyToRole str "user" Target: "user", "system", "assistant"
addToExisting bool true Modify existing vs create new message
onlyIfEmpty bool false Only apply if role doesn't exist

Copy link

matter-code-review bot commented Jul 31, 2025

Code Quality bug fix reliability

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

The addPrefix plugin's error handling in plugins/default/addPrefix.ts has been improved. Previously, caught errors were assigned directly. Now, errors are wrapped in a structured object, including a user-friendly message and the original error object, after deleting the stack trace.

🔍 Impact of the Change

This change enhances the clarity and debuggability of errors originating from the addPrefix plugin. By providing a structured error object with a clear message and the original error, it becomes easier to diagnose issues without exposing sensitive stack traces directly.

📁 Total Files Changed

  • plugins/default/addPrefix.ts: Modified the error handling within the catch block to provide a more structured error object (4 additions, 1 deletion).

🧪 Test Added

No new tests were added in this Pull Request. The existing test file plugins/default/addPrefix.test.ts was removed in a previous change, resulting in a loss of 450 lines of test coverage for the addPrefix guardrail.

🔒Security Vulnerabilities

No new security vulnerabilities were introduced. The change improves error reporting, which can indirectly aid in debugging and identifying potential issues more efficiently.

Motivation

To provide more informative and structured error messages from the addPrefix plugin, improving the debugging experience and clarity for users encountering issues.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

N/A

Tip

Quality Recommendations

  1. Consider adding a specific log entry for the originalError to aid in debugging, especially if e.stack is being deleted.

  2. Ensure that the addPrefix plugin has adequate unit and integration test coverage, especially given previous changes that removed tests.

Tanka Poem ♫

Error caught with care,
Message clear, stack now hidden,
Debugging's new dawn.
Prefix guardrail stands robust,
Code's wisdom, a gentle hum. ✨

Sequence Diagram

sequenceDiagram
    participant PH as PluginHandler
    participant IPL as InternalPluginLogic

    PH->>IPL: executePluginLogic(data)
    alt Error Occurs
        IPL--xPH: throws Error (e)
        PH->>PH: catch(e)
        Note over PH: delete e.stack
        PH->>PH: formatError(e.message, e)
        PH-->>PH: error = { message, originalError }
    else Success
        IPL-->>PH: result
    end
    PH-->>Caller: { error, verdict, data, transformedData, transformed }
Loading

Copy link

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

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

This PR implements a well-structured and thoroughly tested "Add Prefix" guardrail plugin. The code is clean, well-organized, and includes comprehensive test coverage. I've identified a few minor improvements that could enhance error handling and code clarity.

Skipped files
  • plugins/default/manifest.json: Skipped file pattern

Comment on lines 164 to 170
newTransformedData = addPrefixToChatCompletion(
context,
prefix,
parameters.applyToRole || 'user',
parameters.addToExisting !== false, // default to true
parameters.onlyIfEmpty === true // default to false
);

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: The default value for addToExisting is set in two different places (function parameter and when calling the function), which could lead to confusion.
Fix: Standardize how defaults are handled by setting them in one place.
Impact: Improves code maintainability and reduces the risk of inconsistent behavior if one default is changed but not the other.

Suggested change
newTransformedData = addPrefixToChatCompletion(
context,
prefix,
parameters.applyToRole || 'user',
parameters.addToExisting !== false, // default to true
parameters.onlyIfEmpty === true // default to false
);
newTransformedData = addPrefixToChatCompletion(
context,
prefix,
parameters.applyToRole || 'user',
parameters.addToExisting === undefined ? true : parameters.addToExisting,
parameters.onlyIfEmpty === true // default to false
);

Comment on lines 37 to 42
const json = context.request.json;
const updatedJson = { ...json };
const messages = [...json.messages];

// Find the target role message
const targetIndex = messages.findIndex((msg) => msg.role === applyToRole);

Choose a reason for hiding this comment

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

⚡️ Performance Improvement

Issue: The code creates a new array with [...json.messages] even when it might not be necessary (if the request is rejected early).
Fix: Move the array creation after the validation checks to avoid unnecessary object creation.
Impact: Slightly improves performance by avoiding unnecessary array copying when the operation won't proceed.

Suggested change
const json = context.request.json;
const updatedJson = { ...json };
const messages = [...json.messages];
// Find the target role message
const targetIndex = messages.findIndex((msg) => msg.role === applyToRole);
const json = context.request.json;
const updatedJson = { ...json };
// Find the target role message
const targetIndex = json.messages.findIndex((msg) => msg.role === applyToRole);
// Create a copy of the messages array only if we're going to modify it
const messages = [...json.messages];

@narengogi narengogi requested a review from VisargD August 4, 2025 09:02
narengogi
narengogi previously approved these changes Aug 4, 2025
Copy link
Collaborator

@narengogi narengogi left a comment

Choose a reason for hiding this comment

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

looks good to me, please add testing done and some sample curls @vrushankportkey

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Co-authored-by: matter-code-review[bot] <150888575+matter-code-review[bot]@users.noreply.github.com>
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

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.

2 participants