gateway icon indicating copy to clipboard operation
gateway copied to clipboard

Fix/support provider specific fields for bedrock in snake case

Open narengogi opened this issue 7 months ago β€’ 1 comments

Code Quality bug fix

Author Description

So, portkey node SDK converts input parameters in chat.completions requests into snake_case, when I originally added transforms for additionalModelRequestFields, it was in camelCase, it was tested with the REST API and the python sdk, but not the node sdk

Summary By MatterAI

πŸ”„ What Changed

This PR adds support for snake_case versions of provider-specific fields for AWS Bedrock in addition to the existing camelCase versions. The change was necessary because the Portkey Node SDK converts all input parameters in chat.completions requests into snake_case format, which was causing issues with the additionalModelRequestFields and other parameters.

πŸ” Impact of the Change

This change ensures compatibility with the Portkey Node SDK by supporting both camelCase and snake_case parameter formats. It maintains backward compatibility while fixing the issue with the Node SDK integration.

πŸ“ Total Files Changed

2 files changed with 46 additions and 4 deletions:

  • src/providers/bedrock/chatComplete.ts: +34, -0
  • src/providers/bedrock/utils.ts: +12, -4

πŸ§ͺ Test Added

N/A

πŸ”’ Security Vulnerabilities

No security vulnerabilities introduced.

Type of Change

  • [x] 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
  • [x] Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • [x] My code follows the style guidelines of this project
  • [x] 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
  • [x] 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

Issue with Portkey Node SDK converting parameters to snake_case

Quality Recommendations

  1. Consider adding unit tests to verify the snake_case parameter handling works correctly

  2. Add documentation comments to explain why both camelCase and snake_case versions are supported

  3. Consider creating a utility function to handle the duplicate parameter checking pattern that appears in multiple transform functions

Sequence Diagram

sequenceDiagram
    participant Client
    participant PortkeyNodeSDK
    participant Gateway
    participant BedrockProvider
    participant AWSBedrock
    
    Client->>PortkeyNodeSDK: chat.completions request
    Note over PortkeyNodeSDK: Converts parameters to snake_case
    PortkeyNodeSDK->>Gateway: Forward request with snake_case params
    Gateway->>BedrockProvider: Process request
    Note over BedrockProvider: Now supports both camelCase and snake_case:
    Note over BedrockProvider: - additionalModelRequestFields
    Note over BedrockProvider: - additional_model_request_fields
    Note over BedrockProvider: - guardrailConfig
    Note over BedrockProvider: - guardrail_config
    BedrockProvider->>BedrockProvider: Transform parameters
    Note over BedrockProvider: transformAdditionalModelRequestFields()
    Note over BedrockProvider: transformAnthropicAdditionalModelRequestFields()
    Note over BedrockProvider: transformCohereAdditionalModelRequestFields()
    Note over BedrockProvider: transformAI21AdditionalModelRequestFields()
    BedrockProvider->>AWSBedrock: Send properly formatted request
    AWSBedrock->>BedrockProvider: Return response
    BedrockProvider->>Gateway: Process response
    Gateway->>PortkeyNodeSDK: Return formatted response
    PortkeyNodeSDK->>Client: Return final response

narengogi avatar Apr 29 '25 10:04 narengogi

So, portkey node SDK converts input parameters in chat.completions requests into snake_case, when I originally added transforms for additionalModelRequestFields, it was in camelCase, it was tested with the REST API and the python sdk, but not the node sdk

narengogi avatar Apr 29 '25 10:04 narengogi

Code Quality bug detected type: bug fix

Summary By MatterAI MatterAI logo

πŸ”„ What Changed

  • Updated BedrockChatCompletionsParams interface to include snake_case versions of additionalModelRequestFields and guardrailConfig.
  • Added corresponding configuration entries in BedrockConverseChatCompleteConfig and provider-specific configs (Anthropic, Cohere, AI21) to handle the new snake_case parameters.
  • Modified transformation functions in src/providers/bedrock/utils.ts (transformAdditionalModelRequestFields, transformAnthropicAdditionalModelRequestFields, etc.) to prioritize camelCase but fall back to snake_case versions when initializing additionalModelRequestFields.
  • Refactored the configuration for anthropic_beta (specifically for Anthropic models on Bedrock) to route it through the transformAnthropicAdditionalModelRequestFields function, treating it as part of the additional model request fields.

πŸ” Impact of the Change

  • Enables users interacting with the Gateway via SDKs that automatically convert parameters to snake_case (like portkey-node-sdk) to successfully pass Bedrock-specific fields (additionalModelRequestFields, guardrailConfig, anthropic_beta, etc.).
  • Increases flexibility by supporting both camelCase and snake_case for these specific parameters.
  • Introduces redundancy in interface definitions and configuration mapping.
  • Potentially introduces a bug where anthropic_beta might not be correctly included in the final request payload under certain conditions due to the refactoring.

πŸ“ Total Files Changed

  • 2 files changed (src/providers/bedrock/chatComplete.ts, src/providers/bedrock/utils.ts).

πŸ§ͺ Test Added

  • N/A (No specific tests mentioned or added in the provided PR data).

πŸ”’Security Vulnerabilities

  • N/A (No security vulnerabilities detected in the changed code).

Description

See Summary By MatterAI above.

Motivation

To support Bedrock provider-specific fields (like additionalModelRequestFields, guardrailConfig, anthropic_beta) when passed in snake_case, primarily due to portkey-node-sdk converting all parameters to snake_case.

Type of Change

  • [x] 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
  • [x] Refactoring (no functional changes)

How Has This Been Tested?

  • [ ] Unit Tests
  • [ ] Integration Tests
  • [x] Manual Testing (Implied based on PR description mentioning testing with REST API and Python SDK previously, and this fix addressing Node SDK)

Screenshots (if applicable)

N/A

Checklist

  • [x] My code follows the style guidelines of this project
  • [x] 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
  • [x] My changes generate no new warnings
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes

Related Issues

N/A

Sequence Diagram

sequenceDiagram
    participant SDK as SDK (e.g., portkey-node-sdk)
    participant Gateway as Gateway API
    participant BedrockProvider as src/providers/bedrock/chatComplete.ts
    participant BedrockUtils as src/providers/bedrock/utils.ts
    participant BedrockAPI as AWS Bedrock API

    SDK->>Gateway: POST /v1/chat/completions (payload with snake_case params, e.g., additional_model_request_fields, guardrail_config, anthropic_beta)
    Gateway->>BedrockProvider: mapRequestParams(payload)
    Note over BedrockProvider: Interface BedrockChatCompletionsParams now accepts both camelCase and snake_case variants (e.g., additionalModelRequestFields, additional_model_request_fields).
    Note over BedrockProvider: Config objects (e.g., BedrockConverseChatCompleteConfig) now have entries for snake_case params, mapping them to internal logic/transforms.
    BedrockProvider->>BedrockUtils: transformAdditionalModelRequestFields(params)
    BedrockUtils->>BedrockUtils: Initialize additionalModelRequestFields = params.additionalModelRequestFields || params.additional_model_request_fields || {}
    BedrockUtils->>BedrockProvider: Return transformedFields
    Note over BedrockProvider: For anthropic_beta, config now points to transformAnthropicAdditionalModelRequestFields.
    BedrockProvider->>BedrockUtils: transformAnthropicAdditionalModelRequestFields(params)
    BedrockUtils->>BedrockUtils: Initialize additionalModelRequestFields = params.additionalModelRequestFields || params.additional_model_request_fields || {}
    BedrockUtils->>BedrockUtils: Add top_k, top_p if present in params
    Note over BedrockUtils: Potential issue: anthropic_beta from top-level params might not be merged here.
    BedrockUtils->>BedrockProvider: Return transformed Anthropic fields
    BedrockProvider->>BedrockAPI: InvokeModel / Converse API call (with transformed parameters)
    BedrockAPI->>BedrockProvider: Response
    BedrockProvider->>Gateway: Formatted Response
    Gateway->>SDK: Response

matter-code-review[bot] avatar May 05 '25 06:05 matter-code-review[bot]

Code Quality bug fix

Summary By MatterAI MatterAI logo

πŸ”„ What Changed

  • Updated Bedrock provider interfaces and configurations (src/providers/bedrock/chatComplete.ts) to accept provider-specific parameters (like additionalModelRequestFields, guardrailConfig) in both camelCase and snake_case.
  • Modified utility functions (src/providers/bedrock/utils.ts) to correctly retrieve these parameters regardless of the case used in the input, prioritizing camelCase if both are provided.
  • Adjusted the mapping for anthropic_beta to be handled via additionalModelRequestFields for Anthropic models on Bedrock.

πŸ” Impact of the Change

  • Ensures compatibility with clients like portkey-node-sdk that automatically convert request parameters to snake_case.
  • Allows users to send Bedrock-specific parameters using either naming convention without errors.
  • Maintains backward compatibility for clients sending camelCase parameters.

πŸ“ Total Files Changed

  • 2 files changed (src/providers/bedrock/chatComplete.ts, src/providers/bedrock/utils.ts)

πŸ§ͺ Test Added

  • N/A (Based on PR description, testing appears to have been manual or via existing integration tests. No new specific unit tests were mentioned for the case handling logic.)

πŸ”’Security Vulnerabilities

  • No security vulnerabilities detected in the changes.

Motivation

So, portkey node SDK converts input parameters in chat.completions requests into snake_case, when I originally added transforms for additionalModelRequestFields, it was in camelCase, it was tested with the REST API and the python sdk, but not the node sdk

Type of Change

  • [x] 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
  • [x] Manual Testing (Implied from PR description context)

Screenshots (if applicable)

N/A

Checklist

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

Related Issues

N/A

Sequence Diagram

sequenceDiagram
    participant Client as Client (e.g., portkey-node-sdk)
    participant Gateway as Portkey Gateway
    participant BedrockProvider as Bedrock Provider Logic
    participant BedrockAPI as AWS Bedrock API

    Client->>Gateway: POST /v1/chat/completions (payload with snake_case params like additional_model_request_fields, guardrail_config)
    Gateway->>BedrockProvider: processChatComplete(payload)
    Note over Gateway,BedrockProvider: Config maps snake_case params (e.g., additional_model_request_fields) to internal camelCase (additionalModelRequestFields)
    BedrockProvider->>BedrockProvider: transform*AdditionalModelRequestFields(params)
    Note over BedrockProvider: Util function checks for params.camelCase || params.snake_case || {}
    BedrockProvider->>BedrockProvider: Construct Bedrock API Request (using transformed params)
    BedrockProvider->>BedrockAPI: InvokeModel / Converse API Call
    BedrockAPI-->>BedrockProvider: API Response
    BedrockProvider-->>Gateway: Formatted Response
    Gateway-->>Client: Final Response

matter-code-review[bot] avatar May 05 '25 06:05 matter-code-review[bot]