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 9fbb4b3970d8 CAMEL-24537: camel-openai - require model for chat 
completion
9fbb4b3970d8 is described below

commit 9fbb4b3970d8d5c024e20943be5c76f7173cec6e
Author: Andrea Cosentino <[email protected]>
AuthorDate: Sun Aug 30 13:46:50 2026 +0200

    CAMEL-24537: camel-openai - require model for chat completion
    
    OpenAIProducer resolved the model from the endpoint option or the 
CamelOpenAIModel
    header and passed it straight to 
ChatCompletionCreateParams.builder().model(model)
    without a null check. With no model option, no default and no header, model 
was
    null and the call failed deep in the OpenAI SDK with an opaque 
NullPointerException.
    
    Every sibling producer (responses, embeddings, moderation, audio, image) 
already
    validates this. Add the same guard so the chat-completion producer fails 
with a
    clear IllegalArgumentException instead.
    
    Closes #25897
    
    Co-Authored-By: Claude Opus 4.8 <[email protected]>
---
 .../camel/component/openai/OpenAIProducer.java     |  3 ++
 .../OpenAIChatCompletionMissingModelTest.java      | 52 ++++++++++++++++++++++
 2 files changed, 55 insertions(+)

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 46d363564335..ec2fe6c776e2 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
@@ -154,6 +154,9 @@ public class OpenAIProducer extends DefaultAsyncProducer {
 
         // Resolve parameters from headers or configuration
         String model = resolveParameter(in, OpenAIConstants.MODEL, 
config.getModel(), String.class);
+        if (model == null) {
+            throw new IllegalArgumentException("Model must be specified via 
model parameter or CamelOpenAIModel header");
+        }
         Double temperature = resolveParameter(in, OpenAIConstants.TEMPERATURE, 
config.getTemperature(), Double.class);
         Double topP = resolveParameter(in, OpenAIConstants.TOP_P, 
config.getTopP(), Double.class);
         Integer maxTokens = resolveParameter(in, OpenAIConstants.MAX_TOKENS, 
config.getMaxTokens(), Integer.class);
diff --git 
a/components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIChatCompletionMissingModelTest.java
 
b/components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIChatCompletionMissingModelTest.java
new file mode 100644
index 000000000000..c7649bd2f745
--- /dev/null
+++ 
b/components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIChatCompletionMissingModelTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The chat-completion producer must fail with a clear error when no model is 
configured, matching the other producers,
+ * instead of passing null to the OpenAI client and surfacing an opaque 
NullPointerException.
+ */
+public class OpenAIChatCompletionMissingModelTest extends CamelTestSupport {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // No model on the endpoint and no model header; a fake 
baseUrl is enough because the guard
+                // rejects the exchange before any request is sent.
+                from("direct:chat-no-model")
+                        
.to("openai:chat-completion?apiKey=dummy&baseUrl=http://localhost:1/v1";);
+            }
+        };
+    }
+
+    @Test
+    void missingModelReportsIllegalArgumentException() {
+        Exchange result = template.request("direct:chat-no-model", e -> 
e.getIn().setBody("Hello"));
+        assertThat(result.getException())
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("Model must be specified");
+    }
+}

Reply via email to