Skip to content

json_schema_to_type crashes on Python keywords, boolean schemas, empty enums, and name collisions #3817

@strawgate

Description

@strawgate

Description

json_schema_to_type() in src/fastmcp/utilities/json_schema_type.py crashes on several valid JSON Schema inputs that commonly appear in OpenAPI-generated tool output schemas:

  1. Python keyword property names (class, return, import) — TypeError: Field names must not be keywords
  2. Boolean schemas (True / False) — AttributeError: 'bool' object has no attribute 'get'
  3. Empty enum ({"enum": []}) — AssertionError: literal "expected" cannot be empty
  4. Sanitized name collisions (foo-bar + foo_bar both become foo_bar) — TypeError: Field name duplicated
  5. Empty/whitespace property names ("", " ") — same duplicate field crash

These all cause the client to crash when parsing structured tool output from servers that use these schema patterns.

Reproduction

from fastmcp.utilities.json_schema_type import json_schema_to_type

# 1. Python keywords
json_schema_to_type({"type": "object", "properties": {"class": {"type": "string"}}})
# TypeError: Field names must not be keywords: 'class'

# 2. Boolean schema
json_schema_to_type(True)
# AttributeError: 'bool' object has no attribute 'get'

# 3. Empty enum
from pydantic import TypeAdapter
T = json_schema_to_type({"type": "object", "properties": {"x": {"enum": []}}})
TypeAdapter(T)  # AssertionError

# 4. Name collision
json_schema_to_type({"type": "object", "properties": {"foo-bar": {"type": "string"}, "foo_bar": {"type": "string"}}})
# TypeError: Field name duplicated: 'foo_bar'

Expected behavior

All of these should produce usable Python types without crashing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.openapiRelated to OpenAPI integration, parsing, or code generation features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions