diff --git a/litellm/llms/vertex_ai/common_utils.py b/litellm/llms/vertex_ai/common_utils.py index 49362bfba31..d4102ff0989 100644 --- a/litellm/llms/vertex_ai/common_utils.py +++ b/litellm/llms/vertex_ai/common_utils.py @@ -1018,6 +1018,8 @@ def _convert_schema_types(schema, depth=0): # Constraint keywords that apply to primitive types and should move # into the anyOf branch with the type. Without this they are left on # the parent next to anyOf and dropped by _filter_anyof_fields. + # multipleOf is not copied on purpose: filter_schema_fields drops it + # from the branch anyway, so carrying it would only imply it survives. scalar_constraint_fields: Final = { "enum", "pattern", @@ -1025,7 +1027,6 @@ def _convert_schema_types(schema, depth=0): "maxLength", "minimum", "maximum", - "multipleOf", "format", } @@ -1044,8 +1045,18 @@ def _convert_schema_types(schema, depth=0): # keywords so they survive _filter_anyof_fields. branch_fields = type_specific_fields if t in ("object", "array") else scalar_constraint_fields for field in branch_fields: - if field in schema: - item_schema[field] = deepcopy(schema[field]) + if field not in schema: + continue + if field == "enum": + # enums only land on the string branch, string values + # only: a mixed enum like ["a", 1] must not put 1 on + # the string branch. _fix_enum_types drops the rest. + if t == "string" and isinstance(schema["enum"], list): + string_values = [v for v in schema["enum"] if isinstance(v, str)] + if string_values: + item_schema["enum"] = string_values + continue + item_schema[field] = deepcopy(schema[field]) any_of.append(item_schema) # Remove the fields we moved into anyOf branches from the parent diff --git a/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py b/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py index b382a62013c..e0645630ba7 100644 --- a/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py +++ b/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py @@ -1794,3 +1794,34 @@ def test_type_array_keeps_scalar_constraints_on_anyof_branches(): }, "required": ["status"], } + + +def test_type_array_mixed_enum_only_string_values_on_string_branch(): + """A mixed-type enum must not leak non-string values onto the string branch.""" + from litellm.llms.vertex_ai.common_utils import _build_vertex_schema + + schema = { + "type": "object", + "properties": { + "mixed": {"type": ["string", "integer"], "enum": ["a", 1]}, + "numbers": {"type": ["string", "integer"], "enum": [1, 2]}, + }, + } + + assert _build_vertex_schema(schema) == { + "type": "object", + "properties": { + "mixed": { + "anyOf": [ + {"type": "string", "enum": ["a"]}, + {"type": "integer"}, + ] + }, + "numbers": { + "anyOf": [ + {"type": "string"}, + {"type": "integer"}, + ] + }, + }, + }