Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pydantic · Pipe

alias_generator

alias_generator is a ConfigDict option that derives field aliases from their names. A callable applies to both validation and serialization; AliasGenerator supports different rules for each.

How it works

Set alias_generator in ConfigDict to a callable that receives each field name and returns its alias. The same callable applies during validation and serialization; use AliasGenerator when those two directions need different rules.

Syntax

model_config = ConfigDict(alias_generator=callable_or_alias_generator)

Parameters

NameTypeDescription
alias_generatorCallable[[str], str] | AliasGenerator | NoneA callable mapping a field name to an alias, an AliasGenerator instance for separate validation and serialization rules, or None.

Key points

  • The setting is a name-to-alias function: each field name is passed in and the returned string becomes its alias.
  • Instead of one callable, configure an AliasGenerator when validation and serialization require separate alias rules.
  • If omitted, no alias generator is configured.
  • The documented built-ins cover PascalCase, camelCase, and snake_case transformations.
  • The accepted configuration value is a string-to-string callable, an AliasGenerator, or None.

Common mistakes

  • A plain callable controls aliases in both directions, so use AliasGenerator if input and output naming must differ.

Example: Generate PascalCase aliases

from pydantic import BaseModel, ConfigDict
from pydantic.alias_generators import to_pascal

class Voice(BaseModel):
    model_config = ConfigDict(alias_generator=to_pascal)
    name: str
    language_code: str

voice = Voice(Name="Filiz", LanguageCode="tr-TR")
print(voice.model_dump(by_alias=True))

The generator converts field names to PascalCase for input aliases and serialized output.

When to use it

Adapt external naming conventions

A Python API exchanges PascalCase keys while model attributes use snake_case.

Separate input and output aliases

A model must accept one naming convention but serialize another.

Automate field alias declarations

Multiple fields need consistent generated aliases without declaring each alias manually.

Interview questions

QWhat does alias_generator do?

alias_generator is a configuration option whose value can be a callable, an AliasGenerator, or None. A callable maps each field name to an alias and is used for both validation and serialization.

Follow-up: How would you use separate rules for input and output? Use an AliasGenerator instance rather than one callable, because it supports distinct validation and serialization generators.

QHow is alias_generator configured and typed?

Configure it through model_config = ConfigDict(alias_generator=...). The callable receives a field name and returns the alias used by the model.

Follow-up: What should the callable return? It should return a string alias for the supplied field name; the documented type is Callable[[str], str].

QWhich built-in generators are available?

Pydantic includes to_pascal, to_camel, and to_snake as built-in alias generators. For example, to_pascal maps language_code to LanguageCode, which can be used for model input and alias-based serialization.

Follow-up: How do you emit aliases when dumping a model? Call model_dump(by_alias=True) so serialization uses the generated aliases.

  • ConfigDict
  • AliasGenerator
  • to_pascal
  • to_camel
  • to_snake
  • BaseModel.model_dump

Source: Configuration - Pydantic. Checked 2026-10-15.