{"record":{"id":"9272c36aa234c421","repo":"sgl-project/sglang","slug":"cannot-parse-schema-json-schema-the-schema-must","errorCode":null,"errorMessage":"Cannot parse schema {json_schema}. The schema must be either a Pydantic class, a dictionary or a string that contains the JSON schema specification","messagePattern":"Cannot parse schema (.+?)\\. The schema must be either a Pydantic class, a dictionary or a string that contains the JSON schema specification","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/utils.py","lineNumber":112,"sourceCode":"    json_schema\n        The JSON schema.\n    Returns\n    -------\n    str\n        The JSON schema converted to a string.\n    Raises\n    ------\n    ValueError\n        If the schema is not a dictionary, a string or a Pydantic class.\n    \"\"\"\n    if isinstance(json_schema, dict):\n        schema_str = json.dumps(json_schema)\n    elif isinstance(json_schema, str):\n        schema_str = json_schema\n    elif issubclass(json_schema, BaseModel):\n        schema_str = json.dumps(json_schema.model_json_schema())\n    else:\n        raise ValueError(\n            f\"Cannot parse schema {json_schema}. The schema must be either \"\n            + \"a Pydantic class, a dictionary or a string that contains the JSON \"\n            + \"schema specification\"\n        )\n    return schema_str\n\n\ndef get_exception_traceback():\n    etype, value, tb = sys.exc_info()\n    err_str = \"\".join(traceback.format_exception(etype, value, tb))\n    return err_str\n\n\ndef is_same_type(values: list):\n    \"\"\"Return whether the elements in values are of the same type.\"\"\"\n    if len(values) <= 1:\n        return True\n    else:","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/utils.py#L94-L130","documentation":"convert_json_schema_to_str accepts only a Pydantic BaseModel subclass, a dict, or a JSON-schema string; anything else (e.g. a random object, a type that isn't BaseModel, a list) raises ValueError.","triggerScenarios":"Passing json_schema as a non-string/non-dict/non-BaseModel value to sampling params (e.g. a TypedDict, dataclass, or None) via to_sampling_params / structured output helpers.","commonSituations":"Using response_format/json_schema with a dataclass or TypedDict instead of Pydantic; passing a schema loaded as a list of schemas.","solutions":["Convert the schema to a dict or JSON string before passing","Use a Pydantic BaseModel subclass for structured output schemas"],"exampleFix":"# before\nparams.json_schema = MyTypedDict\n# after\nparams.json_schema = json.dumps(MyAnnotatedSchema)  # or use a pydantic BaseModel","handlingStrategy":"type-guard","validationCode":"from pydantic import BaseModel\nok = isinstance(json_schema, (str, dict)) or (isinstance(json_schema, type) and issubclass(json_schema, BaseModel))","typeGuard":"def is_valid_schema(s):\n    from pydantic import BaseModel\n    return isinstance(s, (str, dict)) or (isinstance(s, type) and issubclass(s, BaseModel))","tryCatchPattern":null,"preventionTips":["Use Pydantic models or JSON strings for json_schema","Validate schema input before building sampling params"],"tags":["json-schema","structured-output","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}