gnodet commented on code in PR #25387:
URL: https://github.com/apache/camel/pull/25387#discussion_r3729633461


##########
components/camel-ai/camel-langchain4j-embeddingstore/src/main/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreProducer.java:
##########
@@ -236,6 +228,40 @@ private void search(Exchange exchange) throws Exception {
         }
     }
 
+    private EmbeddingResult resolveEmbedding(Exchange exchange) throws 
NoSuchHeaderException {
+        final Message in = exchange.getMessage();
+        LangChain4jEmbeddingStoreConfiguration config = 
getEndpoint().getConfiguration();
+
+        Embedding embedding = 
in.getHeader(LangChain4jEmbeddingsHeaders.EMBEDDING, Embedding.class);
+        TextSegment textSegment = 
in.getHeader(LangChain4jEmbeddingsHeaders.TEXT_SEGMENT, TextSegment.class);
+
+        if (embedding == null && config.getEmbeddingModel() != null) {
+            String text = in.getBody(String.class);

Review Comment:
   The null-body check here is unconditional within the auto-embed block, but 
it shouldn't be. When a `TextSegment` header is already present (fetched at 
line 236), the body text isn't needed — `embed(textSegment)` at line 249 will 
use the header-provided segment. Currently, sending a message with a 
`TextSegment` header but a null/non-String body will throw 
`IllegalArgumentException` even though there's enough information to compute 
the embedding.
   
   Consider moving the body-fetch and null-check inside the `if (textSegment == 
null)` block:
   
   ```suggestion
               if (textSegment == null) {
                   String text = in.getBody(String.class);
                   if (text == null) {
                       throw new IllegalArgumentException(
                               "Message body cannot be converted to String for 
auto-embedding. "
                                                          + "Either set the 
body to a text value or provide a pre-computed embedding via the "
                                                          + 
LangChain4jEmbeddingsHeaders.EMBEDDING + " header.");
                   }
                   textSegment = TextSegment.from(text);
               }
   ```
   
   Note: the existing test `addWithTextSegmentHeaderPreservedDuringAutoEmbed` 
always supplies a non-null body, so it doesn't catch this edge case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to