Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): allow request_scope import without fastapi (#2562)

AxelRay4bd121493da3
diff --git a/headroom/proxy/request_scope.py b/headroom/proxy/request_scope.py
index 4a7e14dc9..dc8e9e1db 100644
--- a/headroom/proxy/request_scope.py
+++ b/headroom/proxy/request_scope.py
@@ -3,10 +3,11 @@
from __future__ import annotations
from collections.abc import MutableMapping
-from typing import Any
+from typing import TYPE_CHECKING, Any
from urllib.parse import quote
-from fastapi import Request
+if TYPE_CHECKING:
+ from fastapi import Request
def normalize_scope_path(scope: MutableMapping[str, Any], path: str) -> None:
diff --git a/tests/test_request_scope_no_fastapi.py b/tests/test_request_scope_no_fastapi.py
new file mode 100644
index 000000000..12b337d7f
--- /dev/null
+++ b/tests/test_request_scope_no_fastapi.py
@@ -0,0 +1,42 @@
+"""Regression tests for importing request-scope helpers without FastAPI."""
+
+import subprocess
+import sys
+import textwrap
+
+
+def test_request_scope_and_project_context_import_without_fastapi() -> None:
+ script = textwrap.dedent(
+ """
+ import builtins
+ import sys
+
+ real_import = builtins.__import__
+
+ def import_without_fastapi(name, *args, **kwargs):
+ if name == "fastapi" or name.startswith("fastapi."):
+ raise ModuleNotFoundError("No module named 'fastapi'")
+ return real_import(name, *args, **kwargs)
+
+ builtins.__import__ = import_without_fastapi
+
+ import headroom.proxy.request_scope as request_scope
+ import headroom.proxy.project_context
+
+ assert not any(
+ name == "fastapi" or name.startswith("fastapi.") for name in sys.modules
+ )
+ scope = {"path": "/a", "raw_path": b"/a"}
+ request_scope.normalize_scope_path(scope, "/b c")
+ assert scope == {"path": "/b c", "raw_path": b"/b%20c"}
+ """
+ )
+
+ result = subprocess.run(
+ [sys.executable, "-c", script],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+
+ assert result.returncode == 0, result.stderr