Repository navigation
fix(litellm): forward stop_sequences to the provider as stop - #1791
Conversation
`stop_sequences` is part of `ChatModelParameters`, is documented in `ChatModel.run`, and is shown in `python/examples/backend/chat.py`, but the LiteLLM adapter forwarded it under its own name. LiteLLM (and the OpenAI-compatible providers behind it) exposes stop words as `stop`, so `stop_sequences` arrived as an unknown keyword and was dropped by `drop_params`, while `stop` was never set. The LangChain adapter already translates the two names (`stop=input.stop_sequences`) and the Transformers adapter honours the value directly. Translate `stop_sequences` to `stop` when the provider advertises support for it, keeping the precedence the parameter merge already uses (per-run value, then the model's default parameters), and stop leaking the unknown `stop_sequences` keyword into the request. Signed-off-by: D41910 <D41910@users.noreply.github.com>
Tomas2D
left a comment
There was a problem hiding this comment.
Thanks for this. It's a real bug, and the fix is in the right place.
On main with litellm 1.89.2, the unknown stop_sequences keyword isn't actually dropped. litellm forwards it as a provider-specific param. OpenAI-compatible endpoints receive a literal "stop_sequences" field, watsonx fails outright with ValueError ... Param: stop_sequences, and Anthropic and Gemini only worked because their native field has the same name. Your change fixes all of these. I checked that Anthropic, Gemini, Vertex, Bedrock and watsonx get the right native field once litellm maps stop, and that models without stop support (e.g. gpt-5) now leave it out.
One behaviour change to be aware of (not blocking): litellm truncates stop to 4 items for every provider unless litellm.disable_stop_sequence_limit is set, so Anthropic and Gemini users with more than 4 sequences now get only the first 4.
Optional test additions: a provider without stop support sends neither field, and run(..., stop_sequences=[]) clears a non-empty default. Approving, and I'll merge once CI is green.
Which issue(s) does this pull-request address?
None; found by comparing the LiteLLM adapter with the LangChain and Transformers adapters, which both honour
stop_sequences.Closes: #
Description
stop_sequencesis part ofChatModelParameters, is documented in theChatModel.rundocstring, and is demonstrated inpython/examples/backend/chat.py:On the LiteLLM path (which backs
OpenAIChatModel,OllamaChatModel,AnthropicChatModel,GroqChatModel, ...), that value never reached the provider:supported_paramsfor OpenAI containsstop, neverstop_sequences, soinclude_keys(...)filtered it out ofparams;settingsand was forwarded toacompletion()as the unknown keywordstop_sequences, whichlitellm.drop_params = Truesilently discards.Verified against litellm at the current lockfile version:
The LangChain adapter already performs this translation (
parameters["stop"]/stop=input.stop_sequences), and the Transformers adapter honoursinput.stop_sequencesdirectly, so the LiteLLM path was the odd one out.Change
_transform_inputnow translatesstop_sequencestostopwhen the provider advertisesstopinsupported_params, keeping the precedence the existing parameter merge uses: per-run value, then the model's defaultparameters.stop_sequencesis added to the exclusion set so the unknown keyword is no longer forwarded.If a provider does not advertise
stop, the value is dropped exactly as it is today.Tests
Four unit tests in
tests/backend/providers/test_litellm_chat.py(monkeypatchedacompletion, no network): per-run value, defaultparametersvalue, per-run override, and no-value. Reverting the fix makes the first three fail.Checklist
General
Pythonfor Python changes,TypeScriptfor TypeScript changesCode quality checks
mise check(mise fixto auto-fix)ruff checkandruff format --checkclean on both changed filesTesting
mise test:unitpytest tests/backend -q-> 94 passed (excludingtests/backend/providers/test_transformers.py, which needs the optionaloutlines/torchextras)pytest tests -m unit -q-> 400 passedmise test:e2e(requires a running Ollama; not run here)Documentation
Note: this change was prepared with AI assistance and reviewed, tested and signed off by the contributor above.