Python: Add MiniMax chat completion connector with regional endpoints - #14225
Python: Add MiniMax chat completion connector with regional endpoints#14225octo-patch wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new Python AI connector (semantic_kernel.connectors.ai.minimax) to integrate with MiniMax’s OpenAI-compatible chat completion API, including region-based endpoint selection, execution settings, docs, and unit tests—following the existing Nvidia connector structure.
Changes:
- Introduces
MiniMaxChatCompletion+ supporting settings/handler/prompt execution settings for MiniMax chat completions with regional endpoint resolution. - Adds unit tests covering settings, prompt execution settings, handler behavior, and basic chat completion behavior.
- Updates the Python AI connectors README/table and adds MiniMax connector documentation.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| python/tests/unit/connectors/conftest.py | Adds minimax_unit_test_env fixture for MiniMax-related unit tests. |
| python/tests/unit/connectors/ai/minimax/settings/test_minimax_settings.py | Tests for MiniMaxSettings defaults, env loading, and region→base URL resolution. |
| python/tests/unit/connectors/ai/minimax/services/test_minimax_handler.py | Tests MiniMaxHandler request routing and basic chat request behavior. |
| python/tests/unit/connectors/ai/minimax/services/test_minimax_chat_completion.py | Tests MiniMaxChatCompletion initialization and basic non-streaming chat completion/error handling. |
| python/tests/unit/connectors/ai/minimax/prompt_execution_settings/test_minimax_prompt_execution_settings.py | Tests execution settings serialization and temperature validation. |
| python/semantic_kernel/connectors/ai/README.md | Adds MiniMax to the connectors table. |
| python/semantic_kernel/connectors/ai/minimax/settings/minimax_settings.py | Adds MiniMaxSettings including region-based base URL resolution. |
| python/semantic_kernel/connectors/ai/minimax/settings/init.py | Package init for MiniMax settings. |
| python/semantic_kernel/connectors/ai/minimax/services/minimax_model_types.py | Adds MiniMaxModelTypes enum. |
| python/semantic_kernel/connectors/ai/minimax/services/minimax_handler.py | Adds OpenAI-client-based request handler and usage tracking for MiniMax. |
| python/semantic_kernel/connectors/ai/minimax/services/minimax_chat_completion.py | Implements the MiniMax chat completion service (non-streaming + streaming paths). |
| python/semantic_kernel/connectors/ai/minimax/services/init.py | Package init for MiniMax services. |
| python/semantic_kernel/connectors/ai/minimax/README.md | Adds MiniMax connector documentation, endpoints, env vars, and quick start. |
| python/semantic_kernel/connectors/ai/minimax/prompt_execution_settings/minimax_prompt_execution_settings.py | Adds prompt execution settings (incl. temperature range validation + serialization aliases). |
| python/semantic_kernel/connectors/ai/minimax/prompt_execution_settings/init.py | Package init for MiniMax prompt execution settings. |
| python/semantic_kernel/connectors/ai/minimax/init.py | Exposes the MiniMax connector public API symbols. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| response = await self._send_request(settings) | ||
| assert isinstance(response, AsyncGenerator) # nosec | ||
|
|
| - `MiniMax-M3` - Latest flagship model with a 1,000,000 token context window and text/image/video | ||
| input support (default). |
There was a problem hiding this comment.
Automated Code Review
Reviewers: 5 | Confidence: 87%
✓ Correctness
The MiniMax chat completion connector closely follows the existing Nvidia connector pattern. The implementation is correct: settings resolution handles None values properly via KernelBaseSettings, the handler safely guards streaming responses in store_usage, and the chat history preparation is safe since ChatMessageContent.content always returns str. No new correctness bugs are introduced beyond patterns already established in the codebase.
✓ Security Reliability
The MiniMax connector is well-structured and follows the existing Nvidia connector pattern. API key handling uses SecretStr in settings, region selection is constrained via Literal types preventing injection, and temperature is properly bounded. The to_dict() method exposes the plain-text API key, but this mirrors the pre-existing Nvidia connector pattern. No new security or reliability regressions are introduced by this PR.
✓ Test Coverage
The test suite covers initialization, settings, prompt execution settings validation, basic chat completion, and error handling. However, the streaming chat completion code path (
_inner_get_streaming_chat_message_contents) has meaningful branching logic (empty-choice filtering, chunk metadata extraction, streaming content construction) with zero test coverage. The existingtest_get_chat_message_contentstest also has shallow assertions that don't verify role, finish_reason, metadata, or usage propagation from the response. Thefrom_dictclass method is also untested. While the absence of streaming tests is consistent with the Nvidia connector pattern, the streaming implementation here has non-trivial logic that warants at least one basic test.
✓ Failure Modes
The MiniMax connector follows the established Nvidia connector pattern closely. The error handling is consistent with the rest of the codebase: non-streaming errors are properly caught and wrapped in ServiceResponseException, streaming iteration errors propagate unwrapped (same as Nvidia). Settings validation, regional URL resolution, and usage tracking all work correctly. No critical failure modes specific to this PR were found.
✗ Design Approach
I found two design-level issues in the new MiniMax connector. The streaming path is wired against the wrong response type, so streamed chat completions will fail before yielding any chunks, and the custom chat-history formatter drops OpenAI-compatible message fields such as tool_calls/tool_call_id instead of reusing the repo’s existing serializer pattern. Both problems come from reimplementing OpenAI-compatible plumbing instead of following the established base-connector contract.
Flagged Issues
- python/semantic_kernel/connectors/ai/minimax/services/minimax_chat_completion.py:183 asserts AsyncGenerator even though the MiniMax handler returns ChatCompletion | AsyncStream[Any] (minimax_handler.py:46-53); the repo's OpenAI-compatible base checks AsyncStream instead (open_ai_chat_completion_base.py:110-113). This will reject streamed responses at runtime.
- python/semantic_kernel/connectors/ai/minimax/services/minimax_chat_completion.py:291-294 flattens every history item to {role, content}, dropping OpenAI-compatible tool_calls/tool_call_id fields that ChatMessageContent.to_dict() preserves (chat_message_content.py:312-321). The repo's existing formatter reuses that serializer (open_ai_chat_completion_base.py:294-303). This breaks tool-calling round-trips.
Automated review by octo-patch's agents
| messages = [] | ||
| for message in chat_history.messages: | ||
| message_dict = {role_key: message.role.value, content_key: message.content} | ||
| messages.append(message_dict) |
There was a problem hiding this comment.
This formatter silently mishandles any ChatHistory containing assistant tool calls or tool results. ChatMessageContent.to_dict() is the repo's contract for OpenAI-compatible messages: assistant function-call turns produce tool_calls, and tool replies include tool_call_id. The existing OpenAI-compatible formatter reuses message.to_dict(...) and applies the instruction_role remap on top. Flattening everything to {role, content} loses tool-call linkage on follow-up turns.
| messages = [] | |
| for message in chat_history.messages: | |
| message_dict = {role_key: message.role.value, content_key: message.content} | |
| messages.append(message_dict) | |
| return [ | |
| { | |
| **message.to_dict(role_key=role_key, content_key=content_key), | |
| role_key: "developer" | |
| if self.instruction_role == "developer" and message.to_dict(role_key=role_key)[role_key] == "system" | |
| else message.to_dict(role_key=role_key)[role_key], | |
| } | |
| for message in chat_history.messages | |
| ] |
|
Flagged issue python/semantic_kernel/connectors/ai/minimax/services/minimax_chat_completion.py:183 asserts AsyncGenerator even though the MiniMax handler returns ChatCompletion | AsyncStream[Any] (minimax_handler.py:46-53); the repo's OpenAI-compatible base checks AsyncStream instead (open_ai_chat_completion_base.py:110-113). This will reject streamed responses at runtime. Source: automated DevFlow PR review |
|
Flagged issue python/semantic_kernel/connectors/ai/minimax/services/minimax_chat_completion.py:291-294 flattens every history item to {role, content}, dropping OpenAI-compatible tool_calls/tool_call_id fields that ChatMessageContent.to_dict() preserves (chat_message_content.py:312-321). The repo's existing formatter reuses that serializer (open_ai_chat_completion_base.py:294-303). This breaks tool-calling round-trips. Source: automated DevFlow PR review |
Reason: Add the MiniMax chat completion provider so Semantic Kernel can call MiniMax-M3 and MiniMax-M2.7 through the documented global and China OpenAI-compatible endpoints.
Changes
semantic_kernel.connectors.ai.minimaxconnector with an OpenAI-compatible chat completion service that follows the existing Nvidia connector pattern.MiniMaxSettingsresolves the base URL from aregionfield, covering theglobal_en(https://api.minimax.io/v1) andcn_zh(https://api.minimaxi.com/v1) regional endpoints.MiniMaxChatCompletiondefaults toMiniMax-M3and acceptsMiniMax-M2.7viaai_model_id/MINIMAX_CHAT_MODEL_ID.MiniMaxChatPromptExecutionSettingsclampstemperatureto the documented[0.0, 1.0]range.Checks
pytest tests/unit/connectors/ai/minimax-> 27 passed.ruff checkandruff format --checkon the new files -> clean.mypy semantic_kernel/connectors/ai/minimax-> clean.