fix(vertex): attach enum to string branch only, drop multipleOf copy

This commit is contained in:
Azhar 2026-09-29 04:04:53 +05:30
parent 6f2fb389a1
commit e5bd410251
2 changed files with 45 additions and 3 deletions

View file

@ -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

View file

@ -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"},
]
},
},
}