xtekky/gpt4free · error · ImportError
Import of '{name}' is not allowed inside a .pa.py sandbox fo
Error message
Import of '{name}' is not allowed inside a .pa.py sandbox for security reasons. What it means
Raised when a .pa.py sandbox imports a g4f submodule listed in _BLOCKED_SUBMODULES — g4f.tools.auth, g4f.tools.run_tools, g4f.config, g4f.cookies, g4f.providers.retry_provider, g4f.providers.config_provider, g4f.Provider, and similar credential/config-bearing modules. Even though 'g4f' itself is allowed, these submodules expose API keys, cookie stores and auth helpers, so the exact name is rejected.
Source
Thrown at g4f/mcp/pa_provider.py:495
if obj is None:
raise ImportError(
f"Cannot find submodule '{name}' in workspace module '{base}'."
)
return obj
return ws_module
raise ImportError(
f"Import of '{name}' is not allowed in safe execution mode.\n"
f"Allowed top-level modules: {', '.join(sorted(allowed))}"
)
# Explicit allowlist takes priority over the blocklist below.
# This permits e.g. "g4f.Provider.helper" even though "g4f.Provider"
# is blocked.
for allowed_sub in _ALLOWED_G4F_SUBPATHS:
if name == allowed_sub or name.startswith(allowed_sub + "."):
return original(name, globals, locals, fromlist, level)
# Block sensitive g4f submodules even though g4f itself is allowed.
if name in _BLOCKED_SUBMODULES:
raise ImportError(
f"Import of '{name}' is not allowed inside a .pa.py sandbox "
f"for security reasons."
)
# Also block when a blocked submodule is the parent of a deeper import
# (e.g. "g4f.tools.auth.something", "g4f.Provider.OpenAI").
for blocked in _BLOCKED_SUBMODULES:
if name.startswith(blocked + "."):
raise ImportError(
f"Import of '{name}' is not allowed inside a .pa.py sandbox "
f"for security reasons."
)
return original(name, globals, locals, fromlist, level)
return _restricted_import
def _make_safe_globals(
allowed: FrozenSet[str] = SAFE_MODULES,View on GitHub (pinned to 973504e177)
Solutions
- Import only the explicitly allowed subpaths (e.g. g4f.Provider.helper) — check _ALLOWED_G4F_SUBPATHS in pa_provider.py
- Pass needed credentials as explicit parameters to your provider instead of reading g4f.config/cookies
- Copy the small helper logic you need into the .pa.py file itself or a sibling workspace module
- Never attempt to reach auth/cookie tooling from sandboxed code — the block is deliberate
Example fix
# before import g4f.Provider # after import g4f.Provider.helper # explicitly allowlisted subpath
Defensive patterns
Strategy: try-catch
Validate before calling
from g4f.mcp.pa_provider import _ALLOWED_G4F_SUBPATHS if False else None
# simpler: static check before load
BLOCKED = {"g4f.tools.auth", "g4f.tools.run_tools", "g4f.config", "g4f.cookies",
"g4f.providers.retry_provider", "g4f.providers.config_provider", "g4f.Provider"}
def is_blocked(name: str) -> bool:
return name in BLOCKED or any(name.startswith(b + ".") for b in BLOCKED) Try / catch
try:
import g4f.config
except ImportError as e:
if "sandbox" in str(e):
# credentials must arrive as provider parameters instead
... Prevention
- Never import credential/config/cookie modules from sandboxed code
- Pass API keys explicitly as provider parameters
- Pre-check imports against the blocked list before loading a .pa.py file
When it happens
Trigger: 'import g4f.config', 'import g4f.cookies', 'import g4f.tools.auth', 'import g4f.Provider' (exact match on a blocked name) inside sandboxed provider code. Note _ALLOWED_G4F_SUBPATHS is checked first, so explicitly permitted paths like g4f.Provider.helper still work.
Common situations: Trying to read the host's stored g4f API key or cookies from a custom provider; importing the Provider package to reuse another provider's payload builder; code written before the blocklist was introduced that worked on older g4f versions.
Related errors
- '{name}' is not available in the restricted os shim.
- Import of '{name}' is not allowed in safe execution mode.\nA
- Cannot find submodule '{name}' in workspace module '{base}'.
- File access outside workspace is denied: '{file}'. Workspace
- TokenError.FILE_ACCESS_ERROR
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/26aa84ea749ab7c2.
Report an issue: GitHub.