Skip to content

fix(langgraph/python): import ag_ui_langgraph without FastAPI installed (#2067)#2182

Open
dkedar7 wants to merge 1 commit into
ag-ui-protocol:mainfrom
dkedar7:fix/2067-fastapi-optional-import
Open

fix(langgraph/python): import ag_ui_langgraph without FastAPI installed (#2067)#2182
dkedar7 wants to merge 1 commit into
ag-ui-protocol:mainfrom
dkedar7:fix/2067-fastapi-optional-import

Conversation

@dkedar7

@dkedar7 dkedar7 commented Jul 14, 2026

Copy link
Copy Markdown

Fixes #2067.

Problem

FastAPI is declared an optional dependency of ag-ui-langgraph (only the HTTP endpoint helper in endpoint.py uses it), but ag_ui_langgraph/__init__.py eagerly imports .endpoint. So a purely in-process consumer — one that just iterates LangGraphAgent.run() to render AG-UI events locally (a terminal or Jupyter renderer, say) — can't import the package without the web stack:

pip install ag-ui-langgraph langgraph      # note: NOT the [fastapi] extra
python -c "from ag_ui_langgraph import LangGraphAgent"
# ModuleNotFoundError: No module named 'fastapi'
#   File ".../ag_ui_langgraph/__init__.py", line 20, in <module>
#     from .endpoint import add_langgraph_fastapi_endpoint

endpoint.py is the only module importing fastapi/starlette; the adapter itself (agent.py, types.py, utils.py, a2ui_tool.py, middlewares/) is already FastAPI-free.

Fix

Guard the optional endpoint import:

try:
    from .endpoint import add_langgraph_fastapi_endpoint
except ImportError:
    # FastAPI is an optional dependency — only the HTTP endpoint helper needs it.
    add_langgraph_fastapi_endpoint = None

add_langgraph_fastapi_endpoint stays in __all__ and is simply None until FastAPI is present, so the served path is unchanged.

Tests

Adds tests/test_import_without_fastapi.py — a regression test that blocks fastapi/starlette in a subprocess and asserts LangGraphAgent imports and add_langgraph_fastapi_endpoint is None. Verified the existing test_deprecation_warnings.py and test_endpoint_health_path.py still pass (the guard doesn't touch the served path), and the full Python suite passes (380 tests).

Context: I filed #2067 two weeks ago with this diagnosis and offered to PR; sending it now since the change is small and ready. Happy to adjust to whatever you prefer.

FastAPI is declared an optional dependency (only the HTTP endpoint helper in
endpoint.py uses it), but __init__.py eagerly imported `.endpoint`, so
`import ag_ui_langgraph` / `from ag_ui_langgraph import LangGraphAgent` raised
ModuleNotFoundError: No module named 'fastapi' when FastAPI wasn't installed --
breaking every purely in-process consumer that just iterates
LangGraphAgent.run() to render AG-UI events locally (a terminal or Jupyter
renderer, for example).

Guard the optional endpoint import: add_langgraph_fastapi_endpoint stays in
__all__ and is simply None until FastAPI is present, so the served path is
unchanged. endpoint.py is the only module importing fastapi/starlette; the
adapter (agent/types/utils/a2ui_tool/middlewares) is already FastAPI-free.

Adds a regression test that blocks fastapi/starlette in a subprocess and asserts
LangGraphAgent imports and add_langgraph_fastapi_endpoint is None.

Fixes ag-ui-protocol#2067

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dkedar7
dkedar7 requested a review from a team as a code owner July 14, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ag_ui_langgraph: import requires FastAPI even for in-process (non-server) use

1 participant