Skip to content

Python: Fix presence_penalty, frequency_penalty and n ranges in MistralAIChatPromptExecutionSettings - #14741

Open
Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:fix-mistral-settings-ranges
Open

Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:fix-mistral-settings-ranges

Conversation

@14H034160212

Copy link
Copy Markdown

Motivation and Context

MistralAIChatPromptExecutionSettings rejects valid values for three fields (source):

  • presence_penalty and frequency_penalty use Field(gt=0). That rejects 0, which means no penalty, and every negative value.
  • n uses Field(gt=1), so n=1 is rejected.

This also breaks the generic settings path. A PromptExecutionSettings whose extension_data contains presence_penalty: 0 or frequency_penalty: 0 (common in prompt templates written for several providers) fails in MistralAIChatPromptExecutionSettings.from_prompt_execution_settings with a ValidationError:

from semantic_kernel.connectors.ai.mistral_ai import MistralAIChatPromptExecutionSettings
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings

MistralAIChatPromptExecutionSettings(n=1)                   # ValidationError: greater than 1
MistralAIChatPromptExecutionSettings(presence_penalty=0.0)  # ValidationError: greater than 0
MistralAIChatPromptExecutionSettings(presence_penalty=-0.5) # ValidationError: greater than 0

generic = PromptExecutionSettings(extension_data={"max_tokens": 256, "presence_penalty": 0, "frequency_penalty": 0})
MistralAIChatPromptExecutionSettings.from_prompt_execution_settings(generic)  # ValidationError

The valid ranges, from primary sources:

  • Mistral API spec: openapi.yaml, schema ChatCompletionRequest (used by POST /v1/chat/completions, see the API reference), defines presence_penalty and frequency_penalty as number | null with minimum: -2, maximum: 2, and n as integer | null with minimum: 1. AgentsCompletionRequest and CompletionArgs use the same penalty range.
  • mistralai SDK (1.12.4, the version the mistralai extra resolves to): ChatCompletionRequest declares presence_penalty: Optional[float], frequency_penalty: Optional[float] and n: OptionalNullable[int] with no validators, so the SDK does not check the range. The service does.

Description

  • presence_penalty and frequency_penalty: Field(gt=0) changed to Field(ge=-2.0, le=2.0), the range in the Mistral API spec.

  • n: Field(gt=1) changed to Field(ge=1).

  • Defaults stay None, so nothing extra is sent unless the user sets a value.

  • Tests added to tests/unit/connectors/ai/mistral_ai/test_mistralai_request_settings.py:

    • both penalties accept -2.0, -0.5, 0.0, 0.5 and 2.0, and the value reaches prepare_settings_dict();
    • both penalties reject -2.1 and 2.1;
    • n accepts 1 and 2 and rejects 0;
    • from_prompt_execution_settings converts generic settings with presence_penalty=0, frequency_penalty=0 and n=1.

    10 of the new cases fail on main.

Verification (from python/):

uv run --no-sync pytest tests/unit/connectors/ai/mistral_ai     # 76 passed
uv run --no-sync ruff check <changed files> && uv run --no-sync ruff format --check <changed files>
uv run --no-sync mypy -p semantic_kernel.connectors.ai.mistral_ai --config-file mypy.ini   # Success: no issues found
uv run --no-sync pre-commit run -c python/.pre-commit-config.yaml --files <changed files>  # all hooks passed

Contribution Checklist

…alAIChatPromptExecutionSettings

presence_penalty and frequency_penalty used Field(gt=0), which rejected 0
(no penalty) and every negative value, and n used Field(gt=1), which
rejected n=1. Converting generic settings that carry presence_penalty=0 or
frequency_penalty=0 through from_prompt_execution_settings therefore
failed with a ValidationError.

The Mistral API spec (ChatCompletionRequest) defines both penalties as
numbers in [-2, 2] and n as an integer >= 1. The mistralai SDK does not
validate these fields, so mirror the API: ge=-2.0, le=2.0 for the
penalties and ge=1 for n.
@14H034160212
Qiming Bao (14H034160212) requested a review from a team as a code owner November 7, 2026 11:16
Copilot AI balanced review requested due to automatic review settings November 7, 2026 11:16
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The review reports no active issues with the Mistral AI chat-settings validation changes.

No open issues 🎉

What changed in this PR

Corrects validation in Python’s Mistral AI chat settings to accept valid API values, including zero penalties and n=1, while preserving None defaults.

Changes:

  • Accepts presence and frequency penalties in the inclusive range [-2, 2].
  • Allows n >= 1.
  • Adds regression tests for valid values, rejected values, serialization, and conversion from generic prompt settings.
File Description
python/​tests/​unit/​connectors/​ai/​mistral_ai/​test_mistralai_request_settings.py Adds validation, serialization, and generic-settings conversion tests.
python/​semantic_kernel/​connectors/​ai/​mistral_ai/​prompt_execution_settings/​mistral_ai_prompt_execution_settings.py Corrects penalty bounds and the minimum value for n.

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — 13d6de08 Deployed Nov 7, 2026 by 14H034160212 via add_label #29622
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants