Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:BerriAI Litellm JSON Validation Rule

From Leeroopedia
Attribute Value
Sources litellm/litellm_core_utils/json_validation_rule.py
Domains JSON Schema, Validation, Tool Normalization
Last Updated 2026-02-15 16:00 GMT

Overview

Provides JSON schema type normalization and response validation for LLM tool definitions and structured outputs.

Description

This module addresses two concerns related to JSON schema handling in LiteLLM:

  1. Schema type normalization -- Some providers (notably certain Google services) use uppercase type names like BOOLEAN, STRING, ARRAY, OBJECT, while standard JSON Schema requires lowercase. The normalize_json_schema_types function recursively traverses a schema and converts all uppercase type values to their lowercase equivalents. The companion normalize_tool_schema function applies this normalization to a tool definition's function parameters.
  1. Response validation -- The validate_schema function validates whether a JSON string response conforms to a given JSON schema. It uses the jsonschema library for validation and raises JSONSchemaValidationError on parse failures or schema mismatches.

Both normalization functions include recursion-depth protection via DEFAULT_MAX_RECURSE_DEPTH.

Usage

Import normalize_json_schema_types or normalize_tool_schema when processing tool definitions from providers that may use non-standard type casing. Import validate_schema when enforcing structured output compliance against a JSON schema.

Code Reference

Source Location

litellm/litellm_core_utils/json_validation_rule.py (121 lines)

Signature

def normalize_json_schema_types(
    schema: Union[Dict[str, Any], List[Any], Any],
    depth: int = 0,
    max_depth: int = DEFAULT_MAX_RECURSE_DEPTH,
) -> Union[Dict[str, Any], List[Any], Any]

def normalize_tool_schema(tool: Dict[str, Any]) -> Dict[str, Any]

def validate_schema(schema: dict, response: str) -> None

Import

from litellm.litellm_core_utils.json_validation_rule import (
    normalize_json_schema_types,
    normalize_tool_schema,
    validate_schema,
)

I/O Contract

normalize_json_schema_types

Direction Name Type Description
Input schema Union[Dict, List, Any] JSON schema to normalize
Input depth int Current recursion depth (default: 0)
Input max_depth int Maximum recursion depth
Output return Union[Dict, List, Any] Schema with all uppercase type names converted to lowercase

normalize_tool_schema

Direction Name Type Description
Input tool Dict[str, Any] Tool definition with optional function.parameters
Output return Dict[str, Any] Tool with normalized parameter schema types

validate_schema

Direction Name Type Description
Input schema dict JSON Schema to validate against
Input response str JSON response string to validate
Output raises JSONSchemaValidationError If the response is not valid JSON or does not match the schema

Usage Examples

from litellm.litellm_core_utils.json_validation_rule import (
    normalize_json_schema_types,
    normalize_tool_schema,
    validate_schema,
)

# Normalize uppercase types from Google-style schemas
schema = {
    "type": "OBJECT",
    "properties": {
        "name": {"type": "STRING"},
        "active": {"type": "BOOLEAN"},
        "tags": {"type": "ARRAY", "items": {"type": "STRING"}},
    },
}
normalized = normalize_json_schema_types(schema)
# {"type": "object", "properties": {"name": {"type": "string"}, ...}}

# Normalize a tool definition
tool = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "parameters": {
            "type": "OBJECT",
            "properties": {"city": {"type": "STRING"}},
        },
    },
}
normalized_tool = normalize_tool_schema(tool)

# Validate a JSON response against a schema
schema = {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}
validate_schema(schema, '{"name": "Alice"}')  # passes

# This will raise JSONSchemaValidationError:
# validate_schema(schema, '{"age": 30}')

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment