Skip to content

Question: field_validator Pydantic and ModelRetry Pydantic AI #3000

@YanSte

Description

@YanSte

Question

Hi Pydantic AI team,

I couldn’t find documentation about using field_validator together with ModelRetry.

Does using field_validator with ModelRetry actually work, and will it cause the LLM to retry with the updated sentence?

Or should I use ValueError?

Thanks in advance.

Additional Context

Here’s my case example:

@final
class Task(BaseModel):
    """A task in an execution plan."""

    task_name: str = Field(
        description='Name of the specialized task to execute.',
    )

    @field_validator('task_name', mode='before')
    @classmethod
    def _upper_task_name(cls, value: str):
        value = value.strip()
        if not value:
            raise ModelRetry('task_name is required and cannot be empty.')

        return value.upper()



# OR

    @field_validator('task_name', mode='before')
    @classmethod
    def _upper_task_name(cls, value: str):
        value = value.strip()
        if not value:
            raise ValueError('task_name is required and cannot be empty.')

        return value.upper()

# Code

agent = Agent(
...
output_type=Task,
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions