Python: Fix walrus operator precedence for model_id kwarg in AzureOpenAIResponsesClient#4310
Python: Fix walrus operator precedence for model_id kwarg in AzureOpenAIResponsesClient#4310moonbox3 wants to merge 7 commits intomicrosoft:mainfrom
Conversation
…ient (microsoft#4299) Add parentheses around the walrus assignment so model_id receives the actual string value instead of the boolean result of `kwargs.pop(...) and not deployment_name`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
moonbox3
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 3 | Confidence: 93%
✓ Correctness
The diff correctly fixes a Python operator precedence bug. In the original code,
model_id := kwargs.pop("model_id", None) and not deployment_namewas parsed asmodel_id := (kwargs.pop("model_id", None) and not deployment_name)due to:=having lower precedence thanand. This causedmodel_idto be assigned a boolean value instead of the actual model ID string, sostr(model_id)would produce"True"rather than the intended deployment name. The fix correctly parenthesizes the walrus operator assignment. The accompanying test covers the fixed behavior.
✓ Security Reliability
The diff fixes a genuine operator-precedence bug where the walrus operator bound to the entire
andexpression, causingmodel_idto be assigned a boolean instead of the actual model ID string. The parenthesization fix is correct and the accompanying test validates the expected behavior. No security or reliability concerns are introduced by this change.
✗ Test Coverage
The bug fix correctly adds parentheses around the walrus operator to fix operator precedence (the old code assigned
pop(...) and not deployment_nametomodel_idinstead of justpop(...)). The new test covers the basic happy path but misses the other branch of the condition (and not deployment_name) and doesn't assert the deployment name itself, only themodel_idattribute.
Blocking Issues
- The test does not cover the case where both
model_idkwarg anddeployment_nameare provided. The fixed condition has anand not deployment_nameguard, so a test should verify that an explicitdeployment_nameis NOT overridden bymodel_id. Without this, a future regression that drops theand not deployment_namecheck would go undetected.
Suggestions
- Consider asserting the underlying deployment name (e.g., via the inner OpenAI client's base URL or a deployment-name attribute) rather than only
model_id, to more directly verify the bug fix. - Consider adding a test with
model_idset to an empty string orNoneto verify no assignment occurs in that edge case.
Automated review by moonbox3's agents
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
Fixes a Python bug in AzureOpenAIResponsesClient where walrus-operator precedence caused model_id=... passed via **kwargs to be coerced into a boolean, leading to an incorrect deployment name being set.
Changes:
- Add parentheses around the walrus assignment to ensure
model_idcaptures the popped value before boolean logic is applied. - Add a unit test verifying
model_id="gpt-4o"propagates correctly toclient.model_id.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/core/agent_framework/azure/_responses_client.py |
Fix walrus precedence so model_id kwarg correctly maps to deployment_name. |
python/packages/core/tests/azure/test_azure_responses_client.py |
Add regression test covering the model_id kwarg behavior (issue #4299). |
…e tests (microsoft#4299) - Replace walrus operator with explicit assignment and 'is not None' check to avoid boolean-coercion pitfalls (empty string now correctly surfaces as ValueError instead of silently falling back) - Add test: deployment_name takes precedence over model_id kwarg - Add test: model_id='' raises ValueError - Add test: model_id=None falls back to env var Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
moonbox3
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 3 | Confidence: 85%
✓ Correctness
The production code change correctly fixes a subtle bug where the walrus operator combined with truthiness checks (
if (model_id := ...) and not deployment_name) would silently ignore falsy-but-valid values likemodel_id=""and would also treatdeployment_name=""as unset. The new explicitis not Nonechecks are semantically correct and handle edge cases properly. The tests cover the important scenarios well, thoughtest_init_model_id_kwarg_empty_stringrelies on downstream validation to raiseValueErrorfor an empty deployment name, which is not implemented in this diff.
✓ Security Reliability
This diff is a reliability improvement that fixes how
model_idanddeployment_nameare evaluated during initialization. The old code used truthiness checks (and not deployment_name) which would incorrectly treat empty strings and other falsy values as unset. The new code correctly uses explicitis not Noneidentity checks. The accompanying tests cover the key edge cases (precedence, empty string, None). No security or reliability issues found.
✗ Test Coverage
The diff fixes a truthiness bug in the
model_idhandling (empty-string and other falsy-but-not-None values were silently ignored) and adds three new tests. Two tests (test_init_model_id_kwarg_does_not_override_deployment_nameandtest_init_model_id_kwarg_none) are well-targeted and have meaningful assertions. However,test_init_model_id_kwarg_empty_stringexpects aValueErrorto be raised, yet the code change itself only setsdeployment_name = str(model_id)(i.e.,""); theValueErrorwould have to originate from downstream validation not visible in this diff. If that downstream validation doesn't exist or changes, the test will fail or give a false sense of coverage. Additionally, there is no test that verifies a falsy-but-validmodel_id(e.g.,model_id=0) is actually forwarded as the deployment name — which is the core scenario the truthiness fix enables.
Blocking Issues
- test_init_model_id_kwarg_empty_string asserts
pytest.raises(ValueError)but the code change only assignsdeployment_name = str(model_id)(an empty string). If no downstream validator raises ValueError for an empty deployment name, this test will fail. Either add explicit validation in the constructor (e.g.,if model_id is not None and not model_id: raise ValueError(...)) or change the test expectation to match actual behaviour.
Suggestions
- In
test_init_model_id_kwarg_empty_string, the test expectsValueErrorwhenmodel_id=""is passed, but the code in this diff does not add any such validation — it will simply setdeployment_name = ""and rely on downstream constructor logic to reject it. Consider either adding explicit validation in__init__(e.g.,if model_id is not None and not model_id: raise ValueError(...)) or adding a comment in the test documenting which downstream component is expected to raise the error, to avoid confusion if that behavior changes. - Consider adding a type check or assertion that
model_idis astr(orNone) before passing it tostr(), sincekwargs.popcan return any type. This would surface misuse earlier with a clear error rather than silently converting arbitrary objects to their string representation. - Add a test for a falsy-but-non-empty
model_idvalue (e.g.,model_id=0) to directly validate the fix for the truthiness bug — this is the exact scenario the oldif (model_id := …)pattern mishandled. - Consider adding explicit empty-string validation in the constructor so the ValueError contract tested in
test_init_model_id_kwarg_empty_stringis clearly self-contained rather than depending on downstream behaviour.
Automated review by moonbox3's agents
python/packages/core/tests/azure/test_azure_responses_client.py
Outdated
Show resolved
Hide resolved
python/packages/core/tests/azure/test_azure_responses_client.py
Outdated
Show resolved
Hide resolved
python/packages/core/agent_framework/azure/_responses_client.py
Outdated
Show resolved
Hide resolved
Reject empty or whitespace-only model_id with ValueError instead of silently passing an empty deployment name downstream. This ensures the test_init_model_id_kwarg_empty_string test correctly validates behavior defined in production code rather than relying on downstream validation. Addresses PR review feedback for microsoft#4299. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
python/packages/core/agent_framework/azure/_responses_client.py
Outdated
Show resolved
Hide resolved
Addresses review comment on PR microsoft#4310. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…4299) The walrus operator refactor silently dropped the empty-string validation, causing test_init_model_id_kwarg_empty_string to fail. Restore the explicit None check and ValueError raise for empty model_id. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…crosoft#4299)" This reverts commit 1d2965f.
Motivation and Context
When passing
model_idas a keyword argument toAzureOpenAIResponsesClient, the walrus operator (:=) bound more loosely thanand, causingmodel_idto be assigned the boolean result of the full expression (True) instead of the actual model name string. This meantdeployment_namewas set to"True"instead of the intended model identifier.Fixes #4299
Description
The root cause was missing parentheses around the walrus assignment
model_id := kwargs.pop("model_id", None). Without them, Python parsed the expression asmodel_id := (kwargs.pop("model_id", None) and not deployment_name), assigning the boolean result tomodel_id. The fix adds parentheses to ensuremodel_idreceives the popped value first, then the boolean check runs separately. A test was added to verify thatmodel_id="gpt-4o"correctly propagates as the deployment name.Contribution Checklist