Skip to content

Fixed LLM critic bug#88

Open
rkritika1508 wants to merge 5 commits intomainfrom
feat/llm-critic-bug
Open

Fixed LLM critic bug#88
rkritika1508 wants to merge 5 commits intomainfrom
feat/llm-critic-bug

Conversation

@rkritika1508
Copy link
Copy Markdown
Collaborator

@rkritika1508 rkritika1508 commented Apr 13, 2026

Summary

Target issue is #76
Explain the motivation for making this change. What existing problem does the pull request solve?
When the LLM critic validator failed, two different error strings surface directly in the API response:

"The response failed the following metrics: ['query']." — returned when the LLM scored a metric below the configured threshold (normal failure path)
"The response is missing or has invalid evaluations for the following metrics: ['query']." — returned when the LLM's response was malformed, missing a metric key, or returned an out-of-range score (including 0, which LLMCritic treats as invalid rather than a failing score)
These messages are internal implementation details of the third-party library and are not meaningful to end users.

Solution
Added _normalize_llm_critic_error to guardrails.py which intercepts these two known strings and replaces them with clear user-facing messages. The function is only invoked when the failing validator is guardrails/llm_critic (checked via log.validator_name), so no other validators are affected. All other error messages pass through unchanged.

Changes -

  1. backend/app/api/routes/guardrails.py — gate error extraction on log.validator_name for LLM critic logs; add _normalize_llm_critic_error helper at the bottom of the file.
  2. backend/app/tests/test_llm_validators.py — 3 new tests covering both error mappings and the pass-through case for unrelated validators

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

Please add here if any other information is required for the reviewer.

Summary by CodeRabbit

  • Bug Fixes

    • Validation error messages are now normalized for certain validator log cases, producing clearer, standardized user-facing feedback when validations fail or evaluations are missing.
  • Tests

    • Added unit tests covering the new normalization behavior and the fallback for unrecognized messages to ensure consistent user-facing errors.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: abcb7519-581b-47ff-ac06-ff77f40d1cf3

📥 Commits

Reviewing files that changed from the base of the PR and between f29a077 and b5dae29.

📒 Files selected for processing (1)
  • backend/app/api/routes/guardrails.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/app/api/routes/guardrails.py

📝 Walkthrough

Walkthrough

Normalize LLM-critic guardrail errors by adding _normalize_llm_critic_error() and using it when log.validator_name == "guardrails/llm_critic"; add unit tests covering known mappings and passthrough behavior.

Changes

Cohort / File(s) Summary
Error Normalization Implementation
backend/app/api/routes/guardrails.py
Add _normalize_llm_critic_error(message: str) -> str and apply it for failing guard logs where validator_name == "guardrails/llm_critic". Other validators continue to use existing _redact_input(...) flow.
Error Normalization Tests
backend/app/tests/test_llm_validators.py
Import _normalize_llm_critic_error and add three unit tests: normalization for "failed the following metrics", normalization for "missing or invalid evaluations" (including retry suggestion), and passthrough for unknown messages.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

enhancement

Suggested reviewers

  • AkhileshNegi
  • nishika26

Poem

🐰 I nibble errors, trim and bright,
Turning muddled warnings into clear light,
Metrics fixed, missing evals politely say "try",
Tests hop along, tidy and spry,
A rabbit's cheer — neat code, oh my!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title "Fixed LLM critic bug" is overly vague and generic. It describes a bug fix but fails to communicate the specific nature of the change—error message normalization for the LLM critic validator—making it unclear for teammates scanning history. Consider a more descriptive title such as "Normalize LLM critic validator error messages" or "Map internal LLM critic errors to user-friendly messages" to convey the actual implementation.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/llm-critic-bug

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@rkritika1508 rkritika1508 self-assigned this Apr 13, 2026
@rkritika1508 rkritika1508 added bug Something isn't working ready-for-review labels Apr 13, 2026
@rkritika1508 rkritika1508 moved this to In Progress in Kaapi-dev Apr 13, 2026
@rkritika1508 rkritika1508 linked an issue Apr 13, 2026 that may be closed by this pull request
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
backend/app/tests/test_llm_validators.py (1)

103-118: Cover the validator-name gate in _validate_with_guard.

These tests validate _normalize_llm_critic_error directly, but they don’t assert the route gate at backend/app/api/routes/guardrails.py Line 222. Please add one test where a non-guardrails/llm_critic validator emits a message containing "failed the following metrics" and verify it is returned unchanged.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/app/tests/test_llm_validators.py` around lines 103 - 118, Add a test
that exercises the validator-name gate in _validate_with_guard by simulating a
non-'guardrails/llm_critic' validator emitting a message containing "failed the
following metrics" and asserting the message is returned unchanged;
specifically, call _validate_with_guard (the function in
backend/app/api/routes/guardrails.py) or the public wrapper used in tests with
validator_name set to something like "some/other_validator" and the raw message
"The response failed the following metrics: ['quality']." then assert the
returned/propagated message equals the original raw string to ensure the
guardrails/llm_critic special-case is not applied.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/app/api/routes/guardrails.py`:
- Around line 294-303: The function _normalize_llm_critic_error currently
returns the raw upstream message for unknown cases; change it to return a safe
generic fallback instead. Keep the existing exact-match checks for "failed the
following metrics" and "missing or has invalid evaluations", but replace the
final "return message" with a user-facing generic string such as "The response
could not be validated by our safety checks; please retry or contact support."
to avoid exposing upstream/internal phrasing from guardrails/llm_critic.

---

Nitpick comments:
In `@backend/app/tests/test_llm_validators.py`:
- Around line 103-118: Add a test that exercises the validator-name gate in
_validate_with_guard by simulating a non-'guardrails/llm_critic' validator
emitting a message containing "failed the following metrics" and asserting the
message is returned unchanged; specifically, call _validate_with_guard (the
function in backend/app/api/routes/guardrails.py) or the public wrapper used in
tests with validator_name set to something like "some/other_validator" and the
raw message "The response failed the following metrics: ['quality']." then
assert the returned/propagated message equals the original raw string to ensure
the guardrails/llm_critic special-case is not applied.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 29da6d22-f4b0-466e-931b-aa512d93e5f3

📥 Commits

Reviewing files that changed from the base of the PR and between e470a13 and bd40eac.

📒 Files selected for processing (2)
  • backend/app/api/routes/guardrails.py
  • backend/app/tests/test_llm_validators.py

Comment thread backend/app/api/routes/guardrails.py
@nishika26 nishika26 moved this from In Progress to In Review in Kaapi-dev Apr 15, 2026
@nishika26 nishika26 removed this from Kaapi-dev Apr 15, 2026
"The critic model returned an incomplete or malformed response. "
"Please retry."
)
return message
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested this out and i am still getting the same error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready-for-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LLM critic validator enhancement

2 participants