OpenBB-finance/OpenBB · error · TypeError

Error: The {name} instance in '{app_path}' is not an instanc

Error message

Error: The {name} instance in '{app_path}' is not an instance of FastAPI

What it means

Final type check in import_app: after retrieving (or calling) the named attribute, the resulting object is not an instance of fastapi.FastAPI, so it cannot be served or have CORS middleware attached.

Source

Thrown at openbb_platform/extensions/platform_api/openbb_platform_api/utils/api.py:301

    # Here we use the same approach as uvicorn to handle factory functions.
    # This prevents us from relying on explicit type annotations.
    # See: https://github.com/encode/uvicorn/blob/master/uvicorn/config.py
    try:
        app = app_or_factory()
        if not factory:
            print(  # noqa: T201
                "\n\n[WARNING]   "
                "App factory detected. Using it, but please consider setting the --factory flag explicitly.\n"
            )
    except TypeError:
        if factory:
            raise TypeError(  # pylint: disable=raise-missing-from
                f"Error: The {name} instance in '{app_path}' appears not to be a callable factory function"
            )
        app = app_or_factory

    if not isinstance(app, FastAPI):
        raise TypeError(
            f"Error: The {name} instance in '{app_path}' is not an instance of FastAPI"
        )

    app.add_middleware(
        CORSMiddleware,
        allow_origins=system.api_settings.cors.allow_origins,
        allow_methods=system.api_settings.cors.allow_methods,
        allow_headers=system.api_settings.cors.allow_headers,
    )

    AppLoader.add_exception_handlers(app)

    return app


def parse_args():  # noqa: PLR0912  # pylint: disable=too-many-branches
    """Parse the launch script command line arguments."""
    args = sys.argv[1:].copy()

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Ensure the named attribute is the FastAPI(...) instance itself.
  2. If using a router, wrap it: app = FastAPI(); app.include_router(router).
  3. If the factory returns extra objects, change it to return only the FastAPI app.
  4. Verify with isinstance(module.app, FastAPI) in a quick REPL check.

Example fix

# before
# main.py
router = APIRouter()

# after
# main.py
app = FastAPI()
app.include_router(router)
Defensive patterns

Strategy: type-guard

Validate before calling

from fastapi import FastAPI
obj = getattr(module, name)
assert isinstance(obj, FastAPI), f"expected FastAPI instance, got {type(obj).__name__}"

Type guard

from fastapi import FastAPI
def is_fastapi_app(obj) -> bool:
    return isinstance(obj, FastAPI)

Try / catch

try:
    app = import_app(path, name)
except TypeError as e:
    if "not an instance of FastAPI" in str(e):
        raise SystemExit("expose the FastAPI(...) instance as the named attribute") from e
    raise

Prevention

When it happens

Trigger: The module attribute named 'app' holds a Starlette/Flask/Django app, an APIRouter, a string, or any non-FastAPI object; or a factory returned something other than a FastAPI instance.

Common situations: Migrating a Starlette app and expecting the OpenBB loader to serve it; exposing an APIRouter under the name 'app'; a factory returning a tuple (app, lifespan) instead of the app.

Related errors


AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14). Data as JSON: /api/errors/305e3fa190724bd9. Report an issue: GitHub.