Skip to content

Allow customization of tool validation error messages #3566

@sigglep

Description

@sigglep

Problem

When tool calls fail validation, Pydantic AI sends "2 validation errors: ... Fix the errors and try again" to the LLM. Some models interpret this as user input rather than an internal error, causing confused responses.

Proposed

Add a way to customize/override the validation error message format. For example with a callable.
Even better if we'd be able to inject RunContext into that formatter so we could do conditionals based on what the state of the conversation is.

class Agent:
    def __init__(
        self,
        ...,`
        tool_retry_formatter: Callable[[str, RunContext], str] | None = None`
    ):
        ...

# Usage
def format_tool_error(error_message: str, ctx: RunContext) -> str:
    return f"Customizable text that tells the agent exactly what to do, that couldn't be explained pre-validation failure\n{error_message}"

Use Case

Prepending custom error

Benefits

  • Clearer error context for LLMs (being able to inform that THIS IS NOT THE USER)
  • Custom agent behavior on validation failures

References

Slack thread

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions