-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(openai): forward request params from with_azure for parity with L… #6211
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
Open
RaidLZ
wants to merge
2
commits into
livekit:main
Choose a base branch
from
RaidLZ:fix/openai-with-azure-param-parity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,90 @@ | ||
| """Hermetic unit tests for ``openai.LLM.with_azure`` request-parameter forwarding. | ||
|
|
||
| These construct the LLM object only and assert on its ``_opts`` — no network or Azure | ||
| credentials are required, so the module runs in the ``--unit`` gate. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from livekit.agents.llm import ChatContext | ||
| from livekit.agents.types import NOT_GIVEN | ||
| from livekit.plugins import openai | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
| # Dummy Azure connection details. ``AsyncAzureOpenAI`` validates that these are present at | ||
| # construction time but does not open a connection, so any non-empty values work here. | ||
| _AZURE_ENDPOINT = "https://example.openai.azure.com" | ||
| _API_VERSION = "2024-10-21" | ||
| _API_KEY = "test-key" | ||
|
|
||
|
|
||
| def test_with_azure_forwards_request_params() -> None: | ||
| """Params shared with ``LLM.__init__`` must reach ``_opts`` instead of being dropped.""" | ||
| extra_body = {"data_sources": [{"type": "azure_search"}]} | ||
| extra_headers = {"x-ms-custom": "1"} | ||
| extra_query = {"foo": "bar"} | ||
| metadata = {"team": "voice"} | ||
|
|
||
| azure_llm = openai.LLM.with_azure( | ||
| azure_endpoint=_AZURE_ENDPOINT, | ||
| api_version=_API_VERSION, | ||
| api_key=_API_KEY, | ||
| azure_deployment="gpt-4o", | ||
| store=True, | ||
| metadata=metadata, | ||
| prompt_cache_retention="24h", | ||
| extra_body=extra_body, | ||
| extra_headers=extra_headers, | ||
| extra_query=extra_query, | ||
| ) | ||
|
|
||
| opts = azure_llm._opts | ||
| assert opts.store is True | ||
| assert opts.metadata == metadata | ||
| assert opts.prompt_cache_retention == "24h" | ||
| assert opts.extra_body == extra_body | ||
| assert opts.extra_headers == extra_headers | ||
| assert opts.extra_query == extra_query | ||
|
|
||
|
|
||
| def test_with_azure_request_params_default_to_not_given() -> None: | ||
| """When omitted, the forwarded params stay ``NOT_GIVEN`` so nothing extra is sent.""" | ||
| azure_llm = openai.LLM.with_azure( | ||
| azure_endpoint=_AZURE_ENDPOINT, | ||
| api_version=_API_VERSION, | ||
| api_key=_API_KEY, | ||
| azure_deployment="gpt-4o", | ||
| ) | ||
|
|
||
| opts = azure_llm._opts | ||
| assert opts.store is NOT_GIVEN | ||
| assert opts.metadata is NOT_GIVEN | ||
| assert opts.prompt_cache_retention is NOT_GIVEN | ||
| assert opts.extra_body is NOT_GIVEN | ||
| assert opts.extra_headers is NOT_GIVEN | ||
| assert opts.extra_query is NOT_GIVEN | ||
|
|
||
|
|
||
| @pytest.mark.concurrent | ||
| async def test_store_is_forwarded_to_chat_request() -> None: | ||
| """``store`` set on the LLM must actually reach the chat-completions request kwargs.""" | ||
| azure_llm = openai.LLM(api_key="test-key", store=True) | ||
| stream = azure_llm.chat(chat_ctx=ChatContext.empty()) | ||
| try: | ||
| assert stream._extra_kwargs.get("store") is True | ||
| finally: | ||
| await stream.aclose() | ||
|
|
||
|
|
||
| @pytest.mark.concurrent | ||
| async def test_store_absent_from_chat_request_when_unset() -> None: | ||
| """When ``store`` is not set, it must not be injected into the request kwargs.""" | ||
| azure_llm = openai.LLM(api_key="test-key") | ||
| stream = azure_llm.chat(chat_ctx=ChatContext.empty()) | ||
| try: | ||
| assert "store" not in stream._extra_kwargs | ||
| finally: | ||
| await stream.aclose() |
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.
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.
🚩 Pre-existing: user-provided extra_headers dict is mutated in-place across chat() calls
In
livekit-agents/livekit/agents/inference/llm.py:412-413, the_run()method doesextra_headers = self._extra_kwargs.setdefault("extra_headers", {})followed byextra_headers.update(get_inference_headers()). Sincechat()atlivekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py:979-980assignsextra["extra_headers"] = self._opts.extra_headers(a direct reference, not a copy), repeatedchat()calls will accumulate inference headers into the user's original dict object stored in_opts. This is a pre-existing issue (not introduced by this PR) but is now more likely to be encountered sincewith_azure()users can now passextra_headers.(Refers to lines 979-980)
Was this helpful? React with 👍 or 👎 to provide feedback.