zylon-ai/private-gpt · error · ValueError

Schema must define a 'type' field

Error message

Schema must define a 'type' field

What it means

Raised by _validate_json_schema_item in private_gpt/chat/schema_models.py when a JSON schema passed to create_model_from_json_schema lacks a 'type' field in strict mode. Schemas using anyOf/allOf are exempt and return early; only a 'regular' schema without 'type' triggers this. Strict mode is used for top-level and array-item schemas, so the root schema must always declare its type.

Source

Thrown at private_gpt/chat/schema_models.py:117

    if "anyOf" in schema:
        if not isinstance(schema["anyOf"], list):
            raise ValueError("'anyOf' must be an array of schemas")
        for sub_schema in schema["anyOf"]:
            _validate_json_schema_item(sub_schema, strict=False)
        return  # anyOf schemas don't need type field

    if "allOf" in schema:
        if not isinstance(schema["allOf"], list):
            raise ValueError("'allOf' must be an array of schemas")
        for sub_schema in schema["allOf"]:
            _validate_json_schema_item(sub_schema, strict=False)
        return  # allOf schemas don't need type field

    # Regular schema validation
    if "type" not in schema:
        if strict:
            raise ValueError("Schema must define a 'type' field")
        else:
            return  # Non-strict mode allows missing type

    if schema["type"] == "array" and "items" not in schema:
        raise ValueError("Array schemas must define 'items'")

    if schema["type"] == "array" and not isinstance(schema.get("items"), dict):
        raise ValueError("Array 'items' must be a dictionary representing JSON Schema")

    # Recursively validate array items
    if schema["type"] == "array":
        _validate_json_schema_item(schema.get("items", {}))


def _validate_json_schema(schema: dict[str, Any]) -> None:
    """Validate that the schema is a valid JSON Schema object."""
    if not isinstance(schema, dict):
        raise ValueError("Schema must be a dictionary representing JSON Schema")

View on GitHub (pinned to 4a030776a3)

Solutions

  1. Add "type": "object" (or the intended type) to the top-level schema dict before passing it in.
  2. If the schema intentionally has no single type, wrap the variants under "anyOf" or "allOf", which are accepted without a type field.
  3. If validating array items, ensure each item schema also declares "type" — items are validated with strict=True.
  4. Log the offending schema before validation to find which node is missing the field.

Example fix

// before
schema = {"properties": {"city": {"type": "string"}}}
model = create_model_from_json_schema(schema)

// after
schema = {"type": "object", "properties": {"city": {"type": "string"}}}
model = create_model_from_json_schema(schema)
Defensive patterns

Strategy: validation

Validate before calling

def has_type_or_composition(schema: dict) -> bool:
    return "type" in schema or any(k in schema for k in ("anyOf", "allOf", "oneOf"))

if not has_type_or_composition(schema):
    schema = {"type": "object", **schema}

Type guard

def is_typed_schema(s: object) -> bool:
    return isinstance(s, dict) and ("type" in s or any(k in s for k in ("anyOf", "allOf", "oneOf")))

Try / catch

try:
    model = create_model_from_json_schema(schema)
except ValueError as e:
    raise HTTPException(400, f"Invalid schema: {e}") from e

Prevention

When it happens

Trigger: Calling create_model_from_json_schema (or ChatContextFilter/structured-output APIs that build models from schemas) with a top-level schema like {"description": "..."} or {"properties": {...}} that has no "type" key. Also an array whose items schema omits "type", since items are validated strictly.

Common situations: User-supplied structured-output schemas from OpenAI tool specs or frontends that assume 'an object with properties' implies type; schemas copied from JSON Schema examples that use $ref at top level; LLM-generated schemas omitting the type keyword.

Related errors


AI-assisted analysis of zylon-ai/private-gpt@4a030776a3 (2026-08-15). Data as JSON: /api/errors/bd3e6ba7b0fee71a. Report an issue: GitHub.