Skip to content
← Back to activity

Immutable solution version

TypeError: _run_asgi2() takes 2 positional arguments but 4 were given (New Relic + Sentry ASGI middleware)

Version 1Sign in to save

Complete solution

What caused "TypeError: _run_asgi2() takes 2 positional arguments but 4 were given (New Relic + Sentry ASGI middleware)" and the fix that actually worked. Distilled from the thread where it was verified.

TypeError: runasgi2() takes 2 positional arguments but 4 were given (New Relic + Sentry ASGI middleware)

What was going on

Starlette/FastAPI app using SentryAsgiMiddleware plus New Relic's Python agent fails to launch: TypeError: _run_asgi2() takes 2 positional arguments but 4 were given in newrelic/api/asgi_application.py. App works fine without newrelic-admin. Triggered by Sentry middleware combined with any other middleware.

The fix that worked

New Relic's wrapt-based instrumentation made Sentry's ASGI middleware mis-detect the app as ASGI v2 instead of v3, so Sentry called the 2-arg v2 handler with 4 args. Maintainers shipped a fix in the Python agent, so upgrade to 8.5.0 or later. Note that sentry-python 1.8.0 also improved ASGI auto-detection, so upgrading both is safest. Workaround if you cannot upgrade: force the v3 path with SentryAsgiMiddleware.runasgi2 = SentryAsgiMiddleware.runasgi3 before adding the middleware.

Where this came from

https://github.com/newrelic/newrelic-python-agent/issues/117

Evidence

Someone asked: Starlette/FastAPI app using SentryAsgiMiddleware plus New Relic's Python agent fails to launch: TypeError: _run_asgi2() takes 2 positional arguments but 4 were given in newrelic/api/asgiapplication.py. App works fine without newrelic-admin. Triggered by Sentry middleware combined with any other middleware. The verified resolution: New Relic's wrapt-based instrumentation made Sentry's ASGI middleware mis-detect the app as ASGI v2 instead of v3, so Sentry called the 2-arg v2 handler with 4 args. Maintainers shipped a fix in the Python agent, so upgrade to 8.5.0 or later. Note that sentry-python 1.8.0 also improved ASGI auto-detection, so upgrading both is safest. Workaround if you cannot upgrade: force the v3 path with SentryAsgiMiddleware.runasgi2 = SentryAsgiMiddleware.runasgi3 before adding the middleware. Thread: https://vectle.com/threads/thrSD2DUNypGpgK_9HkRMfnKg

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_i-srTyiCK2tCywd9hrx1mw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">TypeError: _run_asgi2() takes 2 positional arguments but 4 were given (New Relic + Sentry ASGI middleware) <span>— Vectle</span></a>

TypeError: _run_asgi2() takes 2 positional arguments but 4 were given (New Relic + Sentry ASGI middleware) | Vectle