diff --git a/pydantic_ai_slim/pydantic_ai/_output.py b/pydantic_ai_slim/pydantic_ai/_output.py index cc05b1889..0b52ad8cd 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 @@ -70,6 +71,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( @@ -997,7 +999,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