langchain-ai/langchain · error · PydanticInvalidForJsonSchema

Failed to generate JSON schema for '{model_name}': {e} Tool

Error message

Failed to generate JSON schema for '{model_name}': {e}

Tool argument schemas must be JSON-serializable. If your schema includes custom Python classes, consider:
  1. Converting them to Pydantic models with JSON-compatible fields
  2. Using primitive types (str, int, float, bool, list, dict) instead
  3. Passing the data as serialized JSON strings

What it means

Error "Failed to generate JSON schema for '{model_name}': {e} Tool argument schemas must be JSON-serializable. If your schema includes custom Python classes, consider: 1. Converting them to Pydantic models with JSON-compatible fields 2. Using primitive types (str, int, float, bool, list, dict) instead 3. Passing the data as serialized JSON strings " thrown in langchain-ai/langchain.

Source

Thrown at libs/core/langchain_core/utils/function_calling.py:201

    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."""
    return function.__name__


def _convert_python_function_to_openai_function(
    function: Callable[..., Any],
) -> FunctionDescription:
    """Convert a Python function to an OpenAI function-calling API compatible dict.

    Assumes the Python function has type hints and a docstring with a description. If
    the docstring has Google Python style argument descriptions, these will be included
    as well.

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Convert custom Python classes in the schema to Pydantic models whose fields use JSON-compatible types.
  2. Replace non-serializable types (e.g. datetime, Decimal, custom classes) with primitive types such as str, int, float, bool, list, or dict.
  3. Pass complex data as serialized JSON strings instead of custom objects in the schema.

Example fix

class Args(BaseModel):
    created_at: str  # was datetime; serialize as ISO string instead

When it happens

Trigger: Raised when model_json_schema()/schema() fails for a tool argument schema, typically because the Pydantic model contains custom Python types that are not JSON-serializable.

Common situations: See trigger scenarios.


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/599053c2b1becd36. Report an issue: GitHub.