Skip to content

Conversation

@Akshay9715
Copy link

Summary

This change updates the EnumSerializer in pydantic-core to properly respect the use_enum_values configuration flag when serializing Python Enum types. Specifically:

  • Adds a use_enum_values: bool field to the EnumSerializer struct.
  • Reads use_enum_values from the model’s model_config in build() (defaulting to false).
  • Updates to_python() and json_key() to unwrap the enum’s .value whenever use_enum_values is true, ensuring that nested models also serialize their enum fields as raw values.
  • Leaves existing behavior unchanged when use_enum_values is false or unset.

These edits resolve the inconsistency described in Issue #10570 so that enums in nested contexts now correctly output their underlying values.

Related issue number

fix #10570

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@Viicos
Copy link
Member

Viicos commented May 21, 2025

Thanks for the contribution @Akshay9715, but AI won't help in solving these kind of complex issues. As per my analysis, this isn't the way to go.

@Viicos Viicos closed this May 21, 2025
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