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 9df487e0429d CAMEL-26521: camel-ibm-watsonx-ai - a Map or a POJO body
fails instead of reaching the model as its toString() (#30256)
9df487e0429d is described below
commit 9df487e0429d6632230369ebc457542688b09b72
Author: Karol Krawczyk <[email protected]>
AuthorDate: Tue Nov 17 16:07:51 2026 +0100
CAMEL-26521: camel-ibm-watsonx-ai - a Map or a POJO body fails instead of
reaching the model as its toString() (#30256)
The operations that read the message body as text read it with the type
converter, so a Map or a POJO reached the
model as its toString(), for example a map written in Java notation. They
now fail with an IllegalArgumentException
that says to marshal the body first: the prompt of textGeneration,
textGenerationStreaming, tokenize, detect and
deploymentGenerate, the user message of chat, chatStreaming and
deploymentChat, the one text of embedding and rerank,
and the file path, job id and file name of the extraction and
classification operations that do not upload, which the
catalog describes in the same sentence.
A body is rejected only when the text the converter gives is the toString()
of the object, which is what
ToStringTypeConverter, the fallback that is never promoted, returns for an
object no other converter reads as text.
Text, bytes, streams, files, the List bodies of chat, embedding and rerank,
a ChatMessage body and a single value are
read as before, and so are the bodies CAMEL-26553 reports as lost in
camel-spring-ai-embeddings: a char[], a
ByteBuffer, and a custom type with its own converter to String.
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../camel/catalog/components/ibm-watsonx-ai.json | 2 +-
.../catalog/docs/ibm-watsonx-ai-component.adoc | 16 +++
.../component/ibm/watsonx/ai/ibm-watsonx-ai.json | 2 +-
.../src/main/docs/ibm-watsonx-ai-component.adoc | 16 +++
.../ibm/watsonx/ai/WatsonxAiEndpoint.java | 3 +-
.../ai/handler/AbstractWatsonxAiHandler.java | 62 +++++++++-
.../ai/handler/TextClassificationHandler.java | 8 +-
.../watsonx/ai/handler/TextExtractionHandler.java | 10 +-
.../ibm/watsonx/ai/WatsonxAiMessageBodyTest.java | 133 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 18 +++
10 files changed, 255 insertions(+), 15 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/ibm-watsonx-ai.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/ibm-watsonx-ai.json
index bc4da3afb2fb..5a92c88dc467 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/ibm-watsonx-ai.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/ibm-watsonx-ai.json
@@ -128,7 +128,7 @@
"CamelIBMWatsonxAiAssistantMessage": { "index": 64, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "com.ibm.watsonx.ai.chat.model.AssistantMessage", "deprecated":
false, "autowired": false, "secret": false, "description": "The full assistant
message from chat response", "constantName":
"org.apache.camel.component.ibm.watsonx.ai.WatsonxAiConstants#ASSISTANT_MESSAGE"
}
},
"bodyTypes": {
- "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "chat", "carriers": [ "text", "bytes", "stream", "list" ]
}, { "option": "operation", "value": "chatStreaming", "carriers": [ "text",
"bytes", "stream", "list" ] }, { "option": "operation", "value":
"deploymentChat", "carriers": [ "text", "bytes", "stream", "list" ] }, {
"option": "operation", "value": "embedding", "carriers": [ "text", "bytes",
"stream", "list" ] }, { "option": "operation", "value [...]
+ "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "chat", "carriers": [ "text", "bytes", "stream", "list" ]
}, { "option": "operation", "value": "chatStreaming", "carriers": [ "text",
"bytes", "stream", "list" ] }, { "option": "operation", "value":
"deploymentChat", "carriers": [ "text", "bytes", "stream", "list" ] }, {
"option": "operation", "value": "embedding", "carriers": [ "text", "bytes",
"stream", "list" ] }, { "option": "operation", "value [...]
"producerOut": { "carriers": [ "text" ], "javaType": "java.lang.String",
"byOption": [ { "option": "operation", "value": "embedding", "carriers": [
"list" ] }, { "option": "operation", "value": "rerank", "carriers": [ "list" ]
}, { "option": "operation", "value": "tokenize", "carriers": [ "value" ],
"javaType": "java.lang.Integer" }, { "option": "operation", "value": "detect",
"carriers": [ "list" ] }, { "option": "operation", "value": "forecast",
"carriers": [ "list" ] }, { "option" [...]
},
"properties": {
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ibm-watsonx-ai-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ibm-watsonx-ai-component.adoc
index de43b9c48420..7671aae07a57 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ibm-watsonx-ai-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ibm-watsonx-ai-component.adoc
@@ -208,6 +208,22 @@ NOTE: Document processing operations require Cloud Object
Storage (COS) configur
If you don't specify an operation explicitly, you must set it via the
`operation` parameter.
+=== Message body read as text
+
+Many operations read the message body as text: the prompt of `textGeneration`,
`textGenerationStreaming`, `tokenize`,
+`detect` and `deploymentGenerate`, the user message of the chat operations,
the texts of `embedding` and `rerank`, and the file
+path, job id or file name of the extraction and classification operations that
do not upload.
+
+Such a body can be a `String`, a `byte[]`, a stream, a file, anything else
Camel converts to its text, such as a
+`char[]` or a `ByteBuffer`, or a value whose text is its `toString()`: a
`UUID`, a `URI`, a `URL`, a `java.time` value
+or a `Date`.
+
+Any other body, such as a `Map` or a POJO, fails with an
`IllegalArgumentException`, since Camel reads it only as its
+`toString()`, which is a dump of its fields rather than a prompt. The rule is
the text itself: a body is rejected
+when the text Camel gives for it is no more than its `toString()`. Marshal
such a body first, for example with `marshal().json()`, set the body
+to the text, or, when the `toString()` of the object is the text to send, say
so with `convertBodyTo(String.class)`
+before the endpoint.
+
=== Common Use Cases
[cols="1,2,2"]
diff --git
a/components/camel-ibm/camel-ibm-watsonx-ai/src/generated/resources/META-INF/org/apache/camel/component/ibm/watsonx/ai/ibm-watsonx-ai.json
b/components/camel-ibm/camel-ibm-watsonx-ai/src/generated/resources/META-INF/org/apache/camel/component/ibm/watsonx/ai/ibm-watsonx-ai.json
index bc4da3afb2fb..5a92c88dc467 100644
---
a/components/camel-ibm/camel-ibm-watsonx-ai/src/generated/resources/META-INF/org/apache/camel/component/ibm/watsonx/ai/ibm-watsonx-ai.json
+++
b/components/camel-ibm/camel-ibm-watsonx-ai/src/generated/resources/META-INF/org/apache/camel/component/ibm/watsonx/ai/ibm-watsonx-ai.json
@@ -128,7 +128,7 @@
"CamelIBMWatsonxAiAssistantMessage": { "index": 64, "kind": "header",
"displayName": "", "group": "producer", "label": "", "required": false,
"javaType": "com.ibm.watsonx.ai.chat.model.AssistantMessage", "deprecated":
false, "autowired": false, "secret": false, "description": "The full assistant
message from chat response", "constantName":
"org.apache.camel.component.ibm.watsonx.ai.WatsonxAiConstants#ASSISTANT_MESSAGE"
}
},
"bodyTypes": {
- "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "chat", "carriers": [ "text", "bytes", "stream", "list" ]
}, { "option": "operation", "value": "chatStreaming", "carriers": [ "text",
"bytes", "stream", "list" ] }, { "option": "operation", "value":
"deploymentChat", "carriers": [ "text", "bytes", "stream", "list" ] }, {
"option": "operation", "value": "embedding", "carriers": [ "text", "bytes",
"stream", "list" ] }, { "option": "operation", "value [...]
+ "producerIn": { "carriers": [ "any" ], "byOption": [ { "option":
"operation", "value": "chat", "carriers": [ "text", "bytes", "stream", "list" ]
}, { "option": "operation", "value": "chatStreaming", "carriers": [ "text",
"bytes", "stream", "list" ] }, { "option": "operation", "value":
"deploymentChat", "carriers": [ "text", "bytes", "stream", "list" ] }, {
"option": "operation", "value": "embedding", "carriers": [ "text", "bytes",
"stream", "list" ] }, { "option": "operation", "value [...]
"producerOut": { "carriers": [ "text" ], "javaType": "java.lang.String",
"byOption": [ { "option": "operation", "value": "embedding", "carriers": [
"list" ] }, { "option": "operation", "value": "rerank", "carriers": [ "list" ]
}, { "option": "operation", "value": "tokenize", "carriers": [ "value" ],
"javaType": "java.lang.Integer" }, { "option": "operation", "value": "detect",
"carriers": [ "list" ] }, { "option": "operation", "value": "forecast",
"carriers": [ "list" ] }, { "option" [...]
},
"properties": {
diff --git
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/docs/ibm-watsonx-ai-component.adoc
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/docs/ibm-watsonx-ai-component.adoc
index de43b9c48420..7671aae07a57 100644
---
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/docs/ibm-watsonx-ai-component.adoc
+++
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/docs/ibm-watsonx-ai-component.adoc
@@ -208,6 +208,22 @@ NOTE: Document processing operations require Cloud Object
Storage (COS) configur
If you don't specify an operation explicitly, you must set it via the
`operation` parameter.
+=== Message body read as text
+
+Many operations read the message body as text: the prompt of `textGeneration`,
`textGenerationStreaming`, `tokenize`,
+`detect` and `deploymentGenerate`, the user message of the chat operations,
the texts of `embedding` and `rerank`, and the file
+path, job id or file name of the extraction and classification operations that
do not upload.
+
+Such a body can be a `String`, a `byte[]`, a stream, a file, anything else
Camel converts to its text, such as a
+`char[]` or a `ByteBuffer`, or a value whose text is its `toString()`: a
`UUID`, a `URI`, a `URL`, a `java.time` value
+or a `Date`.
+
+Any other body, such as a `Map` or a POJO, fails with an
`IllegalArgumentException`, since Camel reads it only as its
+`toString()`, which is a dump of its fields rather than a prompt. The rule is
the text itself: a body is rejected
+when the text Camel gives for it is no more than its `toString()`. Marshal
such a body first, for example with `marshal().json()`, set the body
+to the text, or, when the `toString()` of the object is the text to send, say
so with `convertBodyTo(String.class)`
+before the endpoint.
+
=== Common Use Cases
[cols="1,2,2"]
diff --git
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiEndpoint.java
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiEndpoint.java
index 5ee9184db7f4..691a7963bae2 100644
---
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiEndpoint.java
+++
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiEndpoint.java
@@ -95,7 +95,8 @@ import org.apache.camel.util.ObjectHelper;
description = "The prompt or text for
textGeneration, tokenize, detect and deploymentGenerate,"
+ " the file path, ID or file
name for the text extraction and classification"
+ " operations that do not
upload, read as text unless a header such as"
- + " CamelIBMWatsonxAiInput
gives it: a Map or a POJO is sent as its toString()."
+ + " CamelIBMWatsonxAiInput
gives it: a Map or a POJO fails, as Camel reads"
+ + " it only as its toString()."
+ " chat, chatStreaming and
deploymentChat take the user message, read as text the"
+ " same way, a ChatMessage,
or the conversation as a List of ChatMessage in which a"
+ " String is a user message
(any other element fails); embedding and rerank take"
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 b00d27057a89..5d8547e81bac 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
@@ -18,8 +18,14 @@ package org.apache.camel.component.ibm.watsonx.ai.handler;
import java.io.File;
import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+import java.time.temporal.Temporal;
+import java.time.temporal.TemporalAmount;
import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
+import java.util.UUID;
import com.ibm.watsonx.ai.chat.model.ChatMessage;
import com.ibm.watsonx.ai.chat.model.ChatParameters;
@@ -33,6 +39,8 @@ import
org.apache.camel.component.ibm.watsonx.ai.WatsonxAiConfiguration;
import org.apache.camel.component.ibm.watsonx.ai.WatsonxAiConstants;
import org.apache.camel.component.ibm.watsonx.ai.WatsonxAiEndpoint;
import org.apache.camel.component.ibm.watsonx.ai.support.FileInput;
+import org.apache.camel.spi.BodyTypeRegistry;
+import org.apache.camel.support.BodyFacts;
/**
* Abstract base class for watsonx.ai operation handlers providing common
helper methods.
@@ -65,7 +73,7 @@ public abstract class AbstractWatsonxAiHandler implements
WatsonxAiOperationHand
// Fall back to body
if (input == null || input.isEmpty()) {
- input = in.getBody(String.class);
+ input = readAsText(in, "the prompt");
}
if (input == null || input.isEmpty()) {
@@ -93,7 +101,7 @@ public abstract class AbstractWatsonxAiHandler implements
WatsonxAiOperationHand
inputs = (List<String>) body;
} else if (body != null) {
// Single input, read as text (as textGeneration does), so a
byte[] or InputStream body works too
- String text = in.getBody(String.class);
+ String text = readAsText(in, "the input text");
if (text != null) {
inputs = List.of(text);
}
@@ -134,7 +142,7 @@ public abstract class AbstractWatsonxAiHandler implements
WatsonxAiOperationHand
messages = List.of(chatMessage);
} else if (body != null) {
// The user message, read as text (as textGeneration
does), so a byte[] or InputStream body works too
- userMessage = in.getBody(String.class);
+ userMessage = readAsText(in, "the user message");
}
}
@@ -184,6 +192,54 @@ public abstract class AbstractWatsonxAiHandler implements
WatsonxAiOperationHand
return messages;
}
+ /**
+ * The body as the text an operation reads, whether it sends it to the
model or uses it as a file path or a request
+ * id: text, bytes, a stream or a file is read as it is, and so is any
other body Camel converts to its text, such
+ * as a {@code char[]} or a {@code ByteBuffer}. A Map or a POJO fails
instead, since Camel has no conversion for it
+ * other than its {@code toString()}, so the model would be asked about a
map written in Java notation, or the
+ * request would name such a map as its file (CAMEL-26521).
+ *
+ * @param in the inbound message whose body to read
+ * @param what the input in the words of the operation, such as "the
prompt", for the error message of a body that
+ * is not text
+ * @return the body as text, or {@code null} when the body is null
+ */
+ protected static String readAsText(Message in, String what) {
+ Object body = in.getBody();
+ String text = in.getBody(String.class);
+ if (text != null && isOnlyToString(body, text)) {
+ throw new IllegalArgumentException(
+ "The message body must be " + what + " as text (a String,
bytes, a stream or a file), but it is a "
+ + body.getClass().getName()
+ + ", which Camel reads only as
its toString(). Marshal it first, for"
+ + " example with
marshal().json(), set the body to the text, or, when"
+ + " the toString() of the
object is the text to send, convert it with"
+ + "
convertBodyTo(String.class)");
+ }
+ return text;
+ }
+
+ /**
+ * Whether the text is no more than the {@code toString()} of the body,
which {@code ToStringTypeConverter} gives
+ * for an object no type converter reads as text. A payload (text, bytes,
a stream, a file) or a single value (a
+ * number, a boolean) is text by itself, whatever its {@code toString()}.
+ */
+ private static boolean isOnlyToString(Object body, String text) {
+ return BodyFacts.kindOf(body) != BodyTypeRegistry.Kind.PAYLOAD &&
!isTextValue(body)
+ && text.equals(body.toString());
+ }
+
+ /**
+ * A value whose {@code toString()} is its text, rather than a dump of its
fields: an id, an address or a point in
+ * time. {@link BodyFacts} counts text, a number, a boolean and an enum as
a payload, but not these, and the type
+ * converter has nothing for them either, so they would otherwise be read
as only a {@code toString()} (raised in
+ * the review of CAMEL-26521).
+ */
+ private static boolean isTextValue(Object body) {
+ return body instanceof UUID || body instanceof URI || body instanceof
URL || body instanceof Temporal
+ || body instanceof TemporalAmount || body instanceof Date;
+ }
+
/**
* Extracts file/stream from message body, supporting Camel file
components.
*/
diff --git
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextClassificationHandler.java
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextClassificationHandler.java
index bc90cc352de7..b5e5cc9fb128 100644
---
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextClassificationHandler.java
+++
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextClassificationHandler.java
@@ -78,7 +78,7 @@ public class TextClassificationHandler extends
AbstractWatsonxAiHandler {
// Get file path from body or header
String filePath = in.getHeader(WatsonxAiConstants.FILE_PATH,
String.class);
if (filePath == null || filePath.isEmpty()) {
- filePath = in.getBody(String.class);
+ filePath = readAsText(in, "the file path");
}
if (filePath == null || filePath.isEmpty()) {
@@ -104,7 +104,7 @@ public class TextClassificationHandler extends
AbstractWatsonxAiHandler {
// Get classification ID from body or header
String classificationId =
in.getHeader(WatsonxAiConstants.CLASSIFICATION_ID, String.class);
if (classificationId == null || classificationId.isEmpty()) {
- classificationId = in.getBody(String.class);
+ classificationId = readAsText(in, "the classification id");
}
if (classificationId == null || classificationId.isEmpty()) {
@@ -211,7 +211,7 @@ public class TextClassificationHandler extends
AbstractWatsonxAiHandler {
// Get file name from body or header
String fileName = in.getHeader(WatsonxAiConstants.FILE_NAME,
String.class);
if (fileName == null || fileName.isEmpty()) {
- fileName = in.getBody(String.class);
+ fileName = readAsText(in, "the file name");
}
if (fileName == null || fileName.isEmpty()) {
throw new IllegalArgumentException(
@@ -233,7 +233,7 @@ public class TextClassificationHandler extends
AbstractWatsonxAiHandler {
// Get classification ID from body or header
String classificationId =
in.getHeader(WatsonxAiConstants.CLASSIFICATION_ID, String.class);
if (classificationId == null || classificationId.isEmpty()) {
- classificationId = in.getBody(String.class);
+ classificationId = readAsText(in, "the classification id");
}
if (classificationId == null || classificationId.isEmpty()) {
throw new IllegalArgumentException(
diff --git
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextExtractionHandler.java
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextExtractionHandler.java
index cfc0b1627000..66945364bf9f 100644
---
a/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextExtractionHandler.java
+++
b/components/camel-ibm/camel-ibm-watsonx-ai/src/main/java/org/apache/camel/component/ibm/watsonx/ai/handler/TextExtractionHandler.java
@@ -81,7 +81,7 @@ public class TextExtractionHandler extends
AbstractWatsonxAiHandler {
// Get file path from body or header
String filePath = in.getHeader(WatsonxAiConstants.FILE_PATH,
String.class);
if (filePath == null || filePath.isEmpty()) {
- filePath = in.getBody(String.class);
+ filePath = readAsText(in, "the file path");
}
if (filePath == null || filePath.isEmpty()) {
@@ -107,7 +107,7 @@ public class TextExtractionHandler extends
AbstractWatsonxAiHandler {
// Get extraction ID from body or header
String extractionId = in.getHeader(WatsonxAiConstants.EXTRACTION_ID,
String.class);
if (extractionId == null || extractionId.isEmpty()) {
- extractionId = in.getBody(String.class);
+ extractionId = readAsText(in, "the extraction id");
}
if (extractionId == null || extractionId.isEmpty()) {
@@ -201,7 +201,7 @@ public class TextExtractionHandler extends
AbstractWatsonxAiHandler {
// Get file name from body or header
String fileName = in.getHeader(WatsonxAiConstants.FILE_NAME,
String.class);
if (fileName == null || fileName.isEmpty()) {
- fileName = in.getBody(String.class);
+ fileName = readAsText(in, "the file name");
}
if (fileName == null || fileName.isEmpty()) {
throw new IllegalArgumentException(
@@ -231,7 +231,7 @@ public class TextExtractionHandler extends
AbstractWatsonxAiHandler {
// Get file name from body or header
String fileName = in.getHeader(WatsonxAiConstants.FILE_NAME,
String.class);
if (fileName == null || fileName.isEmpty()) {
- fileName = in.getBody(String.class);
+ fileName = readAsText(in, "the file name");
}
if (fileName == null || fileName.isEmpty()) {
throw new IllegalArgumentException(
@@ -253,7 +253,7 @@ public class TextExtractionHandler extends
AbstractWatsonxAiHandler {
// Get extraction ID from body or header
String extractionId = in.getHeader(WatsonxAiConstants.EXTRACTION_ID,
String.class);
if (extractionId == null || extractionId.isEmpty()) {
- extractionId = in.getBody(String.class);
+ extractionId = readAsText(in, "the extraction id");
}
if (extractionId == null || extractionId.isEmpty()) {
throw new IllegalArgumentException(
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 e8cc09d2c4e3..db2509bad62d 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
@@ -20,10 +20,14 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
+import java.net.URI;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -363,6 +367,38 @@ class WatsonxAiMessageBodyTest extends CamelTestSupport {
assertUserMessage("What is the capital of France?",
request("/ml/v1/text/chat").get("messages").get(0));
}
+ @Test
+ void chatReadsACharArrayBodyAsTheUserMessage() throws Exception {
+ Exchange result = call(WatsonxAiOperations.chat, "What is the capital
of France?".toCharArray());
+
+ assertEquals("Paris", result.getMessage().getBody());
+ assertUserMessage("What is the capital of France?",
request("/ml/v1/text/chat").get("messages").get(0));
+ }
+
+ @Test
+ void chatReadsAByteBufferBodyAsTheUserMessage() throws Exception {
+ Exchange result = call(WatsonxAiOperations.chat,
+ ByteBuffer.wrap("What is the capital of
France?".getBytes(StandardCharsets.UTF_8)));
+
+ assertEquals("Paris", result.getMessage().getBody());
+ assertUserMessage("What is the capital of France?",
request("/ml/v1/text/chat").get("messages").get(0));
+ }
+
+ /** A value whose text is its toString(), which the type converter has
nothing for. */
+ @Test
+ void chatReadsAUuidAUriAndAnInstantBodyAsTheirText() throws Exception {
+ UUID uuid = UUID.randomUUID();
+ call(WatsonxAiOperations.chat, uuid);
+ assertUserMessage(uuid.toString(),
request("/ml/v1/text/chat").get("messages").get(0));
+
+ call(WatsonxAiOperations.chat, URI.create("https://camel.apache.org"));
+ assertUserMessage("https://camel.apache.org",
request("/ml/v1/text/chat").get("messages").get(0));
+
+ Instant instant = Instant.parse("2026-01-01T00:00:00Z");
+ call(WatsonxAiOperations.chat, instant);
+ assertUserMessage(instant.toString(),
request("/ml/v1/text/chat").get("messages").get(0));
+ }
+
@Test
void chatFailsWithoutABody() {
Exchange result = send(WatsonxAiOperations.chat, null);
@@ -429,6 +465,103 @@ class WatsonxAiMessageBodyTest extends CamelTestSupport {
assertEquals("Apache Camel is an integration framework",
request.get("inputs").get(0).get("text").asText());
}
+ // a Map or a POJO body, which Camel reads only as its toString()
+
+ @Test
+ void chatFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.chat, Map.of("question",
"What is the capital of France?"));
+
+ assertOnlyToStringFailure(result, "the user message", "Map");
+ assertFalse(REQUESTS.containsKey("/ml/v1/text/chat"), "no request is
sent");
+ }
+
+ @Test
+ void chatFailsForAPojoBody() {
+ Exchange result = send(WatsonxAiOperations.chat, new Question("What is
the capital of France?"));
+
+ assertOnlyToStringFailure(result, "the user message",
Question.class.getName());
+ assertFalse(REQUESTS.containsKey("/ml/v1/text/chat"), "no request is
sent");
+ }
+
+ @Test
+ void chatStreamingFailsForAMapBody() {
+ Consumer<String> streamConsumer = chunk -> {
+ };
+ Exchange result = template.send("direct:watsonx", exchange -> {
+ exchange.getIn().setHeader(WatsonxAiConstants.OPERATION,
WatsonxAiOperations.chatStreaming);
+ exchange.getIn().setHeader(WatsonxAiConstants.STREAM_CONSUMER,
streamConsumer);
+ exchange.getIn().setBody(Map.of("question", "What is the capital
of France?"));
+ });
+
+ assertOnlyToStringFailure(result, "the user message", "Map");
+ assertFalse(REQUESTS.containsKey("/ml/v1/text/chat_stream"), "no
request is sent");
+ }
+
+ @Test
+ void deploymentChatFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.deploymentChat,
Map.of("question", "What is the capital of France?"));
+
+ assertOnlyToStringFailure(result, "the user message", "Map");
+
assertFalse(REQUESTS.containsKey("/ml/v1/deployments/my-deployment/text/chat"),
"no request is sent");
+ }
+
+ @Test
+ void embeddingFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.embedding, Map.of("text",
"Apache Camel"));
+
+ assertOnlyToStringFailure(result, "the input text", "Map");
+ assertFalse(REQUESTS.containsKey("/ml/v1/text/embeddings"), "no
request is sent");
+ }
+
+ @Test
+ void rerankFailsForAPojoBody() {
+ Exchange result = rerank(new Question("Apache Camel is an integration
framework"));
+
+ assertOnlyToStringFailure(result, "the input text",
Question.class.getName());
+ assertFalse(REQUESTS.containsKey("/ml/v1/text/rerank"), "no request is
sent");
+ }
+
+ /** textGeneration, tokenize, detect and deploymentGenerate read the
prompt the same way. */
+ @Test
+ void textGenerationFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.textGeneration,
Map.of("prompt", "What is the capital of France?"));
+
+ assertOnlyToStringFailure(result, "the prompt", "Map");
+ }
+
+ @Test
+ void tokenizeFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.tokenize, Map.of("prompt",
"What is the capital of France?"));
+
+ assertOnlyToStringFailure(result, "the prompt", "Map");
+ }
+
+ /** The extraction and classification operations read their file path and
job id the same way. */
+ @Test
+ void textExtractionFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.textExtraction,
Map.of("filePath", "report.pdf"));
+
+ assertOnlyToStringFailure(result, "the file path", "Map");
+ }
+
+ @Test
+ void textClassificationFetchFailsForAMapBody() {
+ Exchange result = send(WatsonxAiOperations.textClassificationFetch,
Map.of("id", "classification-1"));
+
+ assertOnlyToStringFailure(result, "the classification id", "Map");
+ }
+
+ private static void assertOnlyToStringFailure(Exchange result, String
what, String type) {
+ IllegalArgumentException e =
assertInstanceOf(IllegalArgumentException.class, result.getException());
+ assertTrue(e.getMessage().contains("must be " + what + " as text"),
e.getMessage());
+ assertTrue(e.getMessage().contains(type), e.getMessage());
+ assertTrue(e.getMessage().contains("Marshal it first"),
e.getMessage());
+ }
+
+ /** A POJO body, which has no type converter to text. */
+ private record Question(String question) {
+ }
+
private Exchange send(WatsonxAiOperations operation, Object body) {
return template.send("direct:watsonx", exchange -> {
exchange.getIn().setHeader(WatsonxAiConstants.OPERATION,
operation);
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index a3f16ba660e6..7c4c92bbbf34 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -9807,6 +9807,24 @@ endpoint.setJakartaServletWebApplication(
JakartaServletWebApplication.buildApplication(servletContext));
----
+=== camel-ibm-watsonx-ai - a Map or a POJO body fails where it was read as its
toString()
+
+The operations that read the message body as text now fail with an
`IllegalArgumentException` for a `Map` or a POJO
+body: the prompt of `textGeneration`, `textGenerationStreaming`, `tokenize`,
`detect` and `deploymentGenerate`, the
+user message of `chat`, `chatStreaming` and `deploymentChat`, the one text of
`embedding` and `rerank`, and the file
+path, job id or file name of the text extraction and classification operations
that do not upload. Before, Camel had
+no conversion for such a body other than the `toString()` of the object, so
the model was asked about a map written in
+Java notation, and the extraction and classification requests named that text
as their file.
+
+Text, a `byte[]`, a stream, a file, and any other body that Camel converts to
its text, such as a `char[]` or a
+`ByteBuffer`, are read as before. So are the `List` bodies of `chat`,
`embedding` and `rerank`, a `ChatMessage` body of
+the chat operations, a single value such as a number, and a value whose text
is its `toString()`: a `UUID`, a `URI`, a
+`URL`, a `java.time` value or a `Date`.
+
+The rule is the text itself: a body is rejected when the text Camel gives for
it is no more than its `toString()`.
+Marshal such a body first, for example with `marshal().json()`, set the body
to the text, or, when the `toString()` of
+the object is the text to send, say so with `convertBodyTo(String.class)`
before the endpoint.
+
=== camel-alibaba-kms - a Map or a POJO in the plaintext header fails, and
only generateDataKey reads numberOfBytes
A `Map` or a POJO in the `CamelAlibabaKmsPlaintext` header, or in the exchange
property of the same name, now fails