Skip to content

CAMEL-24613: camel-langchain4j-agent-api - LanguageGuardrail must not reject plain English with allowMixed=false - #26081

Merged
oscerd merged 1 commit into
apache:mainfrom
oscerd:fix/CAMEL-24613
Sep 3, 2026
Merged

CAMEL-24613: camel-langchain4j-agent-api - LanguageGuardrail must not reject plain English with allowMixed=false#26081
oscerd merged 1 commit into
apache:mainfrom
oscerd:fix/CAMEL-24613

Conversation

@oscerd

@oscerd oscerd commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Issue

CAMEL-24613

Problem

With allowMixed(false) and allowedLanguages={ENGLISH}, plain English is falsely rejected as "Mixed
language content is not allowed." 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 treats LATIN_SCRIPT as a foreign language (!allowed && != ENGLISH) and fails.

Fix

Treat LATIN_SCRIPT as acceptable when ENGLISH is allowed (English is Latin script), so overlapping
detection 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 lenient
class 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

  • New allowMixedFalseWithEnglishAllowsPlainEnglish asserts plain English passes with allowMixed(false)
    and ENGLISH allowed; verified it fails against the unpatched code. All existing LanguageGuardrailTest
    cases still pass.
  • mvn -Psourcecheck validate green.

🤖 Generated with Claude Code

… 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>
@oscerd oscerd added the bug Something isn't working label Sep 3, 2026
@oscerd oscerd added this to the 4.23.0 milestone Sep 3, 2026
@oscerd
oscerd requested review from Croway and davsclaus September 3, 2026 09:53
@Croway

Croway commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

I am not 100% sure about the LATIN stuff, but I think that this guardrail is a bit loose, compared to the expectation

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🧪 CI tested the following changed modules:

  • components/camel-ai/camel-langchain4j-agent-api

🔬 Scalpel shadow comparison — Scalpel: 10 tested, 27 compile-only — current: 10 all tested

Maveniverse Scalpel detected 37 affected modules (current approach: 10).

⚠️ Modules only in Scalpel (27)
  • apache-camel
  • camel-allcomponents
  • camel-catalog
  • camel-catalog-console
  • camel-catalog-maven
  • camel-catalog-suggest
  • camel-componentdsl
  • camel-endpointdsl
  • camel-endpointdsl-support
  • camel-itest
  • camel-jbang-core
  • camel-jbang-it
  • camel-jbang-main
  • camel-jbang-plugin-edit
  • camel-jbang-plugin-generate
  • camel-jbang-plugin-kubernetes
  • camel-jbang-plugin-test
  • camel-kamelet-main
  • camel-launcher
  • camel-report-maven-plugin
  • camel-route-parser
  • camel-yaml-dsl
  • camel-yaml-dsl-deserializers
  • camel-yaml-dsl-maven-plugin
  • coverage
  • docs
  • dummy-component

Skip-tests mode would test 10 modules (1 direct + 9 downstream), skip tests for 27 (generated code, meta-modules)

Modules Scalpel would test (10)
  • camel-jbang-mcp
  • camel-jbang-plugin-mcp
  • camel-jbang-plugin-route-parser
  • camel-jbang-plugin-tui
  • camel-jbang-plugin-validate
  • camel-langchain4j-agent
  • camel-langchain4j-agent-api
  • camel-launcher-container
  • camel-yaml-dsl-validator
  • camel-yaml-dsl-validator-maven-plugin
Modules with tests skipped (27)
  • apache-camel
  • camel-allcomponents
  • camel-catalog
  • camel-catalog-console
  • camel-catalog-maven
  • camel-catalog-suggest
  • camel-componentdsl
  • camel-endpointdsl
  • camel-endpointdsl-support
  • camel-itest
  • camel-jbang-core
  • camel-jbang-it
  • camel-jbang-main
  • camel-jbang-plugin-edit
  • camel-jbang-plugin-generate
  • camel-jbang-plugin-kubernetes
  • camel-jbang-plugin-test
  • camel-kamelet-main
  • camel-launcher
  • camel-report-maven-plugin
  • camel-route-parser
  • camel-yaml-dsl
  • camel-yaml-dsl-deserializers
  • camel-yaml-dsl-maven-plugin
  • coverage
  • docs
  • dummy-component

ℹ️ Shadow mode — Scalpel observes but does not affect test execution. Learn more

All tested modules (37 modules)
  • Camel :: AI :: LangChain4j :: Agent
  • Camel :: AI :: LangChain4j :: Agent :: API
  • Camel :: All Components Sync point
  • Camel :: Assembly
  • Camel :: Catalog :: Camel Catalog
  • Camel :: Catalog :: Camel Report Maven Plugin
  • Camel :: Catalog :: Camel Route Parser
  • Camel :: Catalog :: Console
  • Camel :: Catalog :: Dummy Component
  • Camel :: Catalog :: Maven
  • Camel :: Catalog :: Suggest
  • Camel :: Component DSL
  • Camel :: Coverage
  • Camel :: Docs
  • Camel :: Endpoint DSL
  • Camel :: Endpoint DSL :: Support
  • Camel :: Integration Tests
  • Camel :: JBang :: Core
  • Camel :: JBang :: Integration tests
  • Camel :: JBang :: MCP
  • Camel :: JBang :: Main
  • Camel :: JBang :: Plugin :: Edit
  • Camel :: JBang :: Plugin :: Generate
  • Camel :: JBang :: Plugin :: Kubernetes
  • Camel :: JBang :: Plugin :: MCP
  • Camel :: JBang :: Plugin :: Route Parser
  • Camel :: JBang :: Plugin :: TUI
  • Camel :: JBang :: Plugin :: Testing
  • Camel :: JBang :: Plugin :: Validate
  • Camel :: Kamelet Main
  • Camel :: Launcher
  • Camel :: Launcher :: Container
  • Camel :: YAML DSL
  • Camel :: YAML DSL :: Deserializers
  • Camel :: YAML DSL :: Maven Plugins
  • Camel :: YAML DSL :: Validator
  • Camel :: YAML DSL :: Validator Maven Plugin

⚙️ View full build and test results

@gnodet gnodet left a comment

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.

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: CHINESE and JAPANESE patterns overlap identically — users with allowedLanguages={CHINESE}, allowMixed=false will hit the same false rejection. Worth a follow-up issue.
  • Pre-existing: minLanguageRatio is declared but never read in validate() — appears to be dead code from the original CAMEL-22793 commit.

🔀 Backport Status

⚠️ This bug fix targets 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

@oscerd oscerd self-assigned this Sep 3, 2026
@oscerd
oscerd merged commit 75acda3 into apache:main Sep 3, 2026
5 checks passed
@gnodet

gnodet commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🔄 Backport Bot

Failed:

  • camel-4.22.x — Cherry-pick conflict in: components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrailTest.java
  • camel-4.18.x — Cherry-pick conflict in: components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrailTest.java

ℹ️ If you push additional commits after /port, use /port again to update the port PRs.

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

Labels

bug Something isn't working components components-ai

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants