langchain-ai/langchain · error · ValueError

Function must have either a docstring or description when in

Error message

Function must have either a docstring or description when infer_schema is False.

What it means

With infer_schema=False the @tool decorator cannot derive a description from a schema, so the target function's docstring (or an explicit description argument) is mandatory — a tool with no description is unusable for models. This check enforces that requirement at creation time.

Source

Thrown at libs/core/langchain_core/tools/convert.py:338

                    name=tool_name,
                    description=tool_description,
                    return_direct=return_direct,
                    args_schema=schema,
                    infer_schema=infer_schema,
                    response_format=response_format,
                    parse_docstring=parse_docstring,
                    error_on_invalid_docstring=error_on_invalid_docstring,
                    extras=extras,
                )
            # If someone doesn't want a schema applied, we must treat it as
            # a simple string->string function
            tool_description = tool_description or dec_func.__doc__
            if tool_description is None:
                msg = (
                    "Function must have either a docstring or description "
                    "when infer_schema is False."
                )
                raise ValueError(msg)
            return Tool(
                name=tool_name,
                func=func,
                description=tool_description,
                return_direct=return_direct,
                coroutine=coroutine,
                response_format=response_format,
                extras=extras,
            )

        return _tool_factory

    if len(args) != 0:
        # Triggered if a user attempts to use positional arguments that
        # do not exist in the function signature
        # e.g., @tool("name", runnable, "extra_arg")
        # Here, "extra_arg" is not a valid argument
        msg = "Too many arguments for tool decorator. A decorator "

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Add a docstring to the decorated function
  2. Or pass description explicitly: @tool(infer_schema=False, description='Does X')
  3. Re-enable infer_schema if schema-derived description was intended

Example fix

# before
@tool(infer_schema=False)
def lookup(id: str) -> str:
    return db.get(id)

# after
@tool(infer_schema=False, description="Look up a record by id.")
def lookup(id: str) -> str:
    return db.get(id)
Defensive patterns

Strategy: validation

Validate before calling

def make_tool(func, *, description: str | None = None, infer_schema: bool = False):
    description = description or (func.__doc__ or "").strip()
    if not description:
        msg = f"Tool {getattr(func, '__name__', func)} needs a description or docstring"
        raise ValueError(msg)
    return tool(func, description=description, infer_schema=infer_schema)

Try / catch

try:
    t = tool(func, infer_schema=False)
except ValueError as e:
    if "docstring or description" in str(e):
        t = tool(func, description="TODO: describe this tool", infer_schema=False)
    else:
        raise

Prevention

When it happens

Trigger: @tool(infer_schema=False) applied to a function with no docstring and no description=... passed to the decorator; same when calling tool(func, infer_schema=False).

Common situations: Quickly stubbing a tool function without a docstring; teams disabling schema inference for tight control over schemas but forgetting the description; copy-pasted functions stripped of docstrings by a linter.

Related errors


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