-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
fix: stop use_chat_completions_api flag from leaking into provider request body #29447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mateo-berri
merged 2 commits into
litellm_internal_staging
from
litellm_fix_use_chat_completions_api_leak
Jun 1, 2026
+75
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
tests/test_litellm/llms/openai/test_use_chat_completions_api_no_leak.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """ | ||
| Regression test for issue #28146. | ||
|
|
||
| `use_chat_completions_api` is a LiteLLM-internal control flag (it forces the | ||
| /responses -> /chat/completions bridge). When set as a model-level param in the | ||
| proxy config, it must never be forwarded to the upstream provider's request | ||
| body. OpenAI/Anthropic reject unknown body params with HTTP 400. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from unittest.mock import MagicMock | ||
|
|
||
| sys.path.insert(0, os.path.abspath("../../../..")) | ||
|
|
||
| import litellm | ||
| from litellm.types.utils import all_litellm_params | ||
| from litellm.utils import get_non_default_completion_params | ||
|
|
||
|
|
||
| def test_use_chat_completions_api_is_a_known_litellm_param(): | ||
| assert "use_chat_completions_api" in all_litellm_params | ||
|
|
||
|
|
||
| def test_use_chat_completions_api_not_forwarded_as_provider_param(): | ||
| forwarded = get_non_default_completion_params( | ||
| {"use_chat_completions_api": True, "temperature": 0.5} | ||
| ) | ||
| assert "use_chat_completions_api" not in forwarded | ||
|
|
||
|
|
||
| def test_completion_does_not_leak_flag_into_provider_request_body(): | ||
| mock_response = MagicMock() | ||
| mock_response.model_dump.return_value = { | ||
| "id": "chatcmpl-1", | ||
| "object": "chat.completion", | ||
| "created": 1234567890, | ||
| "model": "gpt-4o-mini", | ||
| "choices": [ | ||
| { | ||
| "index": 0, | ||
| "message": {"role": "assistant", "content": "hi"}, | ||
| "finish_reason": "stop", | ||
| } | ||
| ], | ||
| "usage": { | ||
| "prompt_tokens": 1, | ||
| "completion_tokens": 1, | ||
| "total_tokens": 2, | ||
| }, | ||
| } | ||
|
|
||
| mock_raw_response = MagicMock() | ||
| mock_raw_response.headers = {} | ||
| mock_raw_response.parse.return_value = mock_response | ||
|
|
||
| mock_client = MagicMock() | ||
| mock_client.chat.completions.with_raw_response.create.return_value = ( | ||
| mock_raw_response | ||
| ) | ||
|
|
||
| litellm.completion( | ||
| model="openai/gpt-4o-mini", | ||
| messages=[{"role": "user", "content": "hi"}], | ||
| use_chat_completions_api=True, | ||
| api_key="sk-test", | ||
| client=mock_client, | ||
| ) | ||
|
|
||
| create_kwargs = ( | ||
| mock_client.chat.completions.with_raw_response.create.call_args.kwargs | ||
| ) | ||
| assert "use_chat_completions_api" not in create_kwargs | ||
| assert "use_chat_completions_api" not in (create_kwargs.get("extra_body") or {}) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we not / dont we have an existing test suite for the all_litellm_params not leaking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point
There's no generic suite for this. A blanket "every
all_litellm_paramskey is stripped" test ends up tautological sincefilter_out_litellm_paramsfilters by membership in that same list, so it wouldn't catch the real failure mode (a param being forgotten from the list)