Skip to content

Conversation

Pouyanpi
Copy link
Collaborator

PR Description

Tool Input Rails Implementation

This PR implements comprehensive tool input rails functionality to validate and secure tool messages before they are processed by the LLM.

Key Changes:

  • Added UserToolMessages event processing with grouped tool message handling
  • Fixed message-to-event conversion for proper tool message support in conversation flows
  • Enhanced passthrough mode to preserve full conversation context including tool calls
  • Implemented tool input rail flows supporting validation, safety checks, and sanitization
  • Comprehensive test coverage for all tool input rails scenarios

Benefits:

  • Enables validation and filtering of potentially dangerous tool outputs
  • Provides sanitization capabilities for sensitive information in tool responses
  • Maintains conversation context integrity during tool interactions
  • Supports configurable tool input validation pipelines

@Pouyanpi Pouyanpi added this to the v0.17.0 milestone Sep 10, 2025
@Pouyanpi Pouyanpi self-assigned this Sep 10, 2025
@Pouyanpi Pouyanpi added the enhancement New feature or request label Sep 10, 2025
@Pouyanpi Pouyanpi requested a review from Copilot September 10, 2025 12:21
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 implements comprehensive tool input rails functionality to validate and secure tool messages before they are processed by the LLM.

  • Added UserToolMessages event processing with grouped tool message handling
  • Fixed message-to-event conversion for proper tool message support in conversation flows
  • Enhanced passthrough mode to preserve full conversation context including tool calls

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_tool_calls_event_extraction.py Updates test for event name change from BotToolCall to BotToolCalls
tests/test_tool_calling_passthrough_only.py Updates test assertions and comments for event name change
tests/test_input_tool_rails.py Comprehensive test suite for tool input rails functionality
tests/test_bot_tool_call_events.py Updates comments to reflect event name change
tests/runnable_rails/test_tool_calling.py Updates test comment for event name change
tests/runnable_rails/test_runnable_rails.py Updates test assertion for improved passthrough mode behavior
tests/input_tool_rails_actions.py Test utility actions for tool input validation, safety checking, and sanitization
nemoguardrails/rails/llm/llmrails.py Core implementation for message-to-event conversion with tool message support
nemoguardrails/rails/llm/llm_flows.co Flow definitions for tool input rails processing
nemoguardrails/integrations/langchain/runnable_rails.py Enhanced message conversion with tool call support
nemoguardrails/actions/llm/utils.py Updated tool call extraction functions for new event name
nemoguardrails/actions/llm/generation.py Enhanced passthrough mode to preserve tool message context

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Pouyanpi Pouyanpi changed the title Feat/tool calling input eat(tool-rails): add support for tool input rails and validation Sep 10, 2025
@Pouyanpi Pouyanpi changed the title eat(tool-rails): add support for tool input rails and validation feat(tool-rails): add support for tool input rails and validation Sep 10, 2025
@Pouyanpi Pouyanpi force-pushed the feat/tool-calling-input branch from 0358cd7 to 3240dc9 Compare September 15, 2025 09:35
@Pouyanpi Pouyanpi force-pushed the feat/tool-output-rails branch from df75862 to 56cb9dd Compare September 15, 2025 09:38
@Pouyanpi Pouyanpi force-pushed the feat/tool-calling-input branch 2 times, most recently from 21e33e2 to 2f57ec4 Compare September 15, 2025 09:46
@Pouyanpi Pouyanpi force-pushed the feat/tool-output-rails branch 2 times, most recently from 44a25f3 to c8a4959 Compare September 15, 2025 09:54
@Pouyanpi Pouyanpi force-pushed the feat/tool-calling-input branch 3 times, most recently from 4c34032 to c8ff064 Compare September 15, 2025 11:01
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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Collaborator

@tgasser-nv tgasser-nv 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, approving. Please address feedback before merging. This is another >1k line PR which should be broken into smaller stacked PRs to review.

Please also run local integration-tests to make a few tool-calls to production LLMs to check this works before merging. There are also some Github CI tests failing, could you take a look before merging?

@Pouyanpi Pouyanpi force-pushed the feat/tool-output-rails branch from c8a4959 to e17edb2 Compare September 22, 2025 09:03
Base automatically changed from feat/tool-output-rails to develop September 22, 2025 09:09
…ion and processing

- Add UserToolMessages event handling and tool input rails processing
- Fix message-to-event conversion to properly handle tool messages in
conversation history
- Preserve tool call context in passthrough mode by using full
conversation history
- Support tool_calls and tool message metadata in LangChain format
conversion
- Include comprehensive test suite for tool input rails functionality

test(runnable_rails): fix prompt format in passthrough mode

feat: support ToolMessage in message dicts

refactor: rename BotToolCall to BotToolCalls
@Pouyanpi Pouyanpi force-pushed the feat/tool-calling-input branch from c8ff064 to 5792bea Compare September 22, 2025 09:11
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 83.67347% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...uardrails/integrations/langchain/runnable_rails.py 33.33% 6 Missing ⚠️
nemoguardrails/actions/llm/generation.py 90.00% 1 Missing ⚠️
nemoguardrails/rails/llm/llmrails.py 95.23% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Pouyanpi Pouyanpi merged commit 96111da into develop Sep 22, 2025
13 of 14 checks passed
@Pouyanpi Pouyanpi deleted the feat/tool-calling-input branch September 22, 2025 09:39
tgasser-nv pushed a commit that referenced this pull request Sep 22, 2025
…ion and processing (#1386)

- Add UserToolMessages event handling and tool input rails processing
- Fix message-to-event conversion to properly handle tool messages in
conversation history
- Preserve tool call context in passthrough mode by using full
conversation history
- Support tool_calls and tool message metadata in LangChain format
conversion
- Include comprehensive test suite for tool input rails functionality

test(runnable_rails): fix prompt format in passthrough mode

feat: support ToolMessage in message dicts

refactor: rename BotToolCall to BotToolCalls
tgasser-nv pushed a commit that referenced this pull request Sep 22, 2025
…ion and processing (#1386)

- Add UserToolMessages event handling and tool input rails processing
- Fix message-to-event conversion to properly handle tool messages in
conversation history
- Preserve tool call context in passthrough mode by using full
conversation history
- Support tool_calls and tool message metadata in LangChain format
conversion
- Include comprehensive test suite for tool input rails functionality

test(runnable_rails): fix prompt format in passthrough mode

feat: support ToolMessage in message dicts

refactor: rename BotToolCall to BotToolCalls
Pouyanpi added a commit that referenced this pull request Oct 1, 2025
…ion and processing (#1386)

- Add UserToolMessages event handling and tool input rails processing
- Fix message-to-event conversion to properly handle tool messages in
conversation history
- Preserve tool call context in passthrough mode by using full
conversation history
- Support tool_calls and tool message metadata in LangChain format
conversion
- Include comprehensive test suite for tool input rails functionality

test(runnable_rails): fix prompt format in passthrough mode

feat: support ToolMessage in message dicts

refactor: rename BotToolCall to BotToolCalls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants