Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend): add prisma types stub generator for pyright compatibility (#11736)

Swifty843c487500b4
diff --git a/.dockerignore b/.dockerignore
index 94bf1742f1..c9524ce700 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -16,6 +16,7 @@
!autogpt_platform/backend/poetry.lock
!autogpt_platform/backend/README.md
!autogpt_platform/backend/.env
+!autogpt_platform/backend/gen_prisma_types_stub.py
# Platform - Market
!autogpt_platform/market/market/
diff --git a/.github/workflows/claude-dependabot.yml b/.github/workflows/claude-dependabot.yml
index 20b6f1d28e..1fd0da3d8e 100644
--- a/.github/workflows/claude-dependabot.yml
+++ b/.github/workflows/claude-dependabot.yml
@@ -74,7 +74,7 @@ jobs:
- name: Generate Prisma Client
working-directory: autogpt_platform/backend
- run: poetry run prisma generate
+ run: poetry run prisma generate && poetry run gen-prisma-stub
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
- name: Set up Node.js
diff --git a/.github/workflows/claude.yml b/.github/workflows/claude.yml
index 3f5e8c22ec..71c6ef49c2 100644
--- a/.github/workflows/claude.yml
+++ b/.github/workflows/claude.yml
@@ -90,7 +90,7 @@ jobs:
- name: Generate Prisma Client
working-directory: autogpt_platform/backend
- run: poetry run prisma generate
+ run: poetry run prisma generate && poetry run gen-prisma-stub
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
- name: Set up Node.js
diff --git a/.github/workflows/copilot-setup-steps.yml b/.github/workflows/copilot-setup-steps.yml
index 13ef01cc44..aac8befee0 100644
--- a/.github/workflows/copilot-setup-steps.yml
+++ b/.github/workflows/copilot-setup-steps.yml
@@ -72,7 +72,7 @@ jobs:
- name: Generate Prisma Client
working-directory: autogpt_platform/backend
- run: poetry run prisma generate
+ run: poetry run prisma generate && poetry run gen-prisma-stub
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
- name: Set up Node.js
@@ -108,6 +108,16 @@ jobs:
# run: pnpm playwright install --with-deps chromium
# Docker setup for development environment
+ - name: Free up disk space
+ run: |
+ # Remove large unused tools to free disk space for Docker builds
+ sudo rm -rf /usr/share/dotnet
+ sudo rm -rf /usr/local/lib/android
+ sudo rm -rf /opt/ghc
+ sudo rm -rf /opt/hostedtoolcache/CodeQL
+ sudo docker system prune -af
+ df -h
+
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
diff --git a/.github/workflows/platform-backend-ci.yml b/.github/workflows/platform-backend-ci.yml
index f962382fa5..da5ab83c1c 100644
--- a/.github/workflows/platform-backend-ci.yml
+++ b/.github/workflows/platform-backend-ci.yml
@@ -134,7 +134,7 @@ jobs:
run: poetry install
- name: Generate Prisma Client
- run: poetry run prisma generate
+ run: poetry run prisma generate && poetry run gen-prisma-stub
- id: supabase
name: Start Supabase
diff --git a/autogpt_platform/Makefile b/autogpt_platform/Makefile
index d99fee49d7..2ff454e392 100644
--- a/autogpt_platform/Makefile
+++ b/autogpt_platform/Makefile
@@ -12,6 +12,7 @@ reset-db:
rm -rf db/docker/volumes/db/data
cd backend && poetry run prisma migrate deploy
cd backend && poetry run prisma generate
+ cd backend && poetry run gen-prisma-stub
# View logs for core services
logs-core:
@@ -33,6 +34,7 @@ init-env:
migrate:
cd backend && poetry run prisma migrate deploy
cd backend && poetry run prisma generate
+ cd backend && poetry run gen-prisma-stub
run-backend:
cd backend && poetry run app
diff --git a/autogpt_platform/backend/Dockerfile b/autogpt_platform/backend/Dockerfile
index 7f51bad3a1..b3389d1787 100644
--- a/autogpt_platform/backend/Dockerfile
+++ b/autogpt_platform/backend/Dockerfile
@@ -48,7 +48,8 @@ RUN poetry install --no-ansi --no-root
# Generate Prisma client
COPY autogpt_platform/backend/schema.prisma ./
COPY autogpt_platform/backend/backend/data/partial_types.py ./backend/data/partial_types.py
-RUN poetry run prisma generate
+COPY autogpt_platform/backend/gen_prisma_types_stub.py ./
+RUN poetry run prisma generate && poetry run gen-prisma-stub
FROM debian:13-slim AS server_dependencies
diff --git a/autogpt_platform/backend/backend/api/features/store/db.py b/autogpt_platform/backend/backend/api/features/store/db.py
index 18db6f43ce..8e4310ee02 100644
--- a/autogpt_platform/backend/backend/api/features/store/db.py
+++ b/autogpt_platform/backend/backend/api/features/store/db.py
@@ -1851,9 +1851,7 @@ async def get_admin_listings_with_versions(
where = prisma.types.StoreListingWhereInput(**where_dict)
include = prisma.types.StoreListingInclude(
Versions=prisma.types.FindManyStoreListingVersionArgsFromStoreListing(
- order_by=prisma.types._StoreListingVersion_version_OrderByInput(
- version="desc"
- )
+ order_by={"version": "desc"}
),
OwningUser=True,
)
diff --git a/autogpt_platform/backend/backend/blocks/airtable/_webhook.py b/autogpt_platform/backend/backend/blocks/airtable/_webhook.py
index 58e6f95d0c..452630953e 100644
--- a/autogpt_platform/backend/backend/blocks/airtable/_webhook.py
+++ b/autogpt_platform/backend/backend/blocks/airtable/_webhook.py
@@ -6,6 +6,9 @@ import hashlib
import hmac
import logging
from enum import Enum
+from typing import cast
+
+from prisma.types import Serializable
from backend.sdk import (
BaseWebhooksManager,
@@ -84,7 +87,9 @@ class AirtableWebhookManager(BaseWebhooksManager):
# update webhook config
await update_webhook(
webhook.id,
- config={"base_id": base_id, "cursor": response.cursor},
+ config=cast(
+ dict[str, Serializable], {"base_id": base_id, "cursor": response.cursor}
+ ),
)
event_type = "notification"
diff --git a/autogpt_platform/backend/gen_prisma_types_stub.py b/autogpt_platform/backend/gen_prisma_types_stub.py
new file mode 100644
index 0000000000..3f6073b2ff
--- /dev/null
+++ b/autogpt_platform/backend/gen_prisma_types_stub.py
@@ -0,0 +1,227 @@
+#!/usr/bin/env python3
+"""
+Generate a lightweight stub for prisma/types.py that collapses all exported
+symbols to Any. This prevents Pyright from spending time/budget on Prisma's
+query DSL types while keeping runtime behavior unchanged.
+
+Usage:
+ poetry run gen-prisma-stub
+
+This script automatically finds the prisma package location and generates
+the types.pyi stub file in the same directory as types.py.
+"""
+
+from __future__ import annotations
+
+import ast
+import importlib.util
+import sys
+from pathlib import Path
+from typing import Iterable, Set
+
+
+def _iter_assigned_names(target: ast.expr) -> Iterable[str]:
+ """Extract names from assignment targets (handles tuple unpacking)."""
+ if isinstance(target, ast.Name):
+ yield target.id
+ elif isinstance(target, (ast.Tuple, ast.List)):
+ for elt in target.elts:
+ yield from _iter_assigned_names(elt)
+
+
+def _is_private(name: str) -> bool:
+ """Check if a name is private (starts with _ but not __)."""
+ return name.startswith("_") and not name.startswith("__")
+
+
+def _is_safe_type_alias(node: ast.Assign) -> bool:
+ """Check if an assignment is a safe type alias that shouldn't be stubbed.
+
+ Safe types are:
+ - Literal types (don't cause type budget issues)
+ - Simple type references (SortMode, SortOrder, etc.)
+ - TypeVar definitions
+ """
+ if not node.value:
+ return False
+
+ # Check if it's a Subscript (like Literal[...], Union[...], TypeVar[...])
+ if isinstance(node.value, ast.Subscript):
+ # Get the base type name
+ if isinstance(node.value.value, ast.Name):
+ base_name = node.value.value.id
+ # Literal types are safe
+ if base_name == "Literal":
+ return True
+ # TypeVar is safe
+ if base_name == "TypeVar":
+ return True
+ elif isinstance(node.value.value, ast.Attribute):
+ # Handle typing_extensions.Literal etc.
+ if node.value.value.attr == "Literal":
+ return True
+
+ # Check if it's a simple Name reference (like SortMode = _types.SortMode)
+ if isinstance(node.value, ast.Attribute):
+ return True
+
+ # Check if it's a Call (like TypeVar(...))
+ if isinstance(node.value, ast.Call):
+ if isinstance(node.value.func, ast.Name):
+ if node.value.func.id == "TypeVar":
+ return True
+
+ return False
+
+
+def collect_top_level_symbols(
+ tree: ast.Module, source_lines: list[str]
+) -> tuple[Set[str], Set[str], list[str], Set[str]]:
+ """Collect all top-level symbols from an AST module.
+
+ Returns:
+ Tuple of (class_names, function_names, safe_variable_sources, unsafe_variable_names)
+ safe_variable_sources contains the actual source code lines for safe variables
+ """
+ classes: Set[str] = set()
+ functions: Set[str] = set()
+ safe_variable_sources: list[str] = []
+ unsafe_variables: Set[str] = set()
+
+ for node in tree.body:
+ if isinstance(node, ast.ClassDef):
+ if not _is_private(node.name):
+ classes.add(node.name)
+ elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
+ if not _is_private(node.name):
+ functions.add(node.name)
+ elif isinstance(node, ast.Assign):
+ is_safe = _is_safe_type_alias(node)
+ names = []
+ for t in node.targets:
+ for n in _iter_assigned_names(t):
+ if not _is_private(n):
+ names.append(n)
+ if names:
+ if is_safe:
+ # Extract the source code for this assignment
+ start_line = node.lineno - 1 # 0-indexed
+ end_line = node.end_lineno if node.end_lineno else node.lineno
+ source = "\n".join(source_lines[start_line:end_line])
+ safe_variable_sources.append(source)
+ else:
+ unsafe_variables.update(names)
+ elif isinstance(node, ast.AnnAssign) and node.target:
+ # Annotated assignments are always stubbed
+ for n in _iter_assigned_names(node.target):
+ if not _is_private(n):
+ unsafe_variables.add(n)
+
+ return classes, functions, safe_variable_sources, unsafe_variables
+
+
+def find_prisma_types_path() -> Path:
+ """Find the prisma types.py file in the installed package."""
+ spec = importlib.util.find_spec("prisma")
+ if spec is None or spec.origin is None:
+ raise RuntimeError("Could not find prisma package. Is it installed?")
+
+ prisma_dir = Path(spec.origin).parent
+ types_path = prisma_dir / "types.py"
+
+ if not types_path.exists():
+ raise RuntimeError(f"prisma/types.py not found at {types_path}")
+
+ return types_path
+
+
+def generate_stub(src_path: Path, stub_path: Path) -> int:
+ """Generate the .pyi stub file from the source types.py."""
+ code = src_path.read_text(encoding="utf-8", errors="ignore")
+ source_lines = code.splitlines()
+ tree = ast.parse(code, filename=str(src_path))
+ classes, functions, safe_variable_sources, unsafe_variables = (
+ collect_top_level_symbols(tree, source_lines)
+ )
+
+ header = """\
+# -*- coding: utf-8 -*-
+# Auto-generated stub file - DO NOT EDIT
+# Generated by gen_prisma_types_stub.py
+#
+# This stub intentionally collapses complex Prisma query DSL types to Any.
+# Prisma's generated types can explode Pyright's type inference budgets
+# on large schemas. We collapse them to Any so the rest of the codebase
+# can remain strongly typed while keeping runtime behavior unchanged.
+#
+# Safe types (Literal, TypeVar, simple references) are preserved from the
+# original types.py to maintain proper type checking where possible.
+
+from __future__ import annotations
+from typing import Any
+from typing_extensions import Literal
+
+# Re-export commonly used typing constructs that may be imported from this module
+from typing import TYPE_CHECKING, TypeVar, Generic, Union, Optional, List, Dict
+
+# Base type alias for stubbed Prisma types - allows any dict structure
+_PrismaDict = dict[str, Any]
+
+"""
+
+ lines = [header]
+
+ # Include safe variable definitions (Literal types, TypeVars, etc.)
+ lines.append("# Safe type definitions preserved from original types.py")
+ for source in safe_variable_sources:
+ lines.append(source)
+ lines.append("")
+
+ # Stub all classes and unsafe variables uniformly as dict[str, Any] aliases
+ # This allows:
+ # 1. Use in type annotations: x: SomeType
+ # 2. Constructor calls: SomeType(...)
+ # 3. Dict literal assignments: x: SomeType = {...}
+ lines.append(
+ "# Stubbed types (collapsed to dict[str, Any] to prevent type budget exhaustion)"
+ )
+ all_stubbed = sorted(classes | unsafe_variables)
+ for name in all_stubbed:
+ lines.append(f"{name} = _PrismaDict")
+
+ lines.append("")
+
+ # Stub functions
+ for name in sorted(functions):
+ lines.append(f"def {name}(*args: Any, **kwargs: Any) -> Any: ...")
+
+ lines.append("")
+
+ stub_path.write_text("\n".join(lines), encoding="utf-8")
+ return (
+ len(classes)
+ + len(functions)
+ + len(safe_variable_sources)
+ + len(unsafe_variables)
+ )
+
+
+def main() -> None:
+ """Main entry point."""
+ try:
+ types_path = find_prisma_types_path()
+ stub_path = types_path.with_suffix(".pyi")
+
+ print(f"Found prisma types.py at: {types_path}")
+ print(f"Generating stub at: {stub_path}")
+
+ num_symbols = generate_stub(types_path, stub_path)
+ print(f"Generated {stub_path.name} with {num_symbols} Any-typed symbols")
+
+ except Exception as e:
+ print(f"Error: {e}", file=sys.stderr)
+ sys.exit(1)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/autogpt_platform/backend/linter.py b/autogpt_platform/backend/linter.py
index a86e6761f7..599aae4580 100644
--- a/autogpt_platform/backend/linter.py
+++ b/autogpt_platform/backend/linter.py
@@ -25,6 +25,9 @@ def run(*command: str) -> None:
def lint():
+ # Generate Prisma types stub before running pyright to prevent type budget exhaustion
+ run("gen-prisma-stub")
+
lint_step_args: list[list[str]] = [
["ruff", "check", *TARGET_DIRS, "--exit-zero"],
["ruff", "format", "--diff", "--check", LIBS_DIR],
@@ -49,4 +52,6 @@ def format():
run("ruff", "format", LIBS_DIR)
run("isort", "--profile", "black", BACKEND_DIR)
run("black", BACKEND_DIR)
+ # Generate Prisma types stub before running pyright to prevent type budget exhaustion
+ run("gen-prisma-stub")
run("pyright", *TARGET_DIRS)
diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.toml
index e8b8fd0ba5..88aff1d0b0 100644
--- a/autogpt_platform/backend/pyproject.toml
+++ b/autogpt_platform/backend/pyproject.toml
@@ -117,6 +117,7 @@ lint = "linter:lint"
test = "run_tests:test"
load-store-agents = "test.load_store_agents:run"
export-api-schema = "backend.cli.generate_openapi_json:main"
+gen-prisma-stub = "gen_prisma_types_stub:main"
oauth-tool = "backend.cli.oauth_tool:cli"
[tool.isort]
diff --git a/autogpt_platform/docker-compose.platform.yml b/autogpt_platform/docker-compose.platform.yml
index b2df626029..de6ecfd612 100644
--- a/autogpt_platform/docker-compose.platform.yml
+++ b/autogpt_platform/docker-compose.platform.yml
@@ -37,7 +37,7 @@ services:
context: ../
dockerfile: autogpt_platform/backend/Dockerfile
target: migrate
- command: ["sh", "-c", "poetry run prisma generate && poetry run prisma migrate deploy"]
+ command: ["sh", "-c", "poetry run prisma generate && poetry run gen-prisma-stub && poetry run prisma migrate deploy"]
develop:
watch:
- path: ./