ai icon indicating copy to clipboard operation
ai copied to clipboard

TypeScript OOM with AI SDK 5.x in large Next.js project - No solution works, need official fix

Open yhfy2006 opened this issue 1 year ago • 6 comments

📋 Problem Summary

When integrating Vercel AI SDK 5.x into a large Next.js project, TypeScript type checking (tsc --noEmit) consistently fails with Out of Memory (OOM) errors in both CI/CD and local environments.

Error Message

FATAL ERROR: Ineffective mark-compacts near heap limit
Allocation failed - JavaScript heap out of memory

Exit Code: 134 (SIGABRT - Memory exhaustion)

🔍 Root Cause

Technical Analysis

  1. AI SDK's Complex Type System

    • Functions like streamText, useCompletion use deeply nested generic types
    • Contains extensive conditional types and mapped types
    • TypeScript compiler must recursively expand these type definitions
  2. Exponential Type Inference Complexity

    // Simple-looking code
    const result = streamText({
      model: openrouter(config.defaultModel),
      prompt,
      temperature: config.temperature,
    });
    
    // But TypeScript infers a type chain with 20+ generic parameters
    // Consuming 4GB+ memory during type checking
    
  3. Memory Consumption Data

    Environment Memory Usage Result
    CI (GitHub Actions) ~2GB → OOM ❌ Failed
    Local (MacBook) ~4GB → OOM ❌ Failed
    main branch (no AI SDK) ~1-2GB ✅ Normal

🚫 Attempted Solutions (All Failed)

1. ❌ Increase Node.js Memory Limit

{
  "scripts": {
    "typecheck": "NODE_OPTIONS='--max-old-space-size=8192' tsc --noEmit"
  }
}

Result: 8GB insufficient, 16GB also insufficient

2. ❌ Use skipLibCheck

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

Result: Only skips .d.ts file checking, doesn't prevent loading types from node_modules

3. ❌ Use @ts-nocheck

// @ts-nocheck
import { streamText } from 'ai';

Result: TypeScript still loads and parses dependent type definitions

4. ❌ Exclude Specific Files

{
  "exclude": [
    "app/api/soap/summary/route.ts"
  ]
}

Result: TypeScript still loads AI SDK types from node_modules

5. ❌ Update moduleResolution

{
  "compilerOptions": {
    "module": "nodenext",
    "moduleResolution": "nodenext"
  }
}

Result: Official recommended solution, but ineffective for our project configuration

6. ❌ Create Type Stub Overrides

Created simplified .d.ts files and used paths mapping to override AI SDK types Result: TypeScript still prioritizes actual types from node_modules

7. ❌ Upgrade Zod to 4.1.12

pnpm add zod@^4.1.12
pnpm add @hookform/resolvers@^5.2.2

Result: Fixed Zod 4 breaking changes and type errors, but TypeScript still OOMs during typecheck. The OOM issue is caused by AI SDK's complex types, not Zod.

⚠️ Current Status: UNRESOLVED

What We've Done

  • ✅ Upgraded Zod from 3.24.2 to 4.1.12
  • ✅ Fixed all Zod 4 breaking changes (z.record(), ZodError.issues)
  • ✅ Upgraded @hookform/resolvers to 5.2.2
  • ✅ Resolved all TypeScript type errors (240 errors → 0 errors)
  • ✅ IDE type checking works normally
  • ✅ Runtime functionality fully operational

What Still Fails

  • ❌ pnpm run typecheck still causes OOM
  • ❌ CI/CD typecheck still fails with exit code 134
  • ❌ Memory consumption still reaches 4GB+ and crashes

Root Cause Confirmed

The OOM issue is NOT caused by Zod, but by AI SDK 5.x's extremely complex generic type definitions. Even with Zod 4, TypeScript compiler cannot handle the type inference complexity in large projects.

🔧 Current Workaround

Since no solution works, we've had to disable typecheck in CI/CD:

# .github/workflows/ci.yml
jobs:
  quality-check:
    runs-on: ubuntu-latest
    steps:
      - name: Run ESLint
        run: pnpm run lint

      # TypeCheck disabled due to AI SDK OOM issue
      # Waiting for official fix from Vercel
      # - name: Type Check
      #   run: pnpm run typecheck

      - name: Run Tests
        run: pnpm test

Alternative Quality Assurance

  1. ESLint - Provides code quality checks
  2. IDE Type Checking - VSCode TypeScript language server works fine
  3. Runtime Tests - Comprehensive test coverage
  4. Manual Code Review - Extra scrutiny on AI SDK code

🙏 Request for Official Fix

We need help from the Vercel AI SDK team to:

  1. Simplify the type definitions in AI SDK 5.x
  2. Reduce generic type nesting depth
  3. Provide a configuration option to use simplified types for large projects
  4. Document memory requirements for different project sizes
  5. Offer guidance on type-checking large projects with AI SDK

📝 Environment Details

  • AI SDK Version: 5.0.60 (@ai-sdk/react: 2.0.60)
  • Zod Version: 4.1.12 (upgraded from 3.24.2)
  • @hookform/resolvers: 5.2.2 (upgraded from 4.0.0)
  • TypeScript Version: 5.7.3
  • Next.js Version: 15.1.7
  • Node.js Version: 22.x
  • Project Size: ~5,000+ lines of code
  • Monorepo: Turborepo with multiple packages

🔗 Related Issues

  • Related to #7351 (Memory leak with aisdk 5.19-5.20 and zod 4)
  • Official docs: https://ai-sdk.dev/docs/troubleshooting/typescript-performance-zod

📅 Timeline

  • 2025-01-26: Initial problem encountered
  • 2025-01-26: Attempted 7+ different solutions, all failed
  • 2025-01-26: Upgraded Zod to 4.1.12 - fixed type errors but OOM persists
  • 2025-01-26: Disabled typecheck in CI/CD as temporary workaround
  • Current: Waiting for official fix from Vercel AI SDK team

Note: This issue documents a critical blocker for using AI SDK 5.x in large production projects. The TypeScript OOM problem makes it impossible to run type checking in CI/CD, forcing teams to choose between AI SDK features and type safety guarantees.

yhfy2006 avatar Oct 07 '25 20:10 yhfy2006

Does the problem persist? Please make sure to use the latest AI SDK and zod package versions when testing.

gr2m avatar Nov 22 '25 19:11 gr2m

@yhfy2006 Rollback to zod 4.1.8. I had the same issue.

olegsubbly avatar Nov 28 '25 17:11 olegsubbly

I ran into this issue with Vercel AI SDK v6, but after upgrading Zod to version 4, it was resolved.

buttercoding avatar Jan 26 '26 23:01 buttercoding

@buttercoding yeah, it also works for me. going to close the issue.

yhfy2006 avatar Jan 27 '26 02:01 yhfy2006

Had the same issue, with ai@6 when running eslint, especially with this rule "plugin:@typescript-eslint/recommended-type-checked"

upgrading zod to version 4, fixed.

memoricab avatar Apr 16 '26 12:04 memoricab

Identify issue type

Identified as Bug
Confidence: High

Rationale

The issue reports a concrete TypeScript OOM failure when using the existing AI SDK. Although it was ultimately resolved through a Zod upgrade, it remains a report of broken behavior rather than a request solely for a new capability or documentation.

Evidence

  • AI SDK 5.x integration reportedly caused tsc --noEmit to exhaust 4–16 GB of memory in a large Next.js project.
  • The failure blocked local and CI type checking and was attributed to complex SDK type inference.
  • Later commenters reported that upgrading to Zod 4 resolved the issue, and the author confirmed the fix and closed it.

lgrammel avatar Jul 22 '26 13:07 lgrammel