fix(extra): respect Pydantic field aliases in response_format#399
Closed
abdelhadi703 wants to merge 1 commit intomistralai:mainfrom
Closed
fix(extra): respect Pydantic field aliases in response_format#399abdelhadi703 wants to merge 1 commit intomistralai:mainfrom
abdelhadi703 wants to merge 1 commit intomistralai:mainfrom
Conversation
…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>
Contributor
|
Thanks for the PR! However, See: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_json_schema |
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
Explicitly passes
by_alias=Truetomodel_json_schema()inresponse_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 inmodel_json_schema(), the explicitby_alias=Trueparameter makes the intent clear and guards against potential future behavior changes.This is a complementary fix to PR #339 which addresses the
JSONSchemaserializer inconsistency in the auto-generated code. This PR focuses on the contributableextra/code path.Changes
src/mistralai/extra/utils/response_format.py: Addedby_alias=Truetomodel.model_json_schema()calltests/test_response_format_alias.py: Added tests verifying alias usage in generated schemasTest plan