{"record":{"id":"30eaf12d37eaf8bd","repo":"PrefectHQ/fastmcp","slug":"you-must-provide-a-name-for-lambda-functions-30eaf1","errorCode":null,"errorMessage":"You must provide a name for lambda functions","messagePattern":"You must provide a name for lambda functions","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/tools/function_tool.py","lineNumber":301,"sourceCode":"                version=version,\n                title=title,\n                description=description,\n                icons=icons,\n                tags=tags,\n                output_schema=output_schema,\n                annotations=annotations,\n                meta=meta,\n                task=task,\n                timeout=timeout,\n                auth=auth,\n                run_in_thread=True if run_in_thread is None else run_in_thread,\n            )\n\n        parsed_fn = ParsedFunction.from_function(fn)\n        func_name = metadata.name or parsed_fn.name\n\n        if func_name == \"<lambda>\":\n            raise ValueError(\"You must provide a name for lambda functions\")\n\n        # Inline sync execution has no cancellation checkpoints, so\n        # anyio.fail_after cannot preempt the call — the timeout would be\n        # silently ignored. Reject the combination so users make an\n        # explicit choice. Async generators are async even though\n        # is_coroutine_function returns False for them; the generator's\n        # iteration has checkpoints, so timeout enforcement still works.\n        if (\n            metadata.timeout is not None\n            and not metadata.run_in_thread\n            and not is_coroutine_function(fn)\n            and not inspect.isasyncgenfunction(fn)\n        ):\n            raise ValueError(\n                f\"Tool {func_name!r}: timeout cannot be enforced when \"\n                \"run_in_thread=False on a sync function. Inline execution has \"\n                \"no cancellation checkpoints, so the timeout would be a no-op. \"\n                \"Either drop the timeout or remove run_in_thread=False and \"","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/tools/function_tool.py#L283-L319","documentation":"A lambda function's __name__ is always '<lambda>', which is not a valid tool identifier, so from_function() raises ValueError unless an explicit name (via parameter or metadata) is supplied. The library refuses to guess a usable name for anonymous functions.","triggerScenarios":"FunctionTool.from_function(lambda x: x * 2) with no name= argument and metadata=None or metadata.name=None.","commonSituations":"Quickly registering a small transformation as a tool inline; code generators or decorators wrapping lambdas without naming them; refactoring a named helper into a lambda.","solutions":["Pass an explicit name: from_function(fn, name='double')","Set name in the ToolMeta object passed as metadata","Convert the lambda to a named def function"],"exampleFix":"// before\nFunctionTool.from_function(lambda x: x * 2)\n// after\nFunctionTool.from_function(lambda x: x * 2, name=\"double\")","handlingStrategy":"validation","validationCode":"def ensure_named(fn, name=None, metadata=None):\n    resolved = name or (getattr(metadata, 'name', None) if metadata else None) or getattr(fn, '__name__', '')\n    if resolved in ('', '<lambda>'):\n        raise ValueError('Lambda requires an explicit tool name')\n    return resolved","typeGuard":"def is_lambda(fn) -> bool:\n    return callable(fn) and getattr(fn, '__name__', '') == '<lambda>'","tryCatchPattern":"try:\n    tool = FunctionTool.from_function(fn)\nexcept ValueError as e:\n    if 'name for lambda' in str(e):\n        tool = FunctionTool.from_function(fn, name=default_name_for(fn))","preventionTips":["Never register bare lambdas; always wrap in a named def","Lint rule: flag FunctionTool.from_function calls whose fn is a lambda without name=","Store tool names alongside functions in registries"],"tags":["python","lambda","naming","tool-definition"],"backgroundTag":"missing-required-name","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}