Skip to content

fix(server): make openai an optional server-only dependency#1623

Merged
Pouyanpi merged 3 commits intodevelopfrom
fix/openai-optional-server-dependency
Feb 6, 2026
Merged

fix(server): make openai an optional server-only dependency#1623
Pouyanpi merged 3 commits intodevelopfrom
fix/openai-optional-server-dependency

Conversation

@Pouyanpi
Copy link
Collaborator

@Pouyanpi Pouyanpi commented Feb 6, 2026

Description

Resolves some minor issues due to #1340 ‪that causes a job failure.

  • Mark openai as an optional dependency in pyproject.toml (already included in the server extras group)
  • Install wheel with [server] extra in the CI test-wheel job since it starts a server
  • Guard the server import in the CLI with a helpful error message when openai is missing
  • Add openai to dev dependencies so server tests pass locally (consistent with yara-python, opentelemetry-api, etc.)
  • Add pytest.importorskip("openai") guards to all server test files as a defensive measure

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 6, 2026

Greptile Overview

Greptile Summary

This PR makes openai an optional dependency that's only required when running the guardrails server. The change consists of:

  • Marking openai as optional in pyproject.toml (it's already included in the server extras group)
  • Adding graceful ImportError handling in the CLI's server command with a clear error message guiding users to install nemoguardrails[server]
  • Updating the CI workflow to install the wheel with [server] extra since the test-wheel job starts a server
  • Poetry automatically updated poetry.lock to mark transitive dependencies (distro, jiter) as optional

This improves the user experience by allowing users to install a lighter version of the package when they don't need server functionality, while providing clear guidance when the server dependencies are missing.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • All changes are well-coordinated: the dependency is properly marked as optional, the CLI provides helpful error handling, and the CI is updated to accommodate the change. The implementation follows best practices for optional dependencies and provides a good user experience.
  • No files require special attention

Important Files Changed

Filename Overview
pyproject.toml Correctly marks openai as optional dependency, already included in server extras group
poetry.lock Transitive dependencies (distro, jiter) of openai automatically marked optional, content hash updated
nemoguardrails/cli/init.py Added proper ImportError handling with helpful error message when openai is missing
.github/workflows/test-and-build-wheel.yml Updated CI to install wheel with [server] extra since test starts a server

Sequence Diagram

sequenceDiagram
    participant User
    participant CLI as nemoguardrails CLI
    participant PyProject as pyproject.toml
    participant Server as nemoguardrails.server.api
    participant OpenAI as openai package

    Note over User,OpenAI: Without [server] extra installed
    User->>CLI: nemoguardrails server
    CLI->>Server: try: from nemoguardrails.server import api
    Server->>OpenAI: import openai
    OpenAI-->>Server: ImportError (not installed)
    Server-->>CLI: ImportError
    CLI->>User: Error: "openai package required. Install with: pip install nemoguardrails[server]"
    CLI->>User: Exit(1)

    Note over User,OpenAI: With [server] extra installed
    User->>CLI: pip install nemoguardrails[server]
    CLI->>PyProject: Read extras dependencies
    PyProject->>CLI: Install: aiofiles, openai
    User->>CLI: nemoguardrails server
    CLI->>Server: try: from nemoguardrails.server import api
    Server->>OpenAI: import openai
    OpenAI-->>Server: Success
    Server-->>CLI: Success
    CLI->>User: Server starts successfully
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@Pouyanpi Pouyanpi force-pushed the fix/openai-optional-server-dependency branch from 60939e2 to d1522de Compare February 6, 2026 08:46
@codecov
Copy link

codecov bot commented Feb 6, 2026

Codecov Report

❌ Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
nemoguardrails/cli/__init__.py 40.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Prevents test collection errors when openai is not installed by adding
pytest.importorskip guards to server test files and including openai in
the dev dependency group
@Pouyanpi Pouyanpi force-pushed the fix/openai-optional-server-dependency branch from 36ccaa3 to 8366e91 Compare February 6, 2026 08:59
@Pouyanpi Pouyanpi self-assigned this Feb 6, 2026
@Pouyanpi Pouyanpi added this to the v0.21 milestone Feb 6, 2026
Copy link
Member

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

@Pouyanpi Pouyanpi merged commit a6589a6 into develop Feb 6, 2026
32 checks passed
@Pouyanpi Pouyanpi deleted the fix/openai-optional-server-dependency branch February 6, 2026 17:20
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