This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new 098f547ba867 [backport camel-4.22.x] CAMEL-24614: 
camel-langchain4j-agent - fail fast when no agent can be resolved (#26118)
098f547ba867 is described below

commit 098f547ba867f1b7e9f0c327d3ca92c3b44758c6
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Sep 4 11:51:05 2026 +0200

    [backport camel-4.22.x] CAMEL-24614: camel-langchain4j-agent - fail fast 
when no agent can be resolved (#26118)
    
    Co-authored-by: github-actions[bot] 
<github-actions[bot]@users.noreply.github.com>
---
 .../agent/LangChain4jAgentProducer.java            | 12 +++++
 .../agent/LangChain4jAgentMissingAgentTest.java    | 60 ++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git 
a/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java
 
b/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java
index 197481eb9054..797fcda39bb9 100644
--- 
a/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java
+++ 
b/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java
@@ -137,6 +137,18 @@ public class LangChain4jAgentProducer extends 
DefaultProducer {
         String tags = endpoint.getConfiguration().getTags();
 
         Agent agent = agentFactory != null ? 
agentFactory.createAgent(exchange) : this.agent;
+        if (agent == null && agentFactory == null) {
+            // Support an agent configured on the endpoint after the route 
started, and give a clear error
+            // instead of an opaque NullPointerException when nothing resolves 
to an agent.
+            agent = endpoint.getConfiguration().getAgent();
+            if (agent == null) {
+                throw new IllegalArgumentException(
+                        "No agent could be resolved for endpoint " + 
endpoint.getEndpointUri()
+                                                   + ". Configure 'agent', 
'agentConfiguration' or 'agentFactory', or bind a bean named '"
+                                                   + endpoint.getAgentId() + 
"' of type " + Agent.class.getName()
+                                                   + " in the registry.");
+            }
+        }
 
         AiAgentBody<?> aiAgentBody = 
exchange.getMessage().getMandatoryBody(AiAgentBody.class);
 
diff --git 
a/components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java
 
b/components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java
new file mode 100644
index 000000000000..76c2549429d9
--- /dev/null
+++ 
b/components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.langchain4j.agent;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.langchain4j.agent.api.AiAgentBody;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class LangChain4jAgentMissingAgentTest {
+
+    @Test
+    void missingAgentReportsAClearError() throws Exception {
+        try (DefaultCamelContext context = new DefaultCamelContext()) {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    // No agent / agentConfiguration / agentFactory, and no 
registry bean named "noSuchAgent".
+                    from("direct:x").to("langchain4j-agent:noSuchAgent");
+                }
+            });
+            context.start();
+
+            Exchange result = context.createProducerTemplate()
+                    .request("direct:x", e -> e.getIn().setBody(new 
AiAgentBody<>("hello")));
+
+            Throwable cause = result.getException();
+            assertNotNull(cause, "an error was expected");
+            boolean clearError = false;
+            while (cause != null) {
+                if (cause instanceof IllegalArgumentException && 
cause.getMessage() != null
+                        && cause.getMessage().contains("No agent could be 
resolved")) {
+                    clearError = true;
+                    break;
+                }
+                cause = cause.getCause();
+            }
+            assertTrue(clearError, "expected a clear 'No agent could be 
resolved' IllegalArgumentException, got: "
+                                   + result.getException());
+        }
+    }
+}

Reply via email to