CAMEL-24613: camel-langchain4j-agent-api - LanguageGuardrail must not reject plain English with allowMixed=false - #26081
Conversation
… reject plain English when mixed content is disallowed
With allowMixed=false and allowedLanguages={ENGLISH}, plain English was falsely
rejected as "Mixed language content". The ENGLISH and LATIN_SCRIPT enum patterns
overlap (ASCII letters match both), so plain English is detected as both ENGLISH
and LATIN_SCRIPT; the mixed-content check then treated LATIN_SCRIPT as a foreign
language. Treat LATIN_SCRIPT as acceptable when ENGLISH is allowed.
(The other item originally bundled under this issue - adding a guardrail-interface
check to parseGuardrailClasses - was withdrawn: that method is an intentionally
lenient class loader, as its Javadoc and existing tests show, and langchain4j
validates guardrail types downstream.)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ka4dAcJMpxahMfk3kmG5Ls
Signed-off-by: Andrea Cosentino <ancosen@gmail.com>
|
I am not 100% sure about the LATIN stuff, but I think that this guardrail is a bit loose, compared to the expectation |
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
🧪 CI tested the following changed modules:
🔬 Scalpel shadow comparison — Scalpel: 10 tested, 27 compile-only — current: 10 all testedMaveniverse Scalpel detected 37 affected modules (current approach: 10).
|
gnodet
left a comment
There was a problem hiding this comment.
The fix is correct and — addressing @Croway's concern — not too loose.
Because ENGLISH requires the entire string to match ^[\p{ASCII}\s\p{Punct}]+$, it cannot co-occur with any non-ASCII script (Cyrillic, Chinese, Arabic, etc.). The only language that can co-occur with ENGLISH is LATIN_SCRIPT (Latin letters are ASCII). So treating LATIN_SCRIPT as acceptable when ENGLISH is allowed only suppresses the false positive from pattern overlap and can never bypass a genuine mixed-content rejection.
The test confirms plain English passes; a negative test for actual mixed content (e.g. English + Cyrillic) being rejected with allowMixed=false would strengthen confidence further.
Minor observations (not blocking):
- Pre-existing:
CHINESEandJAPANESEpatterns overlap identically — users withallowedLanguages={CHINESE}, allowMixed=falsewill hit the same false rejection. Worth a follow-up issue. - Pre-existing:
minLanguageRatiois declared but never read invalidate()— appears to be dead code from the original CAMEL-22793 commit.
🔀 Backport Status
main but no backport PRs or labels were found. The LanguageGuardrail class exists on camel-4.22.x and camel-4.18.x with the same buggy code at line 209. Consider backporting to both active maintenance branches.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of Guillaume Nodet
🔄 Backport BotFailed:
ℹ️ If you push additional commits after |
Issue
CAMEL-24613
Problem
With
allowMixed(false)andallowedLanguages={ENGLISH}, plain English is falsely rejected as "Mixedlanguage content is not allowed." The
ENGLISHandLATIN_SCRIPTenum patterns overlap (ASCII lettersmatch both), so plain English is detected as both
ENGLISHandLATIN_SCRIPT; the mixed-contentcheck then treats
LATIN_SCRIPTas a foreign language (!allowed && != ENGLISH) and fails.Fix
Treat
LATIN_SCRIPTas acceptable whenENGLISHis allowed (English is Latin script), so overlappingdetection no longer triggers a false mixed-content rejection.
Scope note
This issue originally also bundled adding a guardrail-interface check to
AgentConfiguration.parseGuardrailClasses. That was withdrawn: the method is an intentionally lenientclass loader (its Javadoc documents loading arbitrary class names, and six existing tests exercise it with
non-guardrail classes such as
java.lang.String), and langchain4j validates guardrail types downstream.Adding the check would break that documented contract.
Testing
allowMixedFalseWithEnglishAllowsPlainEnglishasserts plain English passes withallowMixed(false)and
ENGLISHallowed; verified it fails against the unpatched code. All existingLanguageGuardrailTestcases still pass.
mvn -Psourcecheck validategreen.🤖 Generated with Claude Code