Skip to content

Conversation

siddharthsambharia-portkey
Copy link
Contributor

Description

This PR adds a new guardrail regex replace to portkey's default gurardrail

Motivation

the ability to replace text while running a regex guardrail on both input and output

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

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

Fixes and Cloaes #1291

Copy link
Contributor

matter-code-review bot commented Sep 1, 2025

Code Quality new feature

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This PR introduces comprehensive support for multi-modal content, specifically image data, within the Google and Google Vertex AI chat completion providers. Key changes include adding an inlineData type definition to content parts, enabling the transformation of modalities parameters into responseModalities for generationConfig, and implementing logic to parse inlineData (base64 encoded images) into image_url content blocks for both incoming requests and streaming responses. This allows the gateway to correctly handle and relay image inputs and outputs to and from Google's multi-modal models.

🔍 Impact of the Change

This feature significantly enhances the capabilities of the Google and Google Vertex AI providers by enabling them to process and generate multi-modal content, primarily images. It allows for richer, more dynamic AI interactions where users can provide images as part of their prompts and potentially receive image-based responses, aligning the gateway with advanced multi-modal AI model functionalities. This expands the range of use cases and improves the overall user experience for applications leveraging these providers.

📁 Total Files Changed

  • src/providers/google-vertex-ai/chatComplete.ts: Modified to include modalities in transformGenerationConfig and handle inlineData for image content in request/response parsing.
  • src/providers/google-vertex-ai/transformGenerationConfig.ts: Updated to map params.modalities to generationConfig['responseModalities'].
  • src/providers/google-vertex-ai/types.ts: Added inlineData type definition for multi-modal content parts.
  • src/providers/google/chatComplete.ts: Similar modifications to its Vertex AI counterpart, adding modalities handling and inlineData parsing for image content.

🧪 Test Added

N/A

🔒Security Vulnerabilities

No new security vulnerabilities are directly introduced by these changes, assuming the base64 encoded image data (inlineData) is handled securely by the underlying Google APIs and that mimeType validation is sufficient to prevent unexpected content types.

Motivation

To enable multi-modal capabilities (image input/output) for Google and Google Vertex AI chat completion providers, enhancing their functionality.

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

.

Tip

Quality Recommendations

  1. Add unit and integration tests specifically for the new multi-modal input and output parsing logic to ensure robustness and correct handling of various image formats and edge cases.

  2. Implement explicit validation for inlineData.mimeType to ensure only expected and supported image types are processed, preventing potential issues with malformed or unsupported data.

  3. Consider adding comprehensive error handling for cases where inlineData might be malformed or missing required fields during parsing, providing clearer error messages.

Tanka Poem ♫

Pixels now flow free,
Models see, then they reply,
Data's rich new form.
Gateway's code, a bridge built,
Vision's dawn, a new frontier. 🚀

Sequence Diagram

sequenceDiagram
    participant User as User
    participant Gateway as Portkey Gateway
    participant GoogleAPI as Google/Vertex AI API

    User->>+Gateway: POST /chat/completions (text, image_url, modalities)
    Note over Gateway: Parse request body
    Gateway->>Gateway: transformGenerationConfig(params)
    Note over Gateway: Map modalities to responseModalities
    Gateway->>Gateway: Process content_blocks
    alt Image Data Present (inlineData)
        Gateway->>GoogleAPI: POST /v1/models/gemini:generateContent
        Note over GoogleAPI: Request with generationConfig, content parts (text, inlineData)
        GoogleAPI-->>-Gateway: 200 OK (response with content parts)
        Gateway->>Gateway: Parse API response
        alt Response has inlineData
            Gateway->>Gateway: Convert inlineData to image_url content_block
        else Response has text/functionCall
            Gateway->>Gateway: Process text/functionCall content_block
        end
        Gateway-->>-User: 200 OK (multi-modal response)
    else Only Text Content
        Gateway->>GoogleAPI: POST /v1/models/gemini:generateContent
        Note over GoogleAPI: Request with generationConfig, content parts (text)
        GoogleAPI-->>-Gateway: 200 OK (response with content parts)
        Gateway->>Gateway: Parse API response
        Gateway-->>-User: 200 OK (text response)
    end
Loading

Copy link
Contributor

@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.

New regex replace plugin implementation looks solid with proper error handling and transformation logic. Minor improvements needed for type safety and performance.

) => {
let error = null;
let verdict = true;
let data: any = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

🐛 Bug Fix

Issue: Using any type weakens type safety and can lead to runtime errors
Fix: Use proper typing for the data object
Impact: Prevents potential runtime type errors and improves code maintainability

Suggested change
let data: any = null;
let data: { regexPattern?: string; verdict?: boolean; explanation?: string } | null = null;

Comment on lines +34 to +41
if (!regexPattern) {
throw new Error('Missing regex pattern');
}
if (!content) {
throw new Error('Missing text to match');
}

const regex = new RegExp(regexPattern, 'g');
Copy link
Contributor

Choose a reason for hiding this comment

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

⚡️ Performance Improvement

Issue: Creating new RegExp instance inside the handler for every execution is inefficient
Fix: Move regex creation after validation but before the loop to avoid recreation
Impact: Reduces object allocation overhead and improves performance for multiple text processing

Suggested change
if (!regexPattern) {
throw new Error('Missing regex pattern');
}
if (!content) {
throw new Error('Missing text to match');
}
const regex = new RegExp(regexPattern, 'g');
if (!regexPattern) {
throw new Error('Missing regex pattern');
}
if (!content) {
throw new Error('Missing text to match');
}
const regex = new RegExp(regexPattern, 'g');

Comment on lines +55 to +58
const matches = text.match(regex);
if (matches && matches.length > 0) {
hasMatches = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🐛 Bug Fix

Issue: The match() method already returns null when no matches are found, making the length check redundant
Fix: Simplify the condition to just check for matches existence
Impact: Cleaner code and slightly better performance by avoiding unnecessary array length check

Suggested change
const matches = text.match(regex);
if (matches && matches.length > 0) {
hasMatches = true;
}
const matches = text.match(regex);
if (matches) {
hasMatches = true;
}

Comment on lines +79 to +81
explanation: transformed
? `Pattern '${regexPattern}' matched and was replaced with '${redactText}'`
: `The regex pattern '${regexPattern}' did not match any text.`,
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: The explanation message construction could be more readable and maintainable
Fix: Use template literals consistently and improve readability
Impact: Better code maintainability and consistent string formatting

Suggested change
explanation: transformed
? `Pattern '${regexPattern}' matched and was replaced with '${redactText}'`
: `The regex pattern '${regexPattern}' did not match any text.`,
explanation: transformed
? `Pattern '${regexPattern}' matched and was replaced with '${redactText}'`
: `The regex pattern '${regexPattern}' did not match any text.`,

Copy link
Contributor

Code adds modalities support and inline data handling for Google Vertex AI. Implementation looks solid with proper type definitions and consistent patterns across files.

@VisargD VisargD merged commit 425ea8a into Portkey-AI:main Sep 2, 2025
1 check passed
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