Skip to content

Commit 7a957a0

Browse files
Python: Fix Mistral AI seed setting being rejected by the SDK (#14677)
`ChatRequest.Seed` was serialized for Mistral as `seed`, but Mistral's chat completions take the value as `random_seed`, so a seed set on the request did not reach the model as the deterministic-sampling seed. `random_seed` could only be set through `ChatRequestVendorMistralExtensions.RandomSeed`. The Mistral request now maps `Seed` to `random_seed` and no longer sends `seed`. `ChatRequestVendorMistralExtensions.RandomSeed` still takes precedence when set (before, the extension unconditionally overwrote the field, so a null extension value is now ignored instead of clearing the mapped seed). Added `MistralChatSeedTests` (no network): `Seed` becomes `random_seed` without `seed`; the extension value wins over `Seed`; an extensions object without `RandomSeed` keeps the seed; no seed sends neither field. Three of the four fail on `master`. This change was made with the help of an AI assistant (Claude Code); the tests were run locally on net8.0.
1 parent aeb4737 commit 7a957a0

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎python/semantic_kernel/connectors/ai/mistral_ai/prompt_execution_settings/mistral_ai_prompt_execution_settings.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ class MistralAIChatPromptExecutionSettings(MistralAIPromptExecutionSettings):
5656
"on the function choice configuration.",
5757
),
5858
] = None
59+
60+
def prepare_settings_dict(self, **kwargs) -> dict[str, Any]:
61+
"""Prepare the settings as a dictionary for sending to the AI service.
62+
63+
The Mistral SDK has no `seed` parameter, it takes `random_seed`, so `seed` is sent
64+
as `random_seed` unless that one is set explicitly.
65+
"""
66+
settings = super().prepare_settings_dict(**kwargs)
67+
seed = settings.pop("seed", None)
68+
if seed is not None:
69+
settings.setdefault("random_seed", seed)
70+
return settings

‎python/tests/unit/connectors/ai/mistral_ai/services/test_mistralai_chat_completion.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ async def test_with_different_execution_settings(kernel: Kernel, mock_mistral_ai
386386
chat_history=chat_history, settings=settings, kernel=kernel, arguments=arguments
387387
)
388388
assert mock_mistral_ai_client_completion.chat.complete_async.call_args.kwargs["temperature"] == 0.2
389-
assert mock_mistral_ai_client_completion.chat.complete_async.call_args.kwargs["seed"] == 2
389+
assert mock_mistral_ai_client_completion.chat.complete_async.call_args.kwargs["random_seed"] == 2
390390

391391

392392
async def test_with_different_execution_settings_stream(
@@ -407,7 +407,7 @@ async def test_with_different_execution_settings_stream(
407407
):
408408
continue
409409
assert mock_mistral_ai_client_completion_stream.chat.stream_async.call_args.kwargs["temperature"] == 0.2
410-
assert mock_mistral_ai_client_completion_stream.chat.stream_async.call_args.kwargs["seed"] == 2
410+
assert mock_mistral_ai_client_completion_stream.chat.stream_async.call_args.kwargs["random_seed"] == 2
411411

412412

413413
async def test_mistral_ai_chat_completion_get_chat_message_contents_success():

‎python/tests/unit/connectors/ai/mistral_ai/test_mistralai_request_settings.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,18 @@ def test_create_options_with_function_choice_behavior():
123123
},
124124
)
125125
assert settings.function_choice_behavior
126+
127+
128+
def test_prepare_settings_dict_maps_seed_to_random_seed():
129+
# The Mistral SDK has no `seed` parameter, only `random_seed`
130+
settings = MistralAIChatPromptExecutionSettings(seed=42)
131+
options = settings.prepare_settings_dict()
132+
assert "seed" not in options
133+
assert options["random_seed"] == 42
134+
135+
136+
def test_prepare_settings_dict_keeps_explicit_random_seed():
137+
settings = MistralAIChatPromptExecutionSettings(seed=42, random_seed=7)
138+
options = settings.prepare_settings_dict()
139+
assert "seed" not in options
140+
assert options["random_seed"] == 7

0 commit comments

Comments
 (0)