diff --git a/src/google/adk/workflow/_retry_config.py b/src/google/adk/workflow/_retry_config.py index 87862e42a5b..75be0771741 100644 --- a/src/google/adk/workflow/_retry_config.py +++ b/src/google/adk/workflow/_retry_config.py @@ -16,6 +16,7 @@ from __future__ import annotations +from collections.abc import Sequence from typing import Any from pydantic import BaseModel @@ -61,10 +62,15 @@ class RetryConfig(BaseModel): @field_validator('exceptions', mode='before') @classmethod - def _normalize_exceptions(cls, v: list[Any] | None) -> list[str] | None: + def _normalize_exceptions(cls, v: Any) -> list[str] | None: """Converts exception classes to their class names for uniform handling.""" if v is None: return None + if isinstance(v, (str, bytes, bytearray)) or not isinstance(v, Sequence): + raise WorkflowConfigurationError( + 'exceptions must be a sequence of exception class names (str) or' + f' exception classes, got {type(v).__name__}: {v!r}' + ) normalized = [] for item in v: if isinstance(item, str): diff --git a/tests/unittests/workflow/utils/test_retry_utils.py b/tests/unittests/workflow/utils/test_retry_utils.py index a25fc5358f1..5b9c5296fd1 100644 --- a/tests/unittests/workflow/utils/test_retry_utils.py +++ b/tests/unittests/workflow/utils/test_retry_utils.py @@ -31,6 +31,7 @@ from google.adk.workflow.utils._retry_utils import _node_retry_config from google.adk.workflow.utils._retry_utils import _should_retry_node from google.adk.workflow.utils._retry_utils import _with_retry_config +from pydantic import ValidationError import pytest @@ -243,6 +244,29 @@ def test_configuration_and_invariant_errors_are_never_retried( _should_retry_node(error, config, NodeState(attempt_count=1)) is False ) + @pytest.mark.parametrize( + "invalid_exceptions", + [ + "ValueError", + b"ValueError", + ValueError, + 123, + {"ValueError": True}, + ], + ids=["str", "bytes", "single_class", "int", "dict"], + ) + def test_non_sequence_or_string_exceptions_raises_validation_error( + self, invalid_exceptions + ) -> None: + """Bare strings or non-sequence values must be rejected during validation. + + Without an explicit guard, a bare string like ``"ValueError"`` is iterated + character-by-character into ``['V', 'a', ...]`` and silently never matches, + while non-iterable values like a bare exception class raise a raw TypeError. + """ + with pytest.raises(ValidationError, match="must be a sequence"): + RetryConfig(exceptions=invalid_exceptions) + class TestDelegatesItemRetry: