Skip to content

Commit

Permalink
[Protocol] Use by_alias=True when dumping pydantic classes (mlc-ai#…
Browse files Browse the repository at this point in the history
…2452)

This PR sets the parameter `by_alias=True` for all the `model_dump`
of pydantic classes, so that aliases are always respected.
  • Loading branch information
MasterJH5574 authored May 28, 2024
1 parent 570380c commit 30e46b4
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/mlc_llm/interface/gen_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def gen_config( # pylint: disable=too-many-locals,too-many-arguments,too-many-b
apply_system_defaults_for_missing_fields(mlc_chat_config)
# Step 5. Dump the configuration file to output directory
with (output / "mlc-chat-config.json").open("w", encoding="utf-8") as out_file:
json.dump(mlc_chat_config.model_dump(), out_file, indent=2)
json.dump(mlc_chat_config.model_dump(by_alias=True), out_file, indent=2)
logger.info("Dumping configuration file to: %s", bold(out_file.name))


Expand Down
2 changes: 1 addition & 1 deletion python/mlc_llm/protocol/conversation_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def check_message_seps(cls, seps: List[str]) -> List[str]:

def to_json_dict(self) -> Dict[str, Any]:
"""Convert to a json dictionary"""
return self.model_dump(exclude_none=True)
return self.model_dump(by_alias=True, exclude_none=True)

@classmethod
def from_json_dict(cls: Type[T], json_dict: Dict[str, Any]) -> T:
Expand Down
2 changes: 1 addition & 1 deletion python/mlc_llm/protocol/openai_api_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def check_function_call_usage(self, conv_template: Conversation) -> None:
for tool in self.tools: # pylint: disable=not-an-iterable
if tool.type != "function":
raise BadRequestError("Only 'function' tool type is supported")
function_list.append(tool.function.model_dump())
function_list.append(tool.function.model_dump(by_alias=True))

conv_template.use_function_calling = True
conv_template.function_string = json.dumps(function_list)
Expand Down

0 comments on commit 30e46b4

Please sign in to comment.