langchain-ai/langchain · error · TypeError
Model must be a Pydantic model.
Error message
Model must be a Pydantic model.
What it means
Error "Model must be a Pydantic model." thrown in langchain-ai/langchain.
Source
Thrown at libs/core/langchain_core/utils/function_calling.py:190
If not provided, the description of the schema will be used.
rm_titles: Whether to remove titles from the schema.
Raises:
TypeError: If the model is not a Pydantic model.
TypeError: If the model contains types that cannot be converted to JSON schema.
Returns:
The function description.
"""
try:
if hasattr(model, "model_json_schema"):
schema = model.model_json_schema() # Pydantic 2
elif hasattr(model, "schema"):
schema = model.schema() # Pydantic 1
else:
msg = "Model must be a Pydantic model."
raise TypeError(msg)
except PydanticInvalidForJsonSchema as e:
model_name = getattr(model, "__name__", str(model))
msg = (
f"Failed to generate JSON schema for '{model_name}': {e}\n\n"
"Tool argument schemas must be JSON-serializable. If your schema includes "
"custom Python classes, consider:\n"
" 1. Converting them to Pydantic models with JSON-compatible fields\n"
" 2. Using primitive types (str, int, float, bool, list, dict) instead\n"
" 3. Passing the data as serialized JSON strings\n\n"
)
raise PydanticInvalidForJsonSchema(msg) from e
return _convert_json_schema_to_openai_function(
schema, name=name, description=description, rm_titles=rm_titles
)
def _get_python_function_name(function: Callable[..., Any]) -> str:
"""Get the name of a Python function."""View on GitHub (pinned to e32fa9a52e)
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.
Example fix
from pydantic import BaseModel
class MySchema(BaseModel):
query: str
tool = convert_to_openai_function(MySchema) When it happens
Trigger: 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.
Common situations: See trigger scenarios.
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/6665c6930c84f34f.
Report an issue: GitHub.