Skip to content

feat: support method chaining by returning self from LLMRails.register_* methods #1296

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 2 commits into
base: develop
Choose a base branch
from

Conversation

winstonallo
Copy link

Description

Return self from the register_* methods in the LLMRails class to allow for function chaining, allows for structuring the code in a more functional way:

Before:

rails = LLMRails(
    config=config,
    llm=azure_openai,
)
rails.register_action(proxy, "proxy")
rails.register_action(fetch_updated_guidelines, "fetch_updated_guidelines")

After:

rails = (
    LLMRails(
        config=config,
        llm=azure_openai,
    )
    .register_action(proxy, "proxy")
    .register_action(fetch_updated_guidelines, "fetch_updated_guidelines")
)

Note that the Before version is still possible, this change would just allow for more flexible structure.

Checklist

@Pouyanpi Pouyanpi requested a review from Copilot July 18, 2025 11:21
Copilot

This comment was marked as outdated.

@Pouyanpi
Copy link
Collaborator

@winstonallo thanks for opening this PR. It seems that some unrelated sections of the code has been deleted, would you revert those?

@winstonallo
Copy link
Author

@winstonallo thanks for opening this PR. It seems that some unrelated sections of the code has been deleted, would you revert those?

Done, sorry about that!

@winstonallo winstonallo requested a review from Copilot July 18, 2025 11:34
Copilot

This comment was marked as outdated.

@winstonallo winstonallo requested a review from Copilot July 18, 2025 11:36
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds method chaining support to the LLMRails class by modifying all register_* methods to return self instead of None. This allows for a more functional programming style where multiple registration calls can be chained together in a single expression.

Key changes:

  • Updated return type annotations from None to Self for all registration methods
  • Added return self statements to enable method chaining
  • Imported Self from typing_extensions to support the new return types
Comments suppressed due to low confidence (1)

nemoguardrails/rails/llm/llmrails.py:367

  • The removal of the streaming token usage configuration appears unrelated to the PR's purpose of adding method chaining. This change should either be reverted or explained in the PR description as it affects functionality beyond the stated scope.
            if api_key:

@winstonallo
Copy link
Author

Hey, do I need to add tests for this? If yes, I will get to that on Wednesday

@winstonallo
Copy link
Author

Hey, I added tests to tests/llm_rails.py verifying that all register_* methods return self and ran the pre-commit hook:)

@winstonallo winstonallo changed the title Return self from LLMRails.register_* methods to allow for chaining Return self from LLMRails.register_* methods to allow for method chaining Jul 23, 2025
Copy link
Collaborator

@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 👍 ! Thank you, @winstonallo!

@trebedea
Copy link
Collaborator

Looks good 👍 ! Thank you, @winstonallo!

Again, you need to run pre-commits and sign your commits, similar to your other MR.

@winstonallo winstonallo force-pushed the allow-chaining-register-functions branch from 4cd9d4a to 31c71e4 Compare July 23, 2025 13:02
@winstonallo winstonallo force-pushed the allow-chaining-register-functions branch from 31c71e4 to 8575b88 Compare July 23, 2025 13:03
@winstonallo winstonallo reopened this Jul 23, 2025
@winstonallo
Copy link
Author

@trebedea done:)

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.27%. Comparing base (cd14a07) to head (cda6e3c).
⚠️ Report is 3 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1296      +/-   ##
===========================================
+ Coverage    70.20%   70.27%   +0.07%     
===========================================
  Files          161      161              
  Lines        16123    16131       +8     
===========================================
+ Hits         11319    11336      +17     
+ Misses        4804     4795       -9     
Flag Coverage Δ
python 70.27% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
nemoguardrails/rails/llm/llmrails.py 90.06% <100.00%> (+0.89%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Pouyanpi Pouyanpi changed the title Return self from LLMRails.register_* methods to allow for method chaining feat: support method chaining by returning self from LLMRails.register_* methods Jul 29, 2025
@Pouyanpi Pouyanpi added this to the v0.16.0 milestone Aug 1, 2025
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.

4 participants