Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mistralai/extra/utils/response_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def response_format_from_pydantic_model(
model: type[CustomPydanticModel],
) -> ResponseFormat:
"""Generate a strict JSON schema from a pydantic model."""
model_schema = rec_strict_json_schema(model.model_json_schema())
model_schema = rec_strict_json_schema(model.model_json_schema(by_alias=True))
json_schema = JSONSchema.model_validate(
{"name": model.__name__, "schema": model_schema, "strict": True}
)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_response_format_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Test that response_format_from_pydantic_model respects Pydantic field aliases."""

from pydantic import BaseModel, Field

from mistralai.extra.utils.response_format import response_format_from_pydantic_model


class AliasedModel(BaseModel):
full_name: str = Field(alias="fullName")
phone_number: str = Field(alias="phoneNumber")


class NoAliasModel(BaseModel):
name: str
age: int


def test_response_format_uses_alias_keys():
"""Schema properties should use alias names, not Python field names."""
result = response_format_from_pydantic_model(AliasedModel)
schema = result.json_schema.schema_definition

props = schema["properties"]
assert "fullName" in props, f"Expected 'fullName' in properties, got {list(props)}"
assert "phoneNumber" in props, f"Expected 'phoneNumber' in properties, got {list(props)}"
assert "full_name" not in props, "Python field name 'full_name' should not appear"
assert "phone_number" not in props, "Python field name 'phone_number' should not appear"


def test_response_format_required_uses_aliases():
"""Required field list should use alias names."""
result = response_format_from_pydantic_model(AliasedModel)
schema = result.json_schema.schema_definition

required = schema["required"]
assert "fullName" in required
assert "phoneNumber" in required


def test_response_format_without_aliases_unchanged():
"""Models without aliases should work as before."""
result = response_format_from_pydantic_model(NoAliasModel)
schema = result.json_schema.schema_definition

props = schema["properties"]
assert "name" in props
assert "age" in props