Summary

This PR updates the MiniMaxChatModel class to properly handle both standard and web search response formats from the MiniMax API. It also improves overall null safety throughout the response processing logic and adds explanatory comments to the MiniMaxApi class.

Changes

  1. Updated call method in MiniMaxChatModel to handle both choice.message() and choice.messages().
  2. Improved null checking for message content and role information.
  3. Refactored buildGeneration method to handle different response formats.
  4. Enhanced error logging for cases where no valid message is found.
  5. Added explanatory comments to MiniMaxApi class regarding the messages field in web search responses.

Detailed Description

MiniMaxChatModel Changes

The call method in MiniMaxChatModel has been updated to handle both standard and web search response formats:

List generations = choices.stream().map(choice -> {
    ChatCompletionMessage message;
    if (choice.message() != null) {
        message = choice.message();
    } else if (choice.messages() != null && !choice.messages().isEmpty()) {
        message = choice.messages().get(0);
    } else {
        logger.warn("No message or messages found in choice: {}", choice);
        return null;
    }
    // ... rest of the method
}).filter(Objects::nonNull).toList();

This change allows the model to process both message and messages fields, accommodating the inconsistency in response formats between standard and web search methods.

MiniMaxApi Changes

Added explanatory comments to the MiniMaxApi class to clarify the purpose of the messages field:

/**
 * @param messages A list of chat completion messages generated by the model. Used in web search responses.
 *                 This field is used to handle the inconsistency in response formats between
 *                 standard and web search methods.
 */

These comments have been added to relevant parts of the MiniMaxApi class to improve code understanding and maintainability.

Testing

  • Updated MiniMaxChatModelWebSearchTest to cover both standard and web search response formats.
  • Added test cases for null message scenarios.
  • All existing tests pass with the new implementation.

How to Test

  1. Run the MiniMaxChatModelWebSearchTest class.
  2. Verify that both testStandardResponseHandling() and testWebSearchResponseHandling() pass.
  3. Check that null safety tests in MiniMaxChatModelNullSafetyTest pass.

Related Issue

Fix https://github.com/spring-projects/spring-ai/issues/1292

Additional Notes

  1. This PR resolves the inconsistencies in handling different response formats from the MiniMax API. It addresses potential NullPointerExceptions and provides a more consistent behavior across different response types.

  2. We suggest updating the official MiniMax API documentation to include information about the messages field in web search responses. This would help other developers understand and handle the different response formats correctly.

  3. Consider adding the following to the official documentation: "When using the web search feature, the API may return responses in the messages field instead of the message field. Clients should be prepared to handle both formats for full compatibility."

Next Steps

  • Update official MiniMax API documentation to reflect these changes and provide clear guidance on handling different response formats.
  • Consider standardizing the response format in future API versions to simplify client-side processing.

Comment From: injae-kim

Fix https://github.com/spring-projects/spring-ai/issues/1292

Comment From: markpollack

@mxsl-gr would you be able to review?

Comment From: mxsl-gr

@mxsl-gr would you be able to review?

this PR aims to resolve the issue https://github.com/spring-projects/spring-ai/issues/1292, but there are some minor implementation issues that will return wrong messages on actual calls, I have provided some feedback in the issue comments. and I think the issue fixed in PR https://github.com/spring-projects/spring-ai/pull/1326, I see you already merged it.

Comment From: markpollack

thanks for your review. If you have any issues @jun10920 please let us know. I do appreciate the effort, seems like it has been fixed already.

Comment From: jun10920

@markpollack Thank you as well! I will continue to show interest in open source and make sure to contribute again in the future.