-
Notifications
You must be signed in to change notification settings - Fork 322
Update _base.py #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update _base.py #410
Conversation
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
There was a problem hiding this 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
-
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. ↩
There was a problem hiding this 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。
我在代码中发现了一个可以提高代码清晰度的地方,请查看我的具体评论。
| populate_by_name=True, | ||
| alias_generator=to_camel_custom, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了提高代码的清晰度和可维护性,建议在此处显式设置 ser_by_alias=True 并为 alias_generator 添加尾随逗号。
- 显式
ser_by_alias: 虽然ser_by_alias=True是 Pydantic v2ConfigDict中的默认设置,但显式声明可以清楚地表明意图,即模型在序列化时应使用别名。考虑到之前的配置中曾尝试设置此行为,显式声明可以避免未来因不了解默认值而产生的困惑。 - 尾随逗号: 为
alias_generator添加尾随逗号是良好的 Python 风格,这使得未来添加新配置项时,git diff 会更清晰,只显示新增的一行。
populate_by_name=True,
ser_by_alias=True,
alias_generator=to_camel_custom,默认使用别名进行序列化
fix: ruff linting errors
|
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:
To downgrade to Pydantic version v2.10, we'd need to update every call to |
|
Thank you for your reply. I will upgrade my version of Pydantic. The model_dump method is indeed a difficult problem to handle. In some places, the object of BaseModel is returned
草原狼
***@***.***
|
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:
CONTRIBUTINGGuide.fix:which represents bug fixes, and correlates to a SemVer patch.feat:represents a new feature, and correlates to a SemVer minor.feat!:, orfix!:,refactor!:, etc., which represent a breaking change (indicated by the!) and will result in a SemVer major.bash scripts/format.shfrom the repository root to format)Fixes #<issue_number_goes_here> 🦕