Fix JSONSchema serialization inconsistency with alias field#339
Fix JSONSchema serialization inconsistency with alias field#339veeceey wants to merge 1 commit intomistralai:mainfrom
Conversation
Manual Test ResultsEnvironment
Test 1: model_dump() without by_alias (the bug)>>> from mistralai.models.jsonschema import JSONSchema
>>> schema = JSONSchema(name="test", schema_definition={"type": "object", "properties": {"name": {"type": "string"}}})
>>> result = schema.model_dump()
>>> result["schema"]
{'type': 'object', 'properties': {'name': {'type': 'string'}}}Before fix: Test 2: model_dump(by_alias=True) still works>>> result_alias = schema.model_dump(by_alias=True)
>>> result_alias["schema"]
{'type': 'object', 'properties': {'name': {'type': 'string'}}}Result: PASS - by_alias=True continues to work as before. Test 3: Both serialization methods produce identical results>>> result = schema.model_dump()
>>> result_alias = schema.model_dump(by_alias=True)
>>> result["schema"] == result_alias["schema"]
True
>>> result["name"] == result_alias["name"]
TrueResult: PASS - consistent results regardless of by_alias parameter. Test 4: Full test suiteResult: PASS - all existing and new tests pass. |
Fixes mistralai#310 The JSONSchema.model_serializer was not handling the case where model_dump() is called without by_alias=True. When by_alias=False, the serialized dict contains field names (e.g., "schema_definition") instead of aliases (e.g., "schema"), causing the serializer to incorrectly set the schema field to None. This commit updates the serializer to check both the alias and field name when retrieving values, ensuring consistent behavior regardless of the by_alias parameter. Changes: - Updated JSONSchema.serialize_model to try both alias and field name - Added test cases to verify serialization works with and without by_alias - Ensured both serialization methods produce identical results
e085903 to
d74d34a
Compare
|
Hi @veeceey, thanks for working on this fix! The root cause analysis is spot on — the The fix itself (checking both alias and field name) is clean and the tests are solid. One concern: the modified file That said, +1 on merging this as-is since it fixes a real user-facing issue. The maintainers can decide how to handle the code-gen aspect. |
Summary
Fixes #310 - JSONSchema serialization returns
nullfor schema field whenmodel_dump()is called withoutby_alias=True.Problem
The
JSONSchema.model_serializerwas attempting to retrieve theschema_definitionfield using its alias"schema", but whenby_alias=False, the serialized dict contains the field name"schema_definition"instead. This caused the schema field to be set toNoneincorrectly.Solution
Updated the serializer to check both the alias key and the field name when retrieving values, ensuring consistent behavior regardless of the
by_aliasparameter.Changes
JSONSchema.serialize_modelto try both alias and field name when getting valuesby_aliasTest Results
All existing tests continue to pass:
Impact
This fix ensures that:
model_dump()andmodel_dump(by_alias=True)produce consistent resultsmodel_dump()will now see the correct schemaby_alias=Truecontinues to work