{"record":{"id":"a8e023b3a99316fd","repo":"PrefectHQ/fastmcp","slug":"you-must-provide-a-name-for-lambda-functions-a8e023","errorCode":null,"errorMessage":"You must provide a name for lambda functions","messagePattern":"You must provide a name for lambda functions","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/resources/template.py","lineNumber":449,"sourceCode":"        fn: Callable[..., Any],\n        uri_template: str,\n        name: str | None = None,\n        version: str | int | None = None,\n        title: str | None = None,\n        description: str | None = None,\n        icons: list[Icon] | None = None,\n        mime_type: str | None = None,\n        tags: set[str] | None = None,\n        annotations: Annotations | None = None,\n        meta: dict[str, Any] | None = None,\n        auth: AuthCheck | list[AuthCheck] | None = None,\n        security: ResourceSecurity | None | InheritSecurity = INHERIT_SECURITY,\n    ) -> FunctionResourceTemplate:\n        \"\"\"Create a template from a function.\"\"\"\n\n        func_name = name or getattr(fn, \"__name__\", None) or fn.__class__.__name__\n        if func_name == \"<lambda>\":\n            raise ValueError(\"You must provide a name for lambda functions\")\n\n        # Reject functions with *args\n        # (**kwargs is allowed because the URI will define the parameter names)\n        sig = inspect.signature(fn)\n        for param in sig.parameters.values():\n            if param.kind == inspect.Parameter.VAR_POSITIONAL:\n                raise ValueError(\n                    \"Functions with *args are not supported as resource templates\"\n                )\n\n        # Extract path and query parameters from URI template.\n        # Allow hyphens in names and normalize to underscores so they\n        # match Python function parameter names.\n        raw_path_params = set(re.findall(r\"{([\\w-]+)(?:\\*)?}\", uri_template))\n        raw_query_params = extract_query_params(uri_template)\n\n        # Detect collisions: two raw param names that normalize to the\n        # same Python identifier (e.g. {user-id} and {user_id}).","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/resources/template.py#L431-L467","documentation":"FastMCP's ResourceTemplate.from_function requires a name for every template. When no explicit name is passed, the library falls back to fn.__name__ (or the class name for callables). Anonymous lambdas report their __name__ as '<lambda>', which is not a valid, stable identifier, so the constructor raises immediately to prevent an unnamed/garbage-named component from being registered.","triggerScenarios":"Calling FunctionResourceTemplate.from_function(fn=uri_template, ...) (or FastMCP.add_template / resource_template) with a lambda as the function and no explicit name= argument.","commonSituations":"Registering a quick inline lambda template during prototyping; programmatically generating templates in a loop with lambdas and forgetting the name kwarg; converting a plain Resource from_function to a template while keeping lambda style.","solutions":["Pass an explicit name: FunctionResourceTemplate.from_function(lambda x: ..., uri_template=\"data://{x}\", name=\"my-template\")","Replace the lambda with a named def function so the fallback name works","Wrap the lambda with functools.partial-free named wrapper and use its __name__"],"exampleFix":"// before\nFunctionResourceTemplate.from_function(lambda i: fetch(i), uri_template=\"data://{i}\")\n// after\ndef get_data(i: int) -> str: ...\nFunctionResourceTemplate.from_function(get_data, uri_template=\"data://{i}\")\n// or\nFunctionResourceTemplate.from_function(lambda i: fetch(i), uri_template=\"data://{i}\", name=\"get-data\")","handlingStrategy":"validation","validationCode":"def ensure_template_name(fn, name=None):\n    if name is None and (getattr(fn, \"__name__\", None) in (None, \"<lambda>\")):\n        raise ValueError(\"lambda passed without an explicit name=\")","typeGuard":"def has_name(fn) -> bool:\n    n = getattr(fn, \"__name__\", None)\n    return isinstance(n, str) and n != \"<lambda>\"","tryCatchPattern":"try:\n    tpl = FunctionResourceTemplate.from_function(fn, uri_template=uri)\nexcept ValueError as e:\n    if \"lambda\" in str(e):\n        tpl = FunctionResourceTemplate.from_function(fn, uri_template=uri, name=fallback_name)\n    else:\n        raise","preventionTips":["Always pass name= explicitly when registering lambdas","Prefer named def functions for all production templates","Add a startup test that registers all templates to catch this early"],"tags":["python","resources","naming"],"backgroundTag":"missing-required-name","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}