{"record":{"id":"b15a69eedbc54891","repo":"PrefectHQ/fastmcp","slug":"can-not-apply-name-to-non-object-schema-name","errorCode":null,"errorMessage":"Can not apply name to non-object schema: {name}","messagePattern":"Can not apply name to non-object schema: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/utilities/json_schema_type.py","lineNumber":237,"sourceCode":"        class Name:\n            name: NameType\n        ```\n    \"\"\"\n    # Boolean schemas (JSON Schema 2020-12 §4.3.2; also valid since draft-06)\n    if schema is True:\n        return Any\n    if schema is False:\n        return _UnsatisfiableType  # type: ignore[return-value]  # ty:ignore[invalid-return-type]\n\n    # Normalise YAML-parsed types (datetime/date → str, non-str keys → str)\n    # so that downstream json.dumps/hashing and default values work correctly.\n    schema = _normalize_yaml_types(schema)\n\n    # Always use the top-level schema for references\n    if schema.get(\"type\") == \"object\":\n        return _object_schema_to_type(schema, schemas=schema, name=name)\n    elif name:\n        raise ValueError(f\"Can not apply name to non-object schema: {name}\")\n    result = _schema_to_type(schema, schemas=schema)\n    return result  # type: ignore[return-value]  # ty:ignore[invalid-return-type]\n\n\ndef _hash_schema(schema: Mapping[str, Any]) -> str:\n    \"\"\"Generate a deterministic hash for schema caching.\n\n    Handles non-JSON-native types (datetime, date, bool keys) that can\n    appear in schemas loaded from YAML, which auto-parses date strings.\n    Uses ``default=str`` for unserializable values and drops ``sort_keys``\n    to avoid ``TypeError`` when dicts mix ``bool`` and ``str`` keys.\n    \"\"\"\n    try:\n        raw = json.dumps(schema, sort_keys=True, default=str)\n    except TypeError:\n        # Mixed key types (bool + str) can't be sorted; fall back\n        raw = json.dumps(schema, default=str)\n    return hashlib.sha256(raw.encode()).hexdigest()","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/utilities/json_schema_type.py#L219-L255","documentation":"`json_schema_to_type` can only attach a Python class `name` when the top-level schema is an object schema (mapping to a named Pydantic model). If a `name` is supplied for a non-object schema (array, string, number, boolean, ref, etc.) there is no model to name, so it raises ValueError.","triggerScenarios":"`json_schema_to_type({'type': 'array', 'items': {...}}, name='MyList')` or any non-object top-level schema passed with a non-None `name`, e.g. from elicitation handlers or default-parsing helpers.","commonSituations":"Wrapping tool output/elicitation schemas that are arrays or scalars while forcing a class name; assuming every tool schema is an object.","solutions":["Omit `name` for non-object schemas and let the primitive/array type be generated anonymously.","Wrap the non-object schema: `{'type': 'object', 'properties': {'value': inner_schema}}` and name that.","Ensure your schema root is `{'type': 'object', ...}` when a named model is required."],"exampleFix":"// before\nt = json_schema_to_type({'type': 'string'}, name='Name')\n// after\nt = json_schema_to_type({'type': 'object', 'properties': {'name': {'type': 'string'}}}, name='Name')","handlingStrategy":"type-guard","validationCode":"def can_name_schema(schema: dict, name) -> bool:\n    return not (name is not None and schema.get('type') != 'object')","typeGuard":"def is_object_schema(schema: dict) -> bool:\n    return isinstance(schema, dict) and schema.get('type') == 'object'","tryCatchPattern":"try:\n    t = json_schema_to_type(schema, name=name)\nexcept ValueError:\n    wrapped = {'type': 'object', 'properties': {'value': schema}, 'required': ['value']}\n    t = json_schema_to_type(wrapped, name=name)","preventionTips":["Only pass `name` when the root schema is an object","Wrap array/scalar roots in an object property if a named model is needed","Check tool/elicitation schemas' top-level type before conversion"],"tags":["json-schema","validation","pydantic","bad-argument"],"backgroundTag":"schema-validation-failed","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}