Skip to content

fix(extra): respect Pydantic field aliases in response_format#399

Closed
abdelhadi703 wants to merge 1 commit intomistralai:mainfrom
abdelhadi703:fix/pydantic-alias-serialization
Closed

fix(extra): respect Pydantic field aliases in response_format#399
abdelhadi703 wants to merge 1 commit intomistralai:mainfrom
abdelhadi703:fix/pydantic-alias-serialization

Conversation

@abdelhadi703
Copy link

Summary

Explicitly passes by_alias=True to model_json_schema() in response_format_from_pydantic_model() to ensure Pydantic field aliases are respected in generated JSON schemas.

Related to #310

Problem

When a Pydantic model uses Field(alias="..."), the generated JSON schema should use alias names (e.g., fullName) rather than Python field names (e.g., full_name). While Pydantic v2 defaults to using aliases in model_json_schema(), the explicit by_alias=True parameter makes the intent clear and guards against potential future behavior changes.

This is a complementary fix to PR #339 which addresses the JSONSchema serializer inconsistency in the auto-generated code. This PR focuses on the contributable extra/ code path.

Changes

  • src/mistralai/extra/utils/response_format.py: Added by_alias=True to model.model_json_schema() call
  • tests/test_response_format_alias.py: Added tests verifying alias usage in generated schemas

Test plan

  • Test that aliased fields appear with alias names in schema properties
  • Test that required fields list uses alias names
  • Test that models without aliases continue to work unchanged

…ponse_format

Ensures Pydantic field aliases are explicitly respected when generating
JSON schemas via response_format_from_pydantic_model(). While Pydantic v2
uses aliases by default in model_json_schema(), adding by_alias=True makes
the intent explicit and guards against potential future behavior changes.

Related to mistralai#310

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@louis-sanna-dev
Copy link
Contributor

louis-sanna-dev commented Mar 2, 2026

Thanks for the PR! However, by_alias=True is already the default for model_json_schema() in Pydantic v2, so this change is effectively a no-op.

See: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_json_schema

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.

2 participants