{"record":{"id":"6665c6930c84f34f","repo":"langchain-ai/langchain","slug":"model-must-be-a-pydantic-model","errorCode":null,"errorMessage":"Model must be a Pydantic model.","messagePattern":"Model must be a Pydantic model\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/utils/function_calling.py","lineNumber":190,"sourceCode":"\n            If not provided, the description of the schema will be used.\n        rm_titles: Whether to remove titles from the schema.\n\n    Raises:\n        TypeError: If the model is not a Pydantic model.\n        TypeError: If the model contains types that cannot be converted to JSON schema.\n\n    Returns:\n        The function description.\n    \"\"\"\n    try:\n        if hasattr(model, \"model_json_schema\"):\n            schema = model.model_json_schema()  # Pydantic 2\n        elif hasattr(model, \"schema\"):\n            schema = model.schema()  # Pydantic 1\n        else:\n            msg = \"Model must be a Pydantic model.\"\n            raise TypeError(msg)\n    except PydanticInvalidForJsonSchema as e:\n        model_name = getattr(model, \"__name__\", str(model))\n        msg = (\n            f\"Failed to generate JSON schema for '{model_name}': {e}\\n\\n\"\n            \"Tool argument schemas must be JSON-serializable. If your schema includes \"\n            \"custom Python classes, consider:\\n\"\n            \"  1. Converting them to Pydantic models with JSON-compatible fields\\n\"\n            \"  2. Using primitive types (str, int, float, bool, list, dict) instead\\n\"\n            \"  3. Passing the data as serialized JSON strings\\n\\n\"\n        )\n        raise PydanticInvalidForJsonSchema(msg) from e\n    return _convert_json_schema_to_openai_function(\n        schema, name=name, description=description, rm_titles=rm_titles\n    )\n\n\ndef _get_python_function_name(function: Callable[..., Any]) -> str:\n    \"\"\"Get the name of a Python function.\"\"\"","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/utils/function_calling.py#L172-L208","documentation":"Error \"Model must be a Pydantic model.\" thrown in langchain-ai/langchain.","triggerScenarios":"Raised when convert_to_openai_function/tool is given a class that is not a Pydantic BaseModel subclass (e.g. a plain class or TypedDict) where a Pydantic model was required.","commonSituations":"See trigger scenarios.","solutions":["Pass a subclass of pydantic.BaseModel (v1 or v2) instead of a dict, dataclass, or TypedDict.","If you have a plain function, use convert_to_openai_function or the @tool decorator, which builds the schema for you.","If you have a JSON schema dict, pass it directly to convert_to_openai_tool instead of convert_to_openai_function."],"exampleFix":"from pydantic import BaseModel\nclass MySchema(BaseModel):\n    query: str\ntool = convert_to_openai_function(MySchema)","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}