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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 32ccf2272029 CAMEL-27093: camel-ibm-watsonx-ai - check the elements of 
the List inputs of embedding and rerank (#30888)
32ccf2272029 is described below

commit 32ccf227202980caea3cb36273f6af6e776897c3
Author: ravismash <[email protected]>
AuthorDate: Fri Nov 27 01:36:56 2026 +0530

    CAMEL-27093: camel-ibm-watsonx-ai - check the elements of the List inputs 
of embedding and rerank (#30888)
    
    getInputs cast a List body, and the list in the CamelIbmWatsonxAiInputs
    header, to List<String> without looking at the elements, so a non-String
    element reached the watsonx.ai SDK. Each element is now checked the way
    toChatMessages checks chat messages: a String is an input text, a value
    whose toString() is its text (UUID, URI, a date) is read as text, and any
    other element fails with its index and class before a request is sent.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../ai/handler/AbstractWatsonxAiHandler.java       | 35 ++++++++++++--
 .../ibm/watsonx/ai/WatsonxAiMessageBodyTest.java   | 54 ++++++++++++++++++++++
 2 files changed, 84 insertions(+), 5 deletions(-)

diff --git 
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/AbstractWatsonxAiHandler.java
 
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/AbstractWatsonxAiHandler.java
index 5d8547e81bac..0fcedc232edc 100644
--- 
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/AbstractWatsonxAiHandler.java
+++ 
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/AbstractWatsonxAiHandler.java
@@ -87,18 +87,18 @@ public abstract class AbstractWatsonxAiHandler implements 
WatsonxAiOperationHand
     /**
      * Extracts list of inputs from exchange body or header.
      */
-    @SuppressWarnings("unchecked")
     protected List<String> getInputs(Exchange exchange) {
         Message in = exchange.getIn();
 
         // Try header first
-        List<String> inputs = in.getHeader(WatsonxAiConstants.INPUTS, 
List.class);
+        List<String> inputs = toInputs(in.getHeader(WatsonxAiConstants.INPUTS, 
List.class),
+                "header '" + WatsonxAiConstants.INPUTS + "'");
 
         // Fall back to body
-        if (inputs == null || inputs.isEmpty()) {
+        if (inputs.isEmpty()) {
             Object body = in.getBody();
-            if (body instanceof List) {
-                inputs = (List<String>) body;
+            if (body instanceof List<?> list) {
+                inputs = toInputs(list, "message body");
             } else if (body != null) {
                 // Single input, read as text (as textGeneration does), so a 
byte[] or InputStream body works too
                 String text = readAsText(in, "the input text");
@@ -116,6 +116,31 @@ public abstract class AbstractWatsonxAiHandler implements 
WatsonxAiOperationHand
         return inputs;
     }
 
+    /**
+     * Checks the elements of a list of input texts: a String is an input 
text, and so is a value whose
+     * {@code toString()} is its text, such as a UUID or a URI (as for a 
single body since CAMEL-26521). Any other
+     * element fails with its index and class, instead of reaching the 
watsonx.ai SDK as something that is not text.
+     */
+    private static List<String> toInputs(List<?> list, String source) {
+        if (list == null || list.isEmpty()) {
+            return List.of();
+        }
+        List<String> inputs = new ArrayList<>(list.size());
+        for (int i = 0; i < list.size(); i++) {
+            Object element = list.get(i);
+            if (element instanceof String text) {
+                inputs.add(text);
+            } else if (isTextValue(element)) {
+                inputs.add(element.toString());
+            } else {
+                throw new IllegalArgumentException(
+                        "The input texts in the " + source + " must be 
Strings, but the element at index " + i + " is "
+                                                   + (element == null ? "null" 
: element.getClass().getName()));
+            }
+        }
+        return inputs;
+    }
+
     /**
      * Extracts chat messages from exchange headers or body.
      */
diff --git 
a/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
 
b/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
index 5d962d3c8019..493a23620786 100644
--- 
a/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
+++ 
b/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
@@ -464,6 +464,60 @@ class WatsonxAiMessageBodyTest extends CamelTestSupport {
         assertInputs(request("/ml/v1/text/embeddings").get("inputs"), "Apache 
Camel", "Integration");
     }
 
+    @Test
+    void embeddingFailsForAListWithAnotherElement() {
+        List<Object> body = new ArrayList<>();
+        body.add("Apache Camel");
+        body.add(42);
+
+        Exchange result = send(WatsonxAiOperations.embedding, body);
+
+        IllegalArgumentException e = 
assertInstanceOf(IllegalArgumentException.class, result.getException());
+        assertTrue(e.getMessage().contains("must be Strings"), e.getMessage());
+        assertTrue(e.getMessage().contains("index 1 is java.lang.Integer"), 
e.getMessage());
+        assertFalse(REQUESTS.containsKey("/ml/v1/text/embeddings"), "no 
request is sent");
+    }
+
+    @Test
+    void embeddingFailsForANullElementInTheInputsHeader() {
+        List<Object> inputs = new ArrayList<>();
+        inputs.add("Apache Camel");
+        inputs.add(null);
+
+        Exchange result = template.send("direct:watsonx", exchange -> {
+            exchange.getIn().setHeader(WatsonxAiConstants.OPERATION, 
WatsonxAiOperations.embedding);
+            exchange.getIn().setHeader(WatsonxAiConstants.INPUTS, inputs);
+        });
+
+        IllegalArgumentException e = 
assertInstanceOf(IllegalArgumentException.class, result.getException());
+        assertTrue(e.getMessage().contains("header '" + 
WatsonxAiConstants.INPUTS + "'"), e.getMessage());
+        assertTrue(e.getMessage().contains("index 1 is null"), e.getMessage());
+        assertFalse(REQUESTS.containsKey("/ml/v1/text/embeddings"), "no 
request is sent");
+    }
+
+    /** A value whose text is its toString() is read as text, as a single body 
is. */
+    @Test
+    void embeddingReadsAUuidInAListAsItsText() throws Exception {
+        UUID uuid = UUID.randomUUID();
+        call(WatsonxAiOperations.embedding, List.of("Apache Camel", uuid));
+
+        assertInputs(request("/ml/v1/text/embeddings").get("inputs"), "Apache 
Camel", uuid.toString());
+    }
+
+    @Test
+    void rerankFailsForAListWithAMapElement() {
+        List<Object> body = new ArrayList<>();
+        body.add("Apache Camel is an integration framework");
+        body.add(Map.of("text", "Camel routes messages"));
+
+        Exchange result = rerank(body);
+
+        IllegalArgumentException e = 
assertInstanceOf(IllegalArgumentException.class, result.getException());
+        assertTrue(e.getMessage().contains("must be Strings"), e.getMessage());
+        assertTrue(e.getMessage().contains("index 1 is java.util."), 
e.getMessage());
+        assertFalse(REQUESTS.containsKey("/ml/v1/text/rerank"), "no request is 
sent");
+    }
+
     @Test
     void embeddingFailsWithoutABody() {
         Exchange result = send(WatsonxAiOperations.embedding, null);

Reply via email to