Skip to content

Fix: Handle Pydantic v2 ValidationError in AgentLoader #1245

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hangfei
Copy link
Collaborator

@hangfei hangfei commented Jun 9, 2025

The AgentLoader previously attempted to access e.msg on exceptions encountered during agent loading. This caused a crash when the exception was a pydantic_core._pydantic_core.ValidationError from Pydantic v2+, as this error type does not have a msg attribute.

This commit modifies the exception handling in AgentLoader's _load_from_module_or_package and _load_from_submodule methods to use str(e) instead of e.msg. This ensures that the error message is correctly retrieved for all exception types, including Pydantic v2 ValidationError.

A new test case has been added to specifically verify this scenario by simulating a Pydantic v2 style error during agent loading. Existing tests for other error types (SyntaxError, NameError) within agent code have been updated to reflect the new RuntimeError wrapping behavior of the AgentLoader.

Fixes #1227

The AgentLoader previously attempted to access `e.msg` on exceptions encountered during agent loading. This caused a crash when the exception was a `pydantic_core._pydantic_core.ValidationError` from Pydantic v2+, as this error type does not have a `msg` attribute.

This commit modifies the exception handling in `AgentLoader`'s `_load_from_module_or_package` and `_load_from_submodule` methods to use `str(e)` instead of `e.msg`. This ensures that the error message is correctly retrieved for all exception types, including Pydantic v2 `ValidationError`.

A new test case has been added to specifically verify this scenario by simulating a Pydantic v2 style error during agent loading. Existing tests for other error types (SyntaxError, NameError) within agent code have been updated to reflect the new `RuntimeError` wrapping behavior of the AgentLoader.

Fixes #1227
@MyHackInfo
Copy link

Hi @hangfei it look good to me and please make sure run test case

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.

pydantic_core._pydantic_core.ValidationError
2 participants