Skip to content

Use alias_generator=to_camel on generated Pydantic models instead of per-field aliases #852

@vdusek

Description

@vdusek

Background

We're on Pydantic ≥2.11. src/apify_client/_models.py is generated by datamodel-codegen: ~190 models with ~606 explicit Field(alias='camelCase') entries, e.g.:

source_type: Annotated[VersionSourceType | None, Field(alias='sourceType')] = None
is_public: Annotated[bool, Field(alias='isPublic', examples=[False])] = None

All models already share ConfigDict(extra='allow', populate_by_name=True).

Proposal

Drive the codegen so each model's config carries alias_generator=to_camel, and emit an explicit alias only where the conversion is irregular:

from pydantic.alias_generators import to_camel

model_config = ConfigDict(extra='allow', populate_by_name=True, alias_generator=to_camel)

Where it makes sense

Because this file is generated, the work lives in the generation + postprocess pipeline rather than in hand edits:

  • Irregular conversions still need an explicit alias. The existing override in datamodel_codegen_aliases.json (gitHubGistUrlgithub_gist_url) is a good example: to_camel('github_gist_url') yields githubGistUrl, not gitHubGistUrl, so such overrides must win over the generator.
  • scripts/postprocess_generated_models.py builds its alias map by AST-walking Field(alias=...) to synthesize the camelCase *Dict TypedDict variants. If aliases stop being emitted per field, that step must instead derive them via to_camel plus the override map. This is the main thing to verify.

Scope

Refactor only, smaller generated output. The wire format must stay identical, guarded by tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    t-toolingIssues with this label are in the ownership of the tooling team.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions