From e93b42f378c8bf9e45a4100679d770e5a8d52ef7 Mon Sep 17 00:00:00 2001 From: dan-rubinstein Date: Mon, 21 Jul 2025 13:35:14 -0400 Subject: [PATCH 1/3] Add exception for perform embedding inference requests with query provided --- .../inference/action/InferenceAction.java | 8 ++++ .../action/InferenceActionRequestTests.java | 38 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java index 64957328d48dd..2fc429132ce62 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java @@ -273,6 +273,14 @@ public ActionRequestValidationException validate() { } } + if (taskType.equals(TaskType.TEXT_EMBEDDING) || taskType.equals(TaskType.SPARSE_EMBEDDING)) { + if(query != null) { + var e = new ActionRequestValidationException(); + e.addValidationError(format("Field [query] cannot be specified for task type [%s]", taskType)); + return e; + } + } + if (taskType.equals(TaskType.TEXT_EMBEDDING) == false && taskType.equals(TaskType.ANY) == false && (inputType != null && InputType.isInternalTypeOrUnspecified(inputType) == false)) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java index 2e2b9bf9b0d23..36bad1c1a7c7f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java @@ -191,6 +191,24 @@ public void testValidation_TextEmbedding_WithTopN() { assertThat(inputError.getMessage(), is("Validation Failed: 1: Field [top_n] cannot be specified for task type [text_embedding];")); } + public void testValidation_TextEmbedding_WithQuery() { + InferenceAction.Request queryRequest = new InferenceAction.Request( + TaskType.TEXT_EMBEDDING, + "model", + "query", + null, + null, + List.of("input"), + null, + null, + null, + false + ); + ActionRequestValidationException queryError = queryRequest.validate(); + assertNotNull(queryError); + assertThat(queryError.getMessage(), is("Validation Failed: 1: Field [query] cannot be specified for task type [text_embedding];")); + } + public void testValidation_Rerank_Null() { InferenceAction.Request queryNullRequest = new InferenceAction.Request( TaskType.RERANK, @@ -249,7 +267,7 @@ public void testValidation_SparseEmbedding_WithInputType() { InferenceAction.Request queryRequest = new InferenceAction.Request( TaskType.SPARSE_EMBEDDING, "model", - "", + null, null, null, List.of("input"), @@ -309,6 +327,24 @@ public void testValidation_SparseEmbedding_WithTopN() { ); } + public void testValidation_SparseEmbedding_WithQuery() { + InferenceAction.Request queryRequest = new InferenceAction.Request( + TaskType.SPARSE_EMBEDDING, + "model", + "query", + null, + null, + List.of("input"), + null, + null, + null, + false + ); + ActionRequestValidationException queryError = queryRequest.validate(); + assertNotNull(queryError); + assertThat(queryError.getMessage(), is("Validation Failed: 1: Field [query] cannot be specified for task type [sparse_embedding];")); + } + public void testValidation_Completion_WithInputType() { InferenceAction.Request queryRequest = new InferenceAction.Request( TaskType.COMPLETION, From cfa398a74951d81b227c2f334fe1b2aa8379ad05 Mon Sep 17 00:00:00 2001 From: Dan Rubinstein Date: Mon, 21 Jul 2025 14:55:38 -0400 Subject: [PATCH 2/3] Update docs/changelog/131641.yaml --- docs/changelog/131641.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/changelog/131641.yaml diff --git a/docs/changelog/131641.yaml b/docs/changelog/131641.yaml new file mode 100644 index 0000000000000..7d86eed413611 --- /dev/null +++ b/docs/changelog/131641.yaml @@ -0,0 +1,5 @@ +pr: 131641 +summary: Add exception for perform embedding inference requests with query provided +area: Machine Learning +type: bug +issues: [] From 59cc27495635f216d5eb297a8683bf70f8f1c7ff Mon Sep 17 00:00:00 2001 From: elasticsearchmachine Date: Mon, 21 Jul 2025 19:05:11 +0000 Subject: [PATCH 3/3] [CI] Auto commit changes from spotless --- .../xpack/core/inference/action/InferenceAction.java | 2 +- .../core/inference/action/InferenceActionRequestTests.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java index 2fc429132ce62..c23996a3ce87a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java @@ -274,7 +274,7 @@ public ActionRequestValidationException validate() { } if (taskType.equals(TaskType.TEXT_EMBEDDING) || taskType.equals(TaskType.SPARSE_EMBEDDING)) { - if(query != null) { + if (query != null) { var e = new ActionRequestValidationException(); e.addValidationError(format("Field [query] cannot be specified for task type [%s]", taskType)); return e; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java index 36bad1c1a7c7f..696b45117497a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java @@ -342,7 +342,10 @@ public void testValidation_SparseEmbedding_WithQuery() { ); ActionRequestValidationException queryError = queryRequest.validate(); assertNotNull(queryError); - assertThat(queryError.getMessage(), is("Validation Failed: 1: Field [query] cannot be specified for task type [sparse_embedding];")); + assertThat( + queryError.getMessage(), + is("Validation Failed: 1: Field [query] cannot be specified for task type [sparse_embedding];") + ); } public void testValidation_Completion_WithInputType() {