From a5ae2452080fdc1961aab729f86a0ce6963d6905 Mon Sep 17 00:00:00 2001 From: Lionel Date: Mon, 22 Sep 2025 12:31:38 +0200 Subject: [PATCH] add sanitization to auto-generated output tool name --- pydantic_ai_slim/pydantic_ai/_output.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pydantic_ai_slim/pydantic_ai/_output.py b/pydantic_ai_slim/pydantic_ai/_output.py index bd0245ae83..64e918e9cc 100644 --- a/pydantic_ai_slim/pydantic_ai/_output.py +++ b/pydantic_ai_slim/pydantic_ai/_output.py @@ -2,6 +2,7 @@ import inspect import json +import re from abc import ABC, abstractmethod from collections.abc import Awaitable, Callable, Sequence from dataclasses import dataclass, field @@ -67,6 +68,7 @@ DEFAULT_OUTPUT_TOOL_NAME = 'final_result' DEFAULT_OUTPUT_TOOL_DESCRIPTION = 'The final response which ends this conversation' +OUTPUT_TOOL_NAME_SANITIZER = re.compile(r'[^a-z0-9-_]') async def execute_traced_output_function( @@ -946,7 +948,9 @@ def build( if name is None: name = default_name if multiple: - name += f'_{object_def.name}' + # strip unsupported characters like "[" and "]" from generic class names + safe_name = OUTPUT_TOOL_NAME_SANITIZER.sub('', object_def.name or '') + name += f'_{safe_name}' i = 1 original_name = name