Skip to content

Conversation

@jonathan-buttner
Copy link
Contributor

@jonathan-buttner jonathan-buttner commented Aug 5, 2025

This PR is a follow on for #131316

It takes the same approach and propagates the changes to the rest of the chat completion handlers.

This tries to remove a lot of the duplication.

@jonathan-buttner jonathan-buttner added >refactoring :ml Machine learning Team:ML Meta label for the ML team v9.2.0 labels Aug 5, 2025

public class UnifiedChatCompletionErrorResponse extends ErrorResponse {

private static final ConstructingObjectParser<Optional<UnifiedChatCompletionErrorResponse>, Void> CONSTRUCTING_OBJECT_PARSER =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new lines were taken from StreamingErrorResponse. I consolidated the classes.

* TODO: {@link ErrorMessageResponseEntity} is nearly identical to this, but doesn't parse as many fields. We must remove the duplication.
*/
public class StreamingErrorResponse extends ErrorResponse {
private static final ConstructingObjectParser<Optional<ErrorResponse>, Void> ERROR_PARSER = new ConstructingObjectParser<>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the parsing functionality to UnifiedChatCompletionErrorResponse.

* @param parseFunction The function to parse the response.
*/
public MistralUnifiedChatCompletionResponseHandler(String requestType, ResponseParser parseFunction) {
super(requestType, parseFunction, ErrorResponse::fromResponse);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MistralErrorResponseHelper::fromResponse basically does the same thing as ErrorResponse::fromResponse aka convert the response into a string.

"error": {
"code": "stream_error",
"message": "Received an error response for request from inference entity id [id].\
Error message: [400: Invalid value: Model 'llama3.12:3b' not found]",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this changed is because I decided to just convert the whole error into a string for all cases for llama.

}
}

private static String code(@Nullable String code, RestStatus status) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an oversight from the previous PR. The OpenAI implementation either uses the provided code or the rest status code.

@jonathan-buttner jonathan-buttner marked this pull request as ready for review August 13, 2025 12:58
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/ml-core (Team:ML)

public class MistralErrorResponseHelper {
private static final String MISTRAL_ERROR = "mistral_error";

public static final UnifiedChatCompletionErrorParserContract ERROR_PARSER = UnifiedChatCompletionErrorResponseUtils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might just be easier to inline this into MistralUnifiedChatCompletionResponseHandler and have the constructor call ERROR_PARSER::parse directly, we can take the above comment and paste it into the unit tests so we don't lose that context

Copy link
Member

@davidkyle davidkyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


public class UnifiedChatCompletionErrorResponse extends ErrorResponse {

private static final ConstructingObjectParser<Optional<UnifiedChatCompletionErrorResponse>, Void> CONSTRUCTING_OBJECT_PARSER =
Copy link
Member

@davidkyle davidkyle Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit rename CONSTRUCTING_OBJECT_PARSER -> ERROR_OBJECT_PARSER

Comment on lines 48 to 49
* Standard error response parser. This can be overridden for those subclasses that
* have a different error response structure.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit static methods cannot be overridden

Suggested change
* Standard error response parser. This can be overridden for those subclasses that
* have a different error response structure.
* Standard error response parser.

import static org.mockito.Mockito.mock;

public class LlamaErrorResponseTests extends ESTestCase {
public class MistralErrorResponseHelperTests extends ESTestCase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to duplicate this file or rename it? Is LlamaErrorResponseTests.java now missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think git flagged this wrong or something. LlamaErrorResponse no longer exists (I inlined the contents in LlamaChatCompletionResponseHandler).

I'm going to do what Pat suggested actually and MistralErrorResponseHelperTests will be removed shortly too.

@jonathan-buttner jonathan-buttner merged commit 9877bab into elastic:main Aug 19, 2025
34 checks passed
@jonathan-buttner jonathan-buttner deleted the ml-refactor-streaming-openai branch August 19, 2025 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:ml Machine learning >refactoring Team:ML Meta label for the ML team v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants