Skip to content

Surface Pydantic ValidationError to LLM in tool arg validation#1862

Open
idryzhov wants to merge 1 commit into
github:mainfrom
idryzhov:idryzhov-surface-pydantic-validation-error
Open

Surface Pydantic ValidationError to LLM in tool arg validation#1862
idryzhov wants to merge 1 commit into
github:mainfrom
idryzhov:idryzhov-surface-pydantic-validation-error

Conversation

@idryzhov

Copy link
Copy Markdown
Contributor

Tool-argument validation failures raised as pydantic ValidationError are now returned to the model as a clean, actionable message built from each error's loc and msg, instead of the generic redacted text. All other exceptions stay fully redacted.

The except branch lives here because tool arguments are deserialized via ptype.model_validate(args) just above in the same try block, so this is where the ValidationError originates and is the right place to catch it, ahead of the generic redaction fallback.

Safe to surface: the invalid values are arguments the LLM itself supplied, and validator messages are authored by tool developers. The user-facing text is assembled from only loc + msg; the full str(exc) (including raw input) is kept in the debug-only error field.

@idryzhov idryzhov requested a review from a team as a code owner June 30, 2026 23:56
Copilot AI review requested due to automatic review settings June 30, 2026 23:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Python SDK’s define_tool wrapper to surface Pydantic ValidationError details to the model in a structured, actionable form (derived from each error’s loc + msg) rather than the generic redacted failure text, and adds unit tests to lock in the behavior.

Changes:

  • Add a dedicated ValidationError catch in define_tool’s wrapped_handler to build an “Invalid tool arguments:” message from Pydantic error entries.
  • Add unit tests ensuring validator failures and extra="forbid" errors include the relevant field/key in the model-facing text.
Show a summary per file
File Description
python/copilot/tools.py Catch pydantic.ValidationError in tool invocation wrapper and format a model-facing “Invalid tool arguments” message.
python/test_tools.py Add tests verifying validation failures are surfaced to the model and include field names (including extra-field loc).

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment thread python/copilot/tools.py Outdated
Comment thread python/copilot/tools.py Outdated
@idryzhov idryzhov force-pushed the idryzhov-surface-pydantic-validation-error branch from ecb8e1e to bce6b79 Compare July 1, 2026 00:09
Tool-argument validation failures raised as pydantic ValidationError are
now returned to the model as a clean, actionable message built from each
error's loc and msg, instead of the generic redacted text. All other
exceptions stay fully redacted.

The ValidationError handler is scoped to only the ptype.model_validate(args)
call that deserializes the tool arguments, so a ValidationError raised from
within a handler body is not surfaced and stays redacted by the broad
fallback like any other exception.

Safe to surface: the invalid values are arguments the LLM itself supplied,
and validator messages are authored by tool developers. The user-facing
text is assembled from only loc + msg; the full str(exc) (including raw
input) is kept in the debug-only error field.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@idryzhov idryzhov force-pushed the idryzhov-surface-pydantic-validation-error branch from bce6b79 to 044263a Compare July 1, 2026 00:11
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