BerriAI/litellm · error · ValueError

Max depth of exceeded while processing schema.

Error message

Max depth of {DEFAULT_MAX_RECURSE_DEPTH} exceeded while processing schema.

What it means

Recursive schema sanitizer guard: _fix_enum_empty_strings (or a sibling processor) exceeded DEFAULT_MAX_RECURSE_DEPTH while walking the tool/response_schema, protecting against pathological or cyclic nested schemas.

Solutions

  1. Reduce nesting depth of the schema.
  2. Flatten or simplify the JSON schema passed in the request.

Example fix

# remove deeply nested $ref/anyOf chains from the response schema.
Defensive patterns

Strategy: validation

When it happens

Trigger: Triggered when schema processing exceeds DEFAULT_MAX_RECURSE_DEPTH due to excessive nesting.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/3829b752bbde83cf. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/vertex_ai/common_utils.py:512

def _check_text_in_content(parts: list[PartType]) -> bool:
    """
    check that user_content has 'text' parameter.
        - Known Vertex Error: Unable to submit request because it must have a text parameter.
        - 'text' param needs to be present (empty strings are valid)
        - Relevant Issue: https://github.com/BerriAI/litellm/issues/5515
    """
    has_text_param = False
    for part in parts:
        if "text" in part and part.get("text") is not None:
            has_text_param = True

    return has_text_param


def _fix_enum_empty_strings(schema, depth=0):
    """Fix empty strings in enum values by replacing them with None. Gemini doesn't accept empty strings in enums."""
    if depth > DEFAULT_MAX_RECURSE_DEPTH:
        raise ValueError(f"Max depth of {DEFAULT_MAX_RECURSE_DEPTH} exceeded while processing schema.")

    if "enum" in schema and isinstance(schema["enum"], list):
        schema["enum"] = [None if value == "" else value for value in schema["enum"]]

    # Reuse existing recursion pattern from convert_anyof_null_to_nullable
    properties: Final = schema.get("properties", None)
    if properties is not None:
        for _, value in properties.items():
            _fix_enum_empty_strings(value, depth=depth + 1)

    items: Final = schema.get("items", None)
    if items is not None:
        _fix_enum_empty_strings(items, depth=depth + 1)


def _fix_enum_types(schema, depth=0):
    """Remove `enum` fields when the schema type is not string.

View on GitHub (pinned to 77b7c6c40c)