a2aproject/a2a-python

[Feat]: Support `populate_by_name = True` for Pydantic models

ognis1205 opened this issue · 0 comments

Is your feature request related to a problem? Please describe.

Currently, instantiating models like APIKeySecurityScheme using keyword arguments for fields with aliases (e.g., in_) does not work as expected:

APIKeySecurityScheme(
    name='X-API-Key',
    in_=In.header,  # Raises validation error unless alias is respected
)

This happens because Pydantic requires populate_by_name = True in the model config to allow population via the field name instead of its alias (in in this case, which is a reserved keyword in Python).

Describe the solution you'd like

Introduce a common base model (A2ABaseModel) that all models inherit from, and configure it with populate_by_name = True via ConfigDict.

# src/a2a/_base.py
from pydantic import BaseModel, ConfigDict

class A2ABaseModel(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,  #Enables safer field population
    )

Then we pass the option --base-class a2a._base.MyBaseModel to the datamodel-codegen command.

Describe alternatives you've considered

  • Using **dict-style unpacking to avoid naming issues (e.g., **{'in': 'header'}), but this is less readable and more error-prone.

Additional context

#290 (comment)

Code of Conduct

  • I agree to follow this project's Code of Conduct