Skip to content

feat(mistralai): support stop sequences#38047

Merged
Mason Daugherty (mdrxy) merged 2 commits into
masterfrom
mdrxy/mistralai/stop-param
Jun 11, 2026
Merged

feat(mistralai): support stop sequences#38047
Mason Daugherty (mdrxy) merged 2 commits into
masterfrom
mdrxy/mistralai/stop-param

Conversation

@mdrxy

@mdrxy Mason Daugherty (mdrxy) commented Jun 10, 2026

Copy link
Copy Markdown
Member

ChatMistralAI now supports stop sequences.

Previously, a stop value passed to the model was silently discarded: the code carried a stale "not yet supported" note, dropped the parameter before the request, and logged a warning. Mistral's chat completions API does accept stop (a string or list of strings, up to 4 sequences), so anyone setting stop and expecting generation to halt was getting no effect.

Now stop is a first-class parameter. It can be set on the constructor (ChatMistralAI(stop=[...])) or per call (model.invoke(prompt, stop=[...])) and is forwarded to the API. A per-call value overrides the instance default, and an empty list is treated as "no stop sequences" — omitted from the request rather than sent as an empty array (which the API rejects).

Verified against the live Mistral API: with stop=["5"], "Count from 1 to 10" returns 1 2 3 4 instead of the full sequence. The 422 extra_forbidden response the API returns for genuinely unknown fields confirms stop is a real schema field, not silently ignored.

This PR also folds in some test hygiene: the base-URL env test uses monkeypatch.setenv so MISTRAL_BASE_URL=boo no longer leaks into later serialization tests, and test_extra_kwargs asserts the intentional unknown-kwarg warning with pytest.warns.

Review notes

  • Behavior change worth a careful look: stop now reaches the API instead of being dropped. This changes request payloads for anyone previously passing stop. It is the intended fix, but flagging it explicitly.
  • Coverage: test_stop_sequence (integration) exercises the end-to-end behavior; unit tests cover parameter wiring, per-call-vs-instance precedence, and the empty-list case.

`ChatMistralAI` now treats `stop` as a first-class constructor parameter instead of letting LangChain move it into `model_kwargs` with a warning.

The related tests also avoid leaking `MISTRAL_BASE_URL` across test cases and explicitly assert the intentional unknown-kwarg warning path. Prepared with AI-agent assistance.

## Changes
- Added `stop` to `ChatMistralAI` initialization so LangChain standard chat-model params no longer produce `model_kwargs` warnings.
- Updated `_get_ls_params` and `_create_message_dicts` to read constructor-level `self.stop` while preserving the existing behavior that removes unsupported `stop` values before Mistral API requests.
- Captured the intentional `foo` extra-kwarg warning in `test_extra_kwargs` with `pytest.warns`, so the test documents the expected warning instead of emitting noise.
- Switched the Mistral base URL environment test to `monkeypatch.setenv`, preventing `MISTRAL_BASE_URL=boo` from leaking into later serialization tests.
- Updated the standard serialization snapshot so `stop` appears as an explicit constructor kwarg and the default endpoint is no longer polluted by leaked environment state.
@github-actions github-actions Bot added fix For PRs that implement a fix integration PR made that is related to a provider partner package integration internal mistralai `langchain-mistralai` package issues & PRs size: XS < 50 LOC labels Jun 10, 2026

@open-swe open-swe Bot 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.

✅ Open SWE Review: No issues found

Open SWE reviewed this PR and found no potential bugs to report.

Open in WebView Open SWE trace

@github-actions github-actions Bot added size: S 50-199 LOC and removed size: XS < 50 LOC labels Jun 11, 2026
@mdrxy Mason Daugherty (mdrxy) changed the title fix(mistralai): declare stop as chat parameter feat(mistralai): support stop sequences Jun 11, 2026
@github-actions github-actions Bot added feature For PRs that implement a new feature; NOT A FEATURE REQUEST and removed fix For PRs that implement a fix labels Jun 11, 2026
@mdrxy Mason Daugherty (mdrxy) merged commit fcaa616 into master Jun 11, 2026
56 checks passed
@mdrxy Mason Daugherty (mdrxy) deleted the mdrxy/mistralai/stop-param branch June 11, 2026 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature For PRs that implement a new feature; NOT A FEATURE REQUEST integration PR made that is related to a provider partner package integration internal mistralai `langchain-mistralai` package issues & PRs size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant