From c1a83b60348739472aa31c36180d2c363789edba Mon Sep 17 00:00:00 2001 From: Andrea Cosentino Date: Mon, 31 Aug 2026 10:18:06 +0200 Subject: [PATCH 1/2] CAMEL-24538: camel-openai - fix operator precedence so an empty userMessage does not drop the body prompt buildUserMessage used `userPrompt == null || userPrompt.isEmpty() && isNotEmpty(config.getUserMessage())`. Because && binds tighter than ||, this is `userPrompt == null || (userPrompt.isEmpty() && ...)`, so when the USER_MESSAGE header is absent and the configured userMessage is an empty string, userPrompt was set to that empty string and buildTextMessage then used "" instead of the message body, failing with "No input provided". Parenthesize as `(userPrompt == null || userPrompt.isEmpty()) && isNotEmpty(...)` so the configured message is only used when it is actually set, otherwise the body is used. The same precedence is corrected for the (benign) system-message twin for consistency. Co-Authored-By: Claude Opus 4.8 Signed-off-by: Andrea Cosentino --- .../component/openai/OpenAIProducer.java | 4 +- .../OpenAIEmptyUserMessageBodyPromptTest.java | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIEmptyUserMessageBodyPromptTest.java diff --git a/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java b/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java index 04b6cbafed051..3d134e312ccf4 100644 --- a/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java +++ b/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java @@ -251,7 +251,7 @@ private List buildMessages(Exchange exchange, OpenAI String systemPrompt = in.getHeader(OpenAIConstants.SYSTEM_MESSAGE, String.class); String developerPrompt = in.getHeader(OpenAIConstants.DEVELOPER_MESSAGE, String.class); - if (systemPrompt == null || systemPrompt.isEmpty() && ObjectHelper.isNotEmpty(config.getSystemMessage())) { + if ((systemPrompt == null || systemPrompt.isEmpty()) && ObjectHelper.isNotEmpty(config.getSystemMessage())) { systemPrompt = config.getSystemMessage(); } if (developerPrompt == null @@ -308,7 +308,7 @@ private void addConversationHistory( private ChatCompletionMessageParam buildUserMessage(Message in, OpenAIConfiguration config) throws Exception { Object body = in.getBody(); String userPrompt = in.getHeader(OpenAIConstants.USER_MESSAGE, String.class); - if (userPrompt == null || userPrompt.isEmpty() && ObjectHelper.isNotEmpty(config.getUserMessage())) { + if ((userPrompt == null || userPrompt.isEmpty()) && ObjectHelper.isNotEmpty(config.getUserMessage())) { userPrompt = config.getUserMessage(); } diff --git a/components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIEmptyUserMessageBodyPromptTest.java b/components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIEmptyUserMessageBodyPromptTest.java new file mode 100644 index 0000000000000..b8a871657673d --- /dev/null +++ b/components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIEmptyUserMessageBodyPromptTest.java @@ -0,0 +1,69 @@ +/* + * 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.openai; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.infra.openai.mock.OpenAIMock; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * When the userMessage option is configured to an empty string, it must not shadow the prompt supplied in the message + * body. An operator-precedence bug used to set the (empty) configured message as the prompt, dropping the body and + * failing with "No input provided". + */ +public class OpenAIEmptyUserMessageBodyPromptTest extends CamelTestSupport { + + @RegisterExtension + public OpenAIMock openAIMock = new OpenAIMock().builder() + .when("hello from body") + .replyWith("mock reply") + .end() + .build(); + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:empty-user-message") + .to("openai:chat-completion?model=gpt-4&apiKey=dummy&baseUrl=" + + openAIMock.getBaseUrl() + "/v1"); + } + }; + } + + @Test + void bodyPromptIsUsedWhenConfiguredUserMessageIsEmpty() { + // Configure the route's endpoint with an empty userMessage - the exact case the precedence bug mishandled. + OpenAIEndpoint endpoint = context.getEndpoints().stream() + .filter(OpenAIEndpoint.class::isInstance) + .map(OpenAIEndpoint.class::cast) + .findFirst() + .orElseThrow(() -> new IllegalStateException("no OpenAIEndpoint found")); + endpoint.getConfiguration().setUserMessage(""); + + Exchange result = template.request("direct:empty-user-message", e -> e.getIn().setBody("hello from body")); + + assertThat(result.getException()).isNull(); + assertThat(result.getMessage().getBody(String.class)).contains("mock reply"); + } +} From b641d806850c13bf1ec0ea12827cd1063f12242b Mon Sep 17 00:00:00 2001 From: Andrea Cosentino Date: Mon, 31 Aug 2026 14:37:53 +0200 Subject: [PATCH 2/2] CAMEL-24538: apply the same precedence fix to the developerPrompt branch Spotted by Claus Ibsen on the PR. The developerPrompt guard had the same missing parentheses as userPrompt and systemPrompt: if (developerPrompt == null || developerPrompt.isEmpty() && ObjectHelper.isNotEmpty(config.getDeveloperMessage())) A null header therefore assigned the configured message unconditionally instead of only when one is configured. Harmless today, since the downstream ObjectHelper.isNotEmpty(developerPrompt) guard drops a null before the message is added, but leaving one of three identical instances unfixed was an oversight. Co-authored-by: Claude Opus 5 (1M context) --- .../org/apache/camel/component/openai/OpenAIProducer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java b/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java index 3d134e312ccf4..6cb5d85c4dc39 100644 --- a/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java +++ b/components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIProducer.java @@ -254,8 +254,8 @@ private List buildMessages(Exchange exchange, OpenAI if ((systemPrompt == null || systemPrompt.isEmpty()) && ObjectHelper.isNotEmpty(config.getSystemMessage())) { systemPrompt = config.getSystemMessage(); } - if (developerPrompt == null - || developerPrompt.isEmpty() && ObjectHelper.isNotEmpty(config.getDeveloperMessage())) { + if ((developerPrompt == null || developerPrompt.isEmpty()) + && ObjectHelper.isNotEmpty(config.getDeveloperMessage())) { developerPrompt = config.getDeveloperMessage(); }