Skip to content

runGuardrails ignores provided OpenAI client and requires OPENAI_API_KEY environment variable #21

@gocar-hunghoang

Description

@gocar-hunghoang

Summary

The runGuardrails function from @openai/guardrails ignores the OpenAI client provided in the context.guardrailLlm parameter and instead tries to create its own client using the OPENAI_API_KEY environment variable.

Environment

  • Package: @openai/guardrails Version: 0.1.2(latest)
  • Node.js version: v22.17.0
  • Operating System: macOS

Expected Behavior

When calling runGuardrails(text, config, context, raiseGuardrailErrors) with a context object containing guardrailLlm (an initialized OpenAI client), the function should use that client for all OpenAI API calls.

Actual Behavior

The function ignores the provided context.guardrailLlm client and attempts to create its own OpenAI client, which fails when OPENAI_API_KEY environment variable is not set.

Minimal Reproduction Example

const { OpenAI } = require('openai');
const { runGuardrails } = require('@openai/guardrails');

const client = new OpenAI({
    apiKey: 'sk-xxx'
});

const guardrailsConfig = {
    guardrails: [
        {
            name: 'Moderation',
            config: {
                categories: ['sexual/minors']
            }
        }
    ]
};

const context = {
    guardrailLlm: client
};

const testText = 'Hello, this is a test message for guardrails.';
runGuardrails(testText, guardrailsConfig, context, true).then(output => {
    console.log('runGuardrails completed successfully');
}).catch(error => {
    console.error(error);
    // ❌ ERROR: The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions