Skip to content

Fix JSONSchema serialization inconsistency with alias field#339

Open
veeceey wants to merge 1 commit intomistralai:mainfrom
veeceey:fix/issue-310-jsonschema-serialization
Open

Fix JSONSchema serialization inconsistency with alias field#339
veeceey wants to merge 1 commit intomistralai:mainfrom
veeceey:fix/issue-310-jsonschema-serialization

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes #310 - JSONSchema serialization returns null for schema field when model_dump() is called without by_alias=True.

Problem

The JSONSchema.model_serializer was attempting to retrieve the schema_definition field using its alias "schema", but when by_alias=False, the serialized dict contains the field name "schema_definition" instead. This caused the schema field to be set to None incorrectly.

Solution

Updated the serializer to check both the alias key and the field name when retrieving values, ensuring consistent behavior regardless of the by_alias parameter.

Changes

  • Modified JSONSchema.serialize_model to try both alias and field name when getting values
  • Added comprehensive test cases to verify serialization works with and without by_alias
  • Ensured both serialization methods produce identical results

Test Results

$ python3 -m pytest tests/test_jsonschema_serialization.py -v
============================= test session starts ==============================
platform darwin -- Python 3.14.2, pytest-9.0.2, pluggy-1.6.0
cachedir: .pytest_cache
rootdir: /Users/vc/open source/client-python
configfile: pyproject.toml
plugins: anyio-4.12.1, mock-3.15.1, asyncio-1.3.0
collecting ... collected 3 items

tests/test_jsonschema_serialization.py::test_jsonschema_serialization_without_by_alias PASSED [ 33%]
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_with_by_alias PASSED [ 66%]
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_consistency PASSED [100%]

========================= 3 passed, 1 warning in 0.26s =========================

All existing tests continue to pass:

$ python3 -m pytest tests/ -v
============================= test session starts ==============================
platform darwin -- Python 3.14.2, pytest-9.0.2, pluggy-1.6.0
cachedir: .pytest_cache
rootdir: /Users/vc/open source/client-python
configfile: pyproject.toml
plugins: anyio-4.12.1, mock-3.15.1, asyncio-1.3.0
collecting ... collected 5 items

tests/test_jsonschema_serialization.py::test_jsonschema_serialization_without_by_alias PASSED [ 20%]
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_with_by_alias PASSED [ 40%]
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_consistency PASSED [ 60%]
tests/test_prepare_readme.py::test_rewrite_relative_links_keeps_absolute PASSED [ 80%]
tests/test_prepare_readme.py::test_main_prints_rewritten_readme_with_defaults PASSED [100%]

========================= 5 passed, 1 warning in 0.26s =========================

Impact

This fix ensures that:

  • model_dump() and model_dump(by_alias=True) produce consistent results
  • Debugging and logging code that uses model_dump() will now see the correct schema
  • No breaking changes - existing code using by_alias=True continues to work

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG CLIENT]: JSONSchema Serialization Inconsistency with Alias Field

1 participant