Skip to content

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
@veeceey
Copy link
Author

veeceey commented Feb 8, 2026

Manual Test Results

Environment

  • Python 3.14.2, macOS 15.4
  • Pydantic 2.11.4

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: result["schema"] was None because the serializer only checked for the alias key.
Result: PASS - schema field is correctly populated.

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"]
True

Result: PASS - consistent results regardless of by_alias parameter.

Test 4: Full test suite

$ python3 -m pytest tests/test_jsonschema_serialization.py -v
collected 3 items

tests/test_jsonschema_serialization.py::test_jsonschema_serialization_without_by_alias PASSED
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_with_by_alias PASSED
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_consistency PASSED

3 passed in 0.26s
$ python3 -m pytest tests/ -v
collected 5 items

tests/test_jsonschema_serialization.py::test_jsonschema_serialization_without_by_alias PASSED
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_with_by_alias PASSED
tests/test_jsonschema_serialization.py::test_jsonschema_serialization_consistency PASSED
tests/test_prepare_readme.py::test_rewrite_relative_links_keeps_absolute PASSED
tests/test_prepare_readme.py::test_main_prints_rewritten_readme_with_defaults PASSED

5 passed in 0.26s

Result: PASS - all existing and new tests pass.

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