Skip to content

Commit 5a1a604

Browse files
committed
fix(agentstudio): let copy_with retune a forward-compat field
copy_with validated its keywords against _fields only, so a key the server adds and this SDK does not know yet could ride along a copy but could not be changed: ModelConfig(id="x", vendor="qwen").copy_with(vendor="other") raised "unknown model config field", even though vendor was in the payload. Validate against _fields plus what the config actually carries. The guard keeps its teeth where it matters: a typo (temp=0.5) and inventing a wire field out of thin air (ModelConfig(id="x").copy_with(vendor="y")) still raise.
1 parent b6798e7 commit 5a1a604

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

‎dashscope/agentstudio/types/models.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,11 @@ def copy_with(self, **changes: Any) -> "ModelConfig":
468468
"""Copy with ``changes`` applied (``None`` drops a field).
469469
470470
Update replaces the whole config, so this changes one knob without
471-
clearing the rest.
471+
clearing the rest. A name outside :attr:`_fields` has to be carried
472+
by this config already, so a forward-compat field can be retuned and
473+
a typo still cannot.
472474
"""
473-
unknown = set(changes) - set(self._fields)
475+
unknown = set(changes) - set(self._fields) - set(self.to_dict())
474476
if unknown:
475477
raise ValueError(
476478
"unknown model config field(s): " + ", ".join(sorted(unknown)),

‎tests/unit/test_agentstudio_models.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,20 @@ def test_model_config_copy_with_can_drop_and_reject_typos():
268268

269269

270270
def test_model_config_unknown_fields_survive_copy_with():
271-
"""Unknown (forward-compat) fields survive copy_with."""
271+
"""Forward-compat fields survive copy_with and can be retuned."""
272272
config = ModelConfig(id="qwen3-max", vendor="qwen")
273273
assert config.copy_with(temperature=0.5).to_dict() == {
274274
"id": "qwen3-max",
275275
"vendor": "qwen",
276276
"temperature": 0.5,
277277
}
278+
assert config.copy_with(vendor="other").to_dict() == {
279+
"id": "qwen3-max",
280+
"vendor": "other",
281+
}
282+
# still not a way to invent wire fields: neither known nor carried
283+
with pytest.raises(ValueError, match="unknown model config field"):
284+
config.copy_with(vendorx="nope")
278285

279286

280287
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)