-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Switch TextExpansionQueryBuilder and TextEmbeddingQueryVectorBuilder to return 400 instead of 500 errors #135800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jonathan-buttner
merged 6 commits into
elastic:main
from
jonathan-buttner:ml-fix-text-expansion-query
Oct 2, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b306c9f
Switching to bad requests
jonathan-buttner bbd3fe7
Update docs/changelog/135800.yaml
jonathan-buttner f41afe7
Renaming test
jonathan-buttner 3394cf2
Using yaml test
jonathan-buttner 1a254d3
Merge branch 'main' of github.com:elastic/elasticsearch into ml-fix-t…
jonathan-buttner c99bc01
fixing test
jonathan-buttner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 135800 | ||
| summary: Switch `TextExpansionQueryBuilder` and `TextEmbeddingQueryVectorBuilder` | ||
| to return 400 instead of 500 errors | ||
| area: Machine Learning | ||
| type: bug | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
.../java/org/elasticsearch/xpack/ml/vectors/TextEmbeddingQueryVectorBuilderFailureTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.ml.vectors; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.inference.WeightedToken; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.test.client.NoOpClient; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.xpack.core.ml.action.InferModelAction; | ||
| import org.elasticsearch.xpack.core.ml.inference.results.TextExpansionResults; | ||
| import org.elasticsearch.xpack.core.ml.vectors.TextEmbeddingQueryVectorBuilder; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| public class TextEmbeddingQueryVectorBuilderFailureTests extends ESTestCase { | ||
|
|
||
| private static class AssertingClient extends NoOpClient { | ||
| private final TextEmbeddingQueryVectorBuilder queryVectorBuilder; | ||
|
|
||
| AssertingClient(ThreadPool threadPool, TextEmbeddingQueryVectorBuilder queryVectorBuilder) { | ||
| super(threadPool); | ||
| this.queryVectorBuilder = queryVectorBuilder; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute( | ||
| ActionType<Response> action, | ||
| Request request, | ||
| ActionListener<Response> listener | ||
| ) { | ||
| listener.onResponse((Response) createResponse(queryVectorBuilder.getModelId())); | ||
| } | ||
| } | ||
|
|
||
| public void testReceiving_TextExpansionResults_ThrowsBadRequestException() { | ||
| var queryVectorBuilder = createTestInstance(); | ||
|
|
||
| try (var threadPool = createThreadPool()) { | ||
| final var client = new AssertingClient(threadPool, queryVectorBuilder); | ||
| PlainActionFuture<float[]> future = new PlainActionFuture<>(); | ||
| queryVectorBuilder.buildVector(client, future); | ||
|
|
||
| var exception = expectThrows(IllegalArgumentException.class, () -> future.actionGet(TimeValue.timeValueSeconds(30))); | ||
| assertThat( | ||
| exception.getMessage(), | ||
| containsString("expected a result of type [text_embedding_result] received [text_expansion_result]") | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static ActionResponse createResponse(String modelId) { | ||
| return new InferModelAction.Response( | ||
| List.of(new TextExpansionResults("foo", List.of(new WeightedToken("toke", 0.1f)), randomBoolean())), | ||
| modelId, | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| private static TextEmbeddingQueryVectorBuilder createTestInstance() { | ||
| return new TextEmbeddingQueryVectorBuilder(randomAlphaOfLength(4), randomAlphaOfLength(4)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why do we need to use a semantic text field to get this error? That feels unrelated since it's not even used in the query.
We can also move it to
resources/rest-api-spec/test/ml/search_knn_query_vector_builder.yml?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah good point, I'll move it and remove the dependency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I can't seem to get it to work with a regular dense vector field:
I'm going to leave it in the semantic text tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks like another bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean we should return a 400 in that situation too instead of a 403? Yeah I typically would think the 403 would be for permissions issues.