Skip to content

fix(actions): handle empty string values in create_event (#1700)#1934

Open
LeSingh1 wants to merge 2 commits into
NVIDIA-NeMo:developfrom
LeSingh1:fix/1700-create-event-empty-string
Open

fix(actions): handle empty string values in create_event (#1700)#1934
LeSingh1 wants to merge 2 commits into
NVIDIA-NeMo:developfrom
LeSingh1:fix/1700-create-event-empty-string

Conversation

@LeSingh1
Copy link
Copy Markdown

@LeSingh1 LeSingh1 commented May 26, 2026

Fixes #1700.

create_event in nemoguardrails/actions/core.py checks v[0] == "$" after confirming v is a string, to detect $variable references that should be looked up in the action context. When v is the empty string "", this raises IndexError: string index out of range because there is no v[0].

The fix replaces v[0] == "$" with v.startswith("$"), which returns False for the empty string and matches the pattern used elsewhere in the codebase. The behavior for non-empty strings is unchanged.

Reproduction from the issue:

import asyncio
from nemoguardrails.actions.core import create_event

asyncio.run(create_event(event={"_type": "SomeEvent", "param": ""}))
# Before: IndexError: string index out of range
# After:  ActionResult with the event passed through unchanged

Tests

Added two unit tests in tests/test_actions.py:

  • test_create_event_accepts_empty_string_values — fails on the old code, passes after the fix
  • test_create_event_still_resolves_dollar_prefixed_references — confirms $user_name lookup against context still works
$ pytest tests/test_actions.py -v
tests/test_actions.py::test_action_decorator PASSED
tests/test_actions.py::test_action_decorator_with_output_mapping PASSED
tests/test_actions.py::test_create_event_accepts_empty_string_values PASSED
tests/test_actions.py::test_create_event_still_resolves_dollar_prefixed_references PASSED
tests/test_actions.py::test_action_result PASSED
============================== 5 passed in 0.26s ===============================

Summary by CodeRabbit

  • Bug Fixes

    • Improved robustness of event creation by preventing edge-case errors during parameter processing.
  • Tests

    • Added test coverage to verify parameter handling reliability.

Review Change Stack

…#1700)

`create_event` checks `v[0] == "$"` after confirming `v` is a string to
detect `$variable` references in the context. For an empty string this
raises `IndexError: string index out of range` because there is no
`v[0]`.

Switch to `v.startswith("$")`, which returns False for the empty string
and matches the pattern used elsewhere in the codebase for the same
check.

Add two unit tests in `tests/test_actions.py`: one for the empty-string
case (which raises on the old code) and one confirming the existing
`$user_name` resolution path still works.

Signed-off-by: Shaurya Singh <sshaurya914@gmail.com>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 26, 2026

Greptile Summary

This PR fixes a one-line bug in create_event where indexing an empty string (v[0]) raised IndexError. The fix replaces the index access with v.startswith("$"), which safely returns False for empty strings while preserving the existing behavior for all non-empty strings.

  • nemoguardrails/actions/core.py: Replaces v[0] == "$" with v.startswith("$") on the variable-reference detection path.
  • tests/test_actions.py: Adds two targeted regression tests — one for the empty-string case and one to confirm $variable resolution against context still works.

Confidence Score: 5/5

Safe to merge — the change is a one-line targeted fix with no behavioral impact on non-empty strings.

The fix is minimal and correct: startswith is strictly safer than index access and produces identical results for every non-empty string. The two new regression tests cover both the broken path and the existing happy path. No other code paths are affected.

No files require special attention.

Important Files Changed

Filename Overview
nemoguardrails/actions/core.py Single-line fix: v[0] == "$"v.startswith("$") eliminates IndexError on empty strings; logic is correct and minimal.
tests/test_actions.py Adds two well-scoped regression tests covering the empty-string bug and the $variable resolution path; assertions align with new_event_dict output shape.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[create_event called] --> B[Build event_dict via new_event_dict]
    B --> C[Iterate over event_dict items]
    C --> D{Is value a string?}
    D -- No --> E[Keep value unchanged]
    D -- Yes --> F{"v.startswith('$')?"}
    F -- "No (includes empty string)" --> E
    F -- Yes --> G{context provided?}
    G -- Yes --> H[Resolve from context]
    G -- No --> I[Set to None]
    H --> J[Return ActionResult]
    I --> J
    E --> J
Loading

Reviews (2): Last reviewed commit: "style(tests): apply ruff format to test_..." | Re-trigger Greptile

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a7839139-bf77-417e-a853-bf650b5d03b9

📥 Commits

Reviewing files that changed from the base of the PR and between d1ee775 and 58a4d47.

📒 Files selected for processing (2)
  • nemoguardrails/actions/core.py
  • tests/test_actions.py

📝 Walkthrough

Walkthrough

This PR fixes a bug in create_event where checking the first character of a parameter string without bounds validation caused IndexError on empty strings. The fix replaces direct character indexing with startswith("$"), and new tests verify both the safety improvement and correct variable substitution behavior.

Changes

create_event robustness

Layer / File(s) Summary
Safe variable-reference detection in create_event
nemoguardrails/actions/core.py
create_event now uses startswith("$") instead of indexing the first character to detect variable-reference strings, preventing IndexError when a parameter value is an empty string while preserving substitution behavior from context.
Test coverage for create_event robustness
tests/test_actions.py
Added imports for asyncio and create_event, then introduced two new asynchronous test cases: one verifies empty string values are handled safely without errors, and another verifies dollar-prefixed references are correctly resolved using the provided context.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: handling empty string values in the create_event function, directly matching the core change in the pull request.
Linked Issues check ✅ Passed The PR implementation directly addresses issue #1700 by replacing the unsafe v[0] check with v.startswith('$') and adding comprehensive unit tests for both empty strings and variable resolution.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the IndexError in create_event and adding tests; no unrelated modifications were introduced.
Test Results For Major Changes ✅ Passed This is a minor bug fix (1-line change) with comprehensive test coverage. Two new tests verify the fix and prevent regression. No performance or numeric implications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Signed-off-by: Shaurya Singh <sshaurya914@gmail.com>
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.

bug: create_event raises IndexError for empty string values

1 participant