Fix JSONSchema serialization inconsistency with alias field#339
Open
veeceey wants to merge 1 commit intomistralai:mainfrom
Open
Fix JSONSchema serialization inconsistency with alias field#339veeceey wants to merge 1 commit intomistralai:mainfrom
veeceey wants to merge 1 commit intomistralai:mainfrom
Conversation
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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