Skip to content

Conversation

@coyoteXujie
Copy link

Pydantic v2 中适配字段名和字段别名的方式,原始方式
model_config = ConfigDict(
# SEE: https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.populate_by_name
validate_by_name=True,
validate_by_alias=True,
serialize_by_alias=True,
alias_generator=to_camel_custom,
)
会导致 实例化 AgentCard 方法的时候 报错
pydantic_core._pydantic_core.ValidationError: 2 validation errors for AgentCard defaultInputModes
Field required [type=missing, input_value={'name': 'Hello World Age...ed_extended_card': True}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
defaultOutputModes

Description

Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Follow the CONTRIBUTING Guide.
  • Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
    • Important Prefixes for release-please:
      • fix: which represents bug fixes, and correlates to a SemVer patch.
      • feat: represents a new feature, and correlates to a SemVer minor.
      • feat!:, or fix!:, refactor!:, etc., which represent a breaking change (indicated by the !) and will result in a SemVer major.
  • Ensure the tests and linter pass (Run bash scripts/format.sh from the repository root to format)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

Pydantic v2 中适配字段名和字段别名的方式,原始方式
    model_config = ConfigDict(
        # SEE: https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.populate_by_name
        validate_by_name=True,
        validate_by_alias=True,
        serialize_by_alias=True,
        alias_generator=to_camel_custom,
    )
会导致  实例化 AgentCard 方法的时候 报错
pydantic_core._pydantic_core.ValidationError: 2 validation errors for AgentCard
defaultInputModes
  Field required [type=missing, input_value={'name': 'Hello World Age...ed_extended_card': True}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.10/v/missing
defaultOutputModes
@coyoteXujie coyoteXujie requested a review from a team as a code owner August 15, 2025 11:15
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @coyoteXujie, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决 Pydantic v2 中模型配置导致实例化 AgentCard 时出现验证错误的问题。通过将 ConfigDict 中的 validate_by_name、validate_by_alias 和 serialize_by_alias 替换为 populate_by_name,修复了字段名和字段别名适配的兼容性问题,确保模型能够正确实例化。

Highlights

  • Pydantic v2 配置更新: 将 src/a2a/_base.py 中 A2ABaseModel 的 model_config 从 validate_by_name、validate_by_alias 和 serialize_by_alias 更新为 populate_by_name,以适应 Pydantic v2 的新配置方式。
  • 修复模型实例化错误: 解决了在 Pydantic v2 中实例化 AgentCard 模型时出现的 ValidationError,该错误是由于旧的配置方式导致字段名和别名处理不当引起的。
  • 简化别名处理: 通过使用 populate_by_name 替代多个独立的配置项,简化了 Pydantic 模型中字段名和别名填充的逻辑。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

感谢您的贡献!此更改正确地修复了 Pydantic v2 的模型配置问题,将无效的配置键替换为正确的 populate_by_name=True。这解决了在实例化模型时因字段名和别名处理不当而导致的 ValidationError

为了使拉取请求更清晰,建议遵循仓库贡献指南中的约定式提交(Conventional Commits)规范来更新标题,例如使用 fix: Correct Pydantic v2 model configuration

我在代码中发现了一个可以提高代码清晰度的地方,请查看我的具体评论。

Comment on lines +34 to 35
populate_by_name=True,
alias_generator=to_camel_custom,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了提高代码的清晰度和可维护性,建议在此处显式设置 ser_by_alias=True 并为 alias_generator 添加尾随逗号。

  1. 显式 ser_by_alias: 虽然 ser_by_alias=True 是 Pydantic v2 ConfigDict 中的默认设置,但显式声明可以清楚地表明意图,即模型在序列化时应使用别名。考虑到之前的配置中曾尝试设置此行为,显式声明可以避免未来因不了解默认值而产生的困惑。
  2. 尾随逗号: 为 alias_generator 添加尾随逗号是良好的 Python 风格,这使得未来添加新配置项时,git diff 会更清晰,只显示新增的一行。
        populate_by_name=True,
        ser_by_alias=True,
        alias_generator=to_camel_custom,

默认使用别名进行序列化
fix: ruff linting errors
@mikeas1
Copy link
Contributor

mikeas1 commented Aug 18, 2025

Unfortunately, our current use of Pydantic is only compatible with v2.11. If you use v2.10, you'll run into errors that you discovered.

There are 2 problems:

  1. We use validate_by_alias and validate_by_name. These were only introduced in v2.11. We could switch to using populate_by_name=True, which is compatible.
  2. We use serialize_by_alias. This is the key problem that prevents v2.10 backwards compatibility. The protocol defines camelCase property names, so we MUST serialize our Pydantic models using camelCase property names. However, our property names are in snake_case to be idiomatic. We get around this by defining camelCase aliases and always serializing the models using the alias.

To downgrade to Pydantic version v2.10, we'd need to update every call to model_dump/mode_dump_json to specify by_alias=True.

@mikeas1 mikeas1 closed this Aug 18, 2025
@coyoteXujie
Copy link
Author

coyoteXujie commented Aug 19, 2025 via email

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