Skip to content

Commit 703eb5f

Browse files
Python: Send the NVIDIA nvext setting in extra_body (#14698)
### Motivation and Context `NvidiaChatPromptExecutionSettings.nvext` is dumped as a top-level argument of `chat.completions.create()`, so every request that sets it fails: ``` ServiceResponseException: ... service failed to complete the chat caused by: TypeError: AsyncCompletions.create() got an unexpected keyword argument 'nvext' ``` ### Description - `prepare_settings_dict()` now sends `nvext` in `extra_body`, merged with the `guided_json` that `response_format` puts there. On a key clash, `extra_body["nvext"]` wins. - Removed the handler's copy of `extra_body["nvext"]`. `model_dump()` already includes `extra_body`, and the copy would overwrite the merged value. - Added two regression tests. The existing tests mock `create()` with a plain `AsyncMock`, which accepts any keyword, so they could not catch this. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent e68f977 commit 703eb5f

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎python/semantic_kernel/connectors/ai/nvidia/prompt_execution_settings/nvidia_prompt_execution_settings.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ class NvidiaChatPromptExecutionSettings(NvidiaPromptExecutionSettings):
6565
nvext: dict[str, Any] | None = None
6666

6767
def prepare_settings_dict(self, **kwargs) -> dict[str, Any]:
68-
"""Override only for embeddings to exclude input_type and truncate."""
69-
return self.model_dump(
70-
exclude={"service_id", "extension_data", "structured_json_response", "response_format"},
68+
"""Prepare the settings dict, moving nvext into extra_body.
69+
70+
The OpenAI client only sends nvext as part of extra_body. Keys already in extra_body["nvext"],
71+
such as the guided_json set from response_format, take precedence over the same keys in nvext.
72+
"""
73+
settings_dict = self.model_dump(
74+
exclude={"service_id", "extension_data", "structured_json_response", "response_format", "nvext"},
7175
exclude_none=True,
7276
by_alias=True,
7377
)
78+
if self.nvext:
79+
extra_body = settings_dict.setdefault("extra_body", {})
80+
extra_body["nvext"] = self.nvext | extra_body.get("nvext", {})
81+
return settings_dict

‎python/semantic_kernel/connectors/ai/nvidia/services/nvidia_handler.py‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ async def _send_chat_completion_request(
6666
"""Send a request to the NVIDIA chat completion endpoint."""
6767
try:
6868
settings_dict = settings.prepare_settings_dict()
69-
70-
# Handle structured output if nvext is present in extra_body
71-
if settings.extra_body and "nvext" in settings.extra_body:
72-
if "extra_body" not in settings_dict:
73-
settings_dict["extra_body"] = {}
74-
settings_dict["extra_body"]["nvext"] = settings.extra_body["nvext"]
75-
7669
response = await self.client.chat.completions.create(**settings_dict)
7770
self.store_usage(response)
7871
return response

‎python/tests/unit/connectors/ai/nvidia/services/test_nvidia_chat_completion.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,46 @@ class TestModel(BaseModel):
136136
assert "nvext" in call_args["extra_body"]
137137
assert "guided_json" in call_args["extra_body"]["nvext"]
138138

139+
@pytest.mark.asyncio
140+
@patch.object(AsyncCompletions, "create", new_callable=AsyncMock)
141+
async def test_nvext_is_sent_in_extra_body(self, mock_create, nvidia_unit_test_env):
142+
"""Test that the nvext setting is sent in extra_body, not as a create() argument."""
143+
mock_create.return_value = _create_mock_chat_completion()
144+
145+
service = NvidiaChatCompletion()
146+
chat_history = ChatHistory()
147+
chat_history.add_user_message("Hello")
148+
settings = NvidiaChatPromptExecutionSettings(nvext={"guided_choice": ["yes", "no"]})
149+
150+
await service.get_chat_message_contents(chat_history, settings)
151+
152+
call_args = mock_create.call_args[1]
153+
assert "nvext" not in call_args
154+
assert call_args["extra_body"]["nvext"] == {"guided_choice": ["yes", "no"]}
155+
156+
@pytest.mark.asyncio
157+
@patch.object(AsyncCompletions, "create", new_callable=AsyncMock)
158+
async def test_nvext_is_merged_with_structured_output(self, mock_create, nvidia_unit_test_env):
159+
"""Test that nvext keys are kept next to the guided_json set from response_format."""
160+
161+
class TestModel(BaseModel):
162+
name: str
163+
164+
mock_create.return_value = _create_mock_chat_completion('{"name": "test"}')
165+
166+
service = NvidiaChatCompletion()
167+
chat_history = ChatHistory()
168+
chat_history.add_user_message("Give me structured data")
169+
settings = NvidiaChatPromptExecutionSettings(nvext={"repetition_penalty": 1.1}, response_format=TestModel)
170+
171+
await service.get_chat_message_contents(chat_history, settings)
172+
173+
call_args = mock_create.call_args[1]
174+
assert call_args["extra_body"]["nvext"] == {
175+
"repetition_penalty": 1.1,
176+
"guided_json": TestModel.model_json_schema(),
177+
}
178+
139179
@pytest.mark.asyncio
140180
@patch.object(AsyncCompletions, "create", new_callable=AsyncMock)
141181
async def test_error_handling(self, mock_create, nvidia_unit_test_env):

0 commit comments

Comments
 (0)