This commit is contained in:
Mohd Quamar Tyagi 2026-09-12 14:53:31 -04:00 committed by GitHub
commit cd246a4306
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 0 deletions

View file

@ -584,6 +584,9 @@ def _fix_enum_empty_strings(schema, depth=0):
if depth > DEFAULT_MAX_RECURSE_DEPTH:
raise ValueError(f"Max depth of {DEFAULT_MAX_RECURSE_DEPTH} exceeded while processing schema.")
if not isinstance(schema, dict):
return
if "enum" in schema and isinstance(schema["enum"], list):
schema["enum"] = [None if value == "" else value for value in schema["enum"]]
@ -597,6 +600,12 @@ def _fix_enum_empty_strings(schema, depth=0):
if items is not None:
_fix_enum_empty_strings(items, depth=depth + 1)
anyof = schema.get("anyOf", None)
if anyof is not None and isinstance(anyof, list):
for item in anyof:
if isinstance(item, dict):
_fix_enum_empty_strings(item, depth=depth + 1)
def _fix_enum_types(schema, depth=0):
"""Remove `enum` fields when the schema type is not string.

View file

@ -885,6 +885,45 @@ def test_fix_enum_empty_strings():
assert "tablet" in enum_values
def test_fix_enum_empty_strings_anyof():
"""Test _fix_enum_empty_strings handles anyOf structures including nullable enums."""
from litellm.llms.vertex_ai.common_utils import _fix_enum_empty_strings
schema = {
"type": "object",
"properties": {
"mode": {
"anyOf": [
{"enum": ["", "fast", "slow"], "type": "string"},
{"type": "null"},
]
},
"complex_choice": {
"anyOf": [
{"enum": ["", "option1"], "type": "string"},
{"type": "integer"},
]
},
},
}
_fix_enum_empty_strings(schema)
mode_enum = schema["properties"]["mode"]["anyOf"][0]["enum"]
assert "" not in mode_enum
assert None in mode_enum
assert mode_enum == [None, "fast", "slow"]
choice_enum = schema["properties"]["complex_choice"]["anyOf"][0]["enum"]
assert "" not in choice_enum
assert None in choice_enum
assert choice_enum == [None, "option1"]
_fix_enum_empty_strings("string_input")
_fix_enum_empty_strings(None)
def test_get_vertex_model_id_from_url():
"""Test get_vertex_model_id_from_url with various URLs"""
from litellm.llms.vertex_ai.common_utils import get_vertex_model_id_from_url