Skip to content

Use of importlib.metadata breaks import when metadata is stripped #15

Closed

Description

@JP-Dhabolt

Expected Behavior: Importing the module works without bundling Package Metadata in application
Actual Behavior: Stripping Package Metadata breaks imports

Cause:
The pattern of directly using importlib.metadata to set the version assumes that package metadata is included in the location where the package is being used. As a result, this breaks when using slimmed down deployment artifacts, as package metadata is not bundled. I have seen a standard approach to this to be to wrap this self-referential version within a try/except block, e.g.

try:
    __version__ = version("annotated-doc")
except Exception:
    __version__ = "unknown"

https://github.com/fastapi/annotated-doc/blob/7dd21fb75ea6882caf79725881690551ab28c5ff/src/annotated_doc/__init__.py#L5C1-L5C39

Activity

  1. DBS-ST-VIT commented on Nov 4, 2025

    @DBS-ST-VIT

    We are also facing this issue. Especially, we are using FastAPI in AWS Lambda and since FastAPI is depending on this package, our application isnt starting anymore as of this Import Error.

    @tiangolo May you can have a look on this?

  2. raghavankarthick commented on Nov 5, 2025

    @raghavankarthick

    Hi @tiangolo, We are also facing this issue with FastAPI in AWS Lambda, can you take a look on this?

  3. JP-Dhabolt commented on Nov 5, 2025

    @JP-Dhabolt
    Author

    There is a workaround available, which is to modify your Lambda Zip artifact generation to include the **/*.dist-info* glob pattern. This will slightly increase the artifact size, but does temporarily resolve the issue.

  4. svlandeg commented on Nov 10, 2025

    @svlandeg
    Member

    Thanks all for the report! We'll look into how best to resolve this 🙏

  5. added theissue type on Nov 10, 2025
  6. tiangolo commented on Nov 10, 2025

    @tiangolo
    Member

    Thanks for the report! This should be solved by #19, available in version 0.0.4, released in the next few minutes. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions