Repository navigation
Python: Fix presence_penalty, frequency_penalty and n ranges in MistralAIChatPromptExecutionSettings - #14741
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Conversation
…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.
Qiming Bao (14H034160212)
deployed
to
github-app-auth
November 7, 2026 11:16 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
MistralAIChatPromptExecutionSettingsrejects valid values for three fields (source):presence_penaltyandfrequency_penaltyuseField(gt=0). That rejects0, which means no penalty, and every negative value.nusesField(gt=1), son=1is rejected.This also breaks the generic settings path. A
PromptExecutionSettingswhoseextension_datacontainspresence_penalty: 0orfrequency_penalty: 0(common in prompt templates written for several providers) fails inMistralAIChatPromptExecutionSettings.from_prompt_execution_settingswith aValidationError:The valid ranges, from primary sources:
openapi.yaml, schemaChatCompletionRequest(used byPOST /v1/chat/completions, see the API reference), definespresence_penaltyandfrequency_penaltyasnumber | nullwithminimum: -2,maximum: 2, andnasinteger | nullwithminimum: 1.AgentsCompletionRequestandCompletionArgsuse the same penalty range.mistralaiextra resolves to):ChatCompletionRequestdeclarespresence_penalty: Optional[float],frequency_penalty: Optional[float]andn: OptionalNullable[int]with no validators, so the SDK does not check the range. The service does.Description
presence_penaltyandfrequency_penalty:Field(gt=0)changed toField(ge=-2.0, le=2.0), the range in the Mistral API spec.n:Field(gt=1)changed toField(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:prepare_settings_dict();naccepts 1 and 2 and rejects 0;from_prompt_execution_settingsconverts generic settings withpresence_penalty=0,frequency_penalty=0andn=1.10 of the new cases fail on
main.Verification (from
python/):Contribution Checklist