You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "atiaomar1978-hub (via GitHub)" <gi...@apache.org> on 2026/07/18 07:02:57 UTC
[PR] CAMEL-23961: Fail clearly when chat completion has empty choices [camel]
atiaomar1978-hub opened a new pull request, #24882:
URL: https://github.com/apache/camel/pull/24882
**CAMEL-23961 is done.**
- **Branch:** `CAMEL-23961-empty-choices-response` (pushed)
- **Commit:** `36fc75910d3`
- **Fix:** Empty `choices` arrays now throw `CamelExchangeException` instead of `IndexOutOfBoundsException` in simple and agentic non-streaming paths
- **Test:** `OpenAIEmptyChoicesResponseTest` — **PASSED**
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-23961: Fail clearly when chat completion has empty choices [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24882:
URL: https://github.com/apache/camel/pull/24882#issuecomment-5010558476
<!-- ci-tested-modules -->
:test_tube: **CI tested the following changed modules:**
- `components/camel-ai/camel-openai`
---
<details><summary>:microscope: Scalpel shadow comparison — Scalpel: 9 tested, 29 compile-only — current: 9 all tested</summary>
[Maveniverse Scalpel](https://github.com/maveniverse/scalpel) detected **38 affected modules** (current approach: 9).
<details><summary>:warning: Modules only in Scalpel (29)</summary>
- `apache-camel`
- `camel-allcomponents`
- `camel-catalog`
- `camel-catalog-console`
- `camel-catalog-lucene`
- `camel-catalog-maven`
- `camel-catalog-suggest`
- `camel-componentdsl`
- `camel-csimple-maven-plugin`
- `camel-endpointdsl`
- `camel-endpointdsl-support`
- `camel-itest`
- `camel-jbang-core`
- `camel-jbang-it`
- `camel-jbang-main`
- `camel-jbang-plugin-edit`
- `camel-jbang-plugin-generate`
- `camel-jbang-plugin-kubernetes`
- `camel-jbang-plugin-test`
- `camel-kamelet-main`
- `camel-launcher`
- `camel-report-maven-plugin`
- `camel-route-parser`
- `camel-yaml-dsl`
- `camel-yaml-dsl-deserializers`
- `camel-yaml-dsl-maven-plugin`
- `coverage`
- `docs`
- `dummy-component`
</details>
**Skip-tests mode would test 9 modules** (1 direct + 8 downstream), **skip tests for 29** (generated code, meta-modules)
<details><summary>Modules Scalpel would test (9)</summary>
- `camel-jbang-mcp`
- `camel-jbang-plugin-mcp`
- `camel-jbang-plugin-route-parser`
- `camel-jbang-plugin-tui`
- `camel-jbang-plugin-validate`
- `camel-launcher-container`
- `camel-openai`
- `camel-yaml-dsl-validator`
- `camel-yaml-dsl-validator-maven-plugin`
</details>
<details><summary>Modules with tests skipped (29)</summary>
- `apache-camel`
- `camel-allcomponents`
- `camel-catalog`
- `camel-catalog-console`
- `camel-catalog-lucene`
- `camel-catalog-maven`
- `camel-catalog-suggest`
- `camel-componentdsl`
- `camel-csimple-maven-plugin`
- `camel-endpointdsl`
- `camel-endpointdsl-support`
- `camel-itest`
- `camel-jbang-core`
- `camel-jbang-it`
- `camel-jbang-main`
- `camel-jbang-plugin-edit`
- `camel-jbang-plugin-generate`
- `camel-jbang-plugin-kubernetes`
- `camel-jbang-plugin-test`
- `camel-kamelet-main`
- `camel-launcher`
- `camel-report-maven-plugin`
- `camel-route-parser`
- `camel-yaml-dsl`
- `camel-yaml-dsl-deserializers`
- `camel-yaml-dsl-maven-plugin`
- `coverage`
- `docs`
- `dummy-component`
</details>
> :information_source: Shadow mode — Scalpel observes but does not affect test execution. [Learn more](https://github.com/maveniverse/scalpel)
</details>
:warning: **Some tests are disabled on GitHub Actions** (`@DisabledIfSystemProperty(named = "ci.env.name")`) and require manual verification:
- `components/camel-ai/camel-openai`: 6 test(s) disabled on GitHub Actions
<details><summary>All tested modules (38 modules)</summary>
- Camel :: AI :: OpenAI
- Camel :: All Components Sync point
- Camel :: Assembly
- Camel :: Catalog :: CSimple Maven Plugin (deprecated)
- Camel :: Catalog :: Camel Catalog
- Camel :: Catalog :: Camel Report Maven Plugin
- Camel :: Catalog :: Camel Route Parser
- Camel :: Catalog :: Console
- Camel :: Catalog :: Dummy Component
- Camel :: Catalog :: Lucene (deprecated)
- Camel :: Catalog :: Maven
- Camel :: Catalog :: Suggest
- Camel :: Component DSL
- Camel :: Coverage
- Camel :: Docs
- Camel :: Endpoint DSL
- Camel :: Endpoint DSL :: Support
- Camel :: Integration Tests
- Camel :: JBang :: Core
- Camel :: JBang :: Integration tests
- Camel :: JBang :: MCP
- Camel :: JBang :: Main
- Camel :: JBang :: Plugin :: Edit
- Camel :: JBang :: Plugin :: Generate
- Camel :: JBang :: Plugin :: Kubernetes
- Camel :: JBang :: Plugin :: MCP
- Camel :: JBang :: Plugin :: Route Parser
- Camel :: JBang :: Plugin :: TUI
- Camel :: JBang :: Plugin :: Testing
- Camel :: JBang :: Plugin :: Validate
- Camel :: Kamelet Main
- Camel :: Launcher
- Camel :: Launcher :: Container
- Camel :: YAML DSL
- Camel :: YAML DSL :: Deserializers
- Camel :: YAML DSL :: Maven Plugins
- Camel :: YAML DSL :: Validator
- Camel :: YAML DSL :: Validator Maven Plugin
</details>
---
:gear: [View full build and test results](https://github.com/apache/camel/actions/runs/29636078431)
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-23961: Fail clearly when chat completion has empty choices [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24882:
URL: https://github.com/apache/camel/pull/24882#issuecomment-5010419881
:star2: Thank you for your contribution to the Apache Camel project! :star2:
:robot: CI automation will test this PR automatically.
:camel: Apache Camel Committers, please review the following items:
* First-time contributors **require MANUAL approval** for the GitHub Actions to run
* You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot although they are normally detected and executed by CI.
* You can label PRs using `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
* Build and test logs are available in the summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary.
:warning: Be careful when sharing logs. Review their contents before sharing them publicly.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-23961: Fail clearly when chat completion has empty choices [camel]
Posted by "atiaomar1978-hub (via GitHub)" <gi...@apache.org>.
atiaomar1978-hub commented on PR #24882:
URL: https://github.com/apache/camel/pull/24882#issuecomment-5010359222
## CAMEL-23961 — Summary
**Issue:** [CAMEL-23961](https://issues.apache.org/jira/browse/CAMEL-23961) — Empty `choices` array causes raw `IndexOutOfBoundsException`
**Component:** `camel-openai`
**Branch:** `CAMEL-23961-empty-choices-response` (pushed)
---
### Problem
When an OpenAI-compatible provider returned HTTP 200 with an empty `choices` array, `OpenAIProducer` failed with:
```
IndexOutOfBoundsException: Index 0 out of bounds for length 0
```
This was inconsistent with `setResponseHeaders()` and `OpenAIToolExecutionProducer`, which already guard against empty choices.
---
### Fix
| Area | Change |
|------|--------|
| **New helper** | `requireFirstChoice(exchange, response)` |
| **Behavior** | Throws `CamelExchangeException("OpenAI response contained no choices", exchange)` |
| **Paths covered** | Non-streaming **simple** and **agentic** loops |
---
### Commits
| Commit | Description |
|--------|-------------|
| `36fc75910d3` | Main fix + Jira reproducer test |
| `8b62d8067ae` | AssertJ assertions (review feedback) |
---
### Files Changed
| File | Change |
|------|--------|
| `OpenAIProducer.java` | Validate `choices` before `get(0)` |
| `OpenAIEmptyChoicesResponseTest.java` | New test (AssertJ) |
---
### Tests
| Test | Result |
|------|--------|
| `OpenAIEmptyChoicesResponseTest` | **PASSED** |
**Build:** `BUILD SUCCESS`
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-23961: Fail clearly when chat completion has empty choices [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus merged PR #24882:
URL: https://github.com/apache/camel/pull/24882
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-23961: Fail clearly when chat completion has empty choices [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #24882:
URL: https://github.com/apache/camel/pull/24882#discussion_r3607910168
##########
components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIEmptyChoicesResponseTest.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+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;
+
+/**
+ * Reproducer for a robustness gap: a chat completion response with an empty {@code choices} array fails the exchange
+ * with a raw {@code IndexOutOfBoundsException} because {@code OpenAIProducer.processNonStreamingSimple} dereferences
+ * {@code response.choices().get(0)} unguarded — while {@code setResponseHeaders} in the same class does guard against
+ * empty choices. Empty {@code choices} payloads are known in the OpenAI-compatible ecosystem from the streaming side
+ * (Azure OpenAI content-filter chunks, OpenRouter usage-only final chunks); for non-streaming this is defensive
+ * hardening against misbehaving compatible shims/proxies, which should surface as a meaningful exception.
+ */
+public class OpenAIEmptyChoicesResponseTest extends CamelTestSupport {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ @RegisterExtension
+ public OpenAIMock openAIMock = new OpenAIMock().builder()
+ .when("hello")
+ .thenRespondWith((exchange, input) -> {
+ try {
+ Map<String, Object> completion = new HashMap<>();
+ completion.put("id", UUID.randomUUID().toString());
+ completion.put("choices", List.of());
+ completion.put("created", System.currentTimeMillis() / 1000L);
+ completion.put("model", "openai-mock");
+ completion.put("object", "chat.completion");
+ return MAPPER.writeValueAsString(completion);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ })
+ .end()
+ .build();
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:chat")
+ .to("openai:chat-completion?model=gpt-5&apiKey=dummy&baseUrl="
+ + openAIMock.getBaseUrl() + "/v1");
+ }
+ };
+ }
+
+ @Test
+ void emptyChoicesMustFailWithMeaningfulException() {
+ Exchange result = template.request("direct:chat", e -> e.getIn().setBody("hello"));
+
+ assertThat(result.getException())
+ .as("An empty choices array cannot produce a response body")
+ .isNotNull()
Review Comment:
**Suggestion (non-blocking):** The negative assertion (`isNotInstanceOf(IndexOutOfBoundsException.class)`) would still pass if the code threw an unrelated exception (e.g., NPE). Consider positively asserting the expected type:
```suggestion
assertThat(result.getException())
.as("An empty choices array cannot produce a response body")
.isInstanceOf(CamelExchangeException.class)
.hasMessageContaining("no choices");
```
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org