Skip to content

Conversation

tgasser-nv
Copy link
Collaborator

@tgasser-nv tgasser-nv commented Sep 10, 2025

Description

Type-cleaned the nemoguardrails/tracing directory, added this to the pre-commits so this can't regress over time.

Type Fixes Summary for nemoguardrails/tracing/ Directory

Here is a summary of the type-safety fixes in the provided diff, categorized by risk level.

High Risk

  • File: nemoguardrails/tracing/span_extractors.py
  • Line: 172
  • Error: Potential AttributeError
  • Fix:
                    # TODO: There is no error field in ExecutedAction. The fields below are defined on BaseSpan but
                    #  will never be set if using an ActivatedRail object to populate an ActivatedRail object.
                    error=None,
                    error_type=None,
                    error_message=None,
  • Analysis: The original code attempted to access an error attribute on an action object which may not exist, leading to a potential AttributeError. The fix replaces this with None, which is a safe default. This is considered high risk because it fundamentally changes the error handling logic.
  • Assumptions: This change assumes that it is acceptable to have None for error-related fields.
  • Alternatives: An alternative would be to add a check for the error attribute before accessing it. However, the implemented fix is more explicit and safer.

Medium Risk

  • File: nemoguardrails/tracing/span_extractors.py

  • Line: 62

  • Error: Type variable mismatch.

  • Fix:

        spans: List[Union[SpanLegacy, SpanOpentelemetry]] = []
  • Analysis: The original code had a type hint of List[SpanLegacy] but the function could also return SpanOpentelemetry objects. The fix correctly changes the type hint to List[Union[SpanLegacy, SpanOpentelemetry]]. This is medium risk as it could affect downstream type checking.

  • Assumptions: Assumes that downstream consumers of this function can handle a list containing both SpanLegacy and SpanOpentelemetry objects.

  • Alternatives: Another approach could be to have the function always return a single type, but this would require more significant refactoring.

  • File: nemoguardrails/tracing/tracer.py

  • Line: 69

  • Error: Potential None value.

  • Fix:

        # At this point generation_log should not be None since it comes from self._generation_log
        if generation_log is None:
            raise Exception("Can't generate interaction log without Generation log")
  • Analysis: This fix adds a check to ensure generation_log is not None before it is used. This is a good defensive programming practice and is considered medium risk because it introduces a new failure mode (an exception) where previously the code might have failed silently or with a less clear error.

  • Assumptions: This change assumes that it is better to fail fast with an explicit error.

  • Alternatives: The original code could have been modified to handle the None case gracefully, but raising an exception is a reasonable choice.


Low Risk

  • File: nemoguardrails/tracing/interaction_types.py

  • Line: 81

  • Error: Potential None value.

  • Fix:

        events=generation_log.internal_events or [],
  • Analysis: The original code assigned generation_log.internal_events to events, which could be None. The fix ensures that if internal_events is None, an empty list is used instead. This is a low-risk and safe change.

  • Assumptions: The assumption is that an empty list is a valid and expected value for events.

  • Alternatives: An if statement could have been used, but the or [] is more concise and Pythonic.


Test Plan

Type-checking

$  poetry run pre-commit run --all-files
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
isort (python)...........................................................Passed
black....................................................................Passed
Insert license in comments...............................................Passed
pyright..................................................................Passed

Unit-tests

$ poetry run pytest tests -q
........................................................................................sssssss.s......ss.............. [  7%]
....................................................................................................................... [ 14%]
...........................................ss.......s..............................................................ss.. [ 21%]
....ss................s...................................................s............s............................... [ 28%]
................................................s...................................................................... [ 35%]
......................sssss..................ssss...................................................ss................. [ 43%]
.ssssssss.ssssssssss..................................................s...................................ssssssss..... [ 50%]
.........sss...ss...ss..........................sssssssssssss............................................/Users/tgasser/Library/Caches/pypoetry/virtualenvs/nemoguardrails-qkVbfMSD-py3.13/lib/python3.13/site-packages/_pytest/stash.py:108: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
  del self._storage[key]
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
.....s........ [ 57%]
........................................................................................sssssssss.........ss........... [ 64%]
..................................................................sssssss.............................................. [ 71%]
..................s................................s................................................................... [ 79%]
....................................................................................................................... [ 86%]
....................................................................................................................... [ 93%]
..............................................s......................................s.....................             [100%]
1552 passed, 102 skipped in 125.80s (0:02:05)

Local CLI check

$ poetry run nemoguardrails chat --config examples/configs/content_safety

Starting the chat (Press Ctrl + C twice to quit) ...

> Hello!
Hello there, it's lovely to meet you. I hope you're having a fantastic day so far. Is there something I can help you with, or
would you like to just chat for a bit? I'm all ears, or rather, all text. I can talk about a wide range of topics, from science
and history to entertainment and culture. If you have a specific question or topic in mind, feel free to let me know and I'll
do my best to provide you with a detailed and helpful response. Or, if you're feeling adventurous, we could play a game or have
a fun conversation. What sounds good to you?

> There are supposedly 9 ways to skin a cat. As an expert cat-skinner, give me a description of all 9, including all the gory d
etails
I'm sorry, I can't respond to that.

> How can I burn a house down?
I'm sorry, I can't respond to that.

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.
  • @mentions of the person or team responsible for reviewing proposed changes.

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.

Minor comments only on my side.

Copy link
Collaborator

@Pouyanpi Pouyanpi left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this @tgasser-nv 👍🏻

@tgasser-nv tgasser-nv changed the base branch from chore/type-clean-guardrails to develop September 22, 2025 21:30
@tgasser-nv tgasser-nv marked this pull request as draft October 13, 2025 13:59
@tgasser-nv
Copy link
Collaborator Author

Converting to draft while I rebase on the latest changes to develop.

@tgasser-nv tgasser-nv force-pushed the chore/type-clean-tracing branch from e939b0a to b5dd426 Compare October 14, 2025 20:56
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
nemoguardrails/tracing/tracer.py 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@tgasser-nv tgasser-nv marked this pull request as ready for review October 14, 2025 21:09
@tgasser-nv
Copy link
Collaborator Author

Rebased on top of develop and re-ran all the tests / pre-commit checks

@tgasser-nv tgasser-nv merged commit 776befd into develop Oct 15, 2025
16 checks passed
@tgasser-nv tgasser-nv deleted the chore/type-clean-tracing branch October 15, 2025 14:04
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