Skip to content

fix: withSupermemory incompatible with AI SDK 6 — preserve prototype properties via Proxy#853

Closed
vorflux[bot] wants to merge 3 commits intomainfrom
vorflux/fix-ai-sdk-v6-model-wrapping
Closed

fix: withSupermemory incompatible with AI SDK 6 — preserve prototype properties via Proxy#853
vorflux[bot] wants to merge 3 commits intomainfrom
vorflux/fix-ai-sdk-v6-model-wrapping

Conversation

@vorflux
Copy link
Copy Markdown
Contributor

@vorflux vorflux bot commented Apr 15, 2026

Summary

Fixes #852withSupermemory throws AI_UnsupportedModelVersionError when used with AI SDK 6 (LanguageModelV3) models.

Root Cause

The wrapVercelLanguageModel function used object spread ({ ...model, doGenerate: ..., doStream: ... }) to wrap the model. The spread operator only copies own enumerable properties, but AI SDK v4/v5/v6 models are class instances where provider, doGenerate, and doStream are defined as prototype getters/methods. This caused provider to become undefined on the wrapped model, triggering AI_UnsupportedModelVersionError at runtime.

Fix

Replaced the object spread with a Proxy-based delegation pattern:

  1. A Proxy intercepts get operations on the model
  2. For doGenerate and doStream — returns the memory-aware overrides
  3. For all other properties — delegates to the original model via Reflect.get(target, prop, target), ensuring this inside prototype getters remains the real model instance

This preserves:

  • Prototype getters (e.g. provider in V3 models)
  • ES private fields (#config) — future-proof against SDK changes
  • Instance brand checks
  • All V2 plain-object properties (backwards compatible)

Changes

  • packages/tools/src/vercel/index.ts — replaced spread with Proxy delegation
  • packages/tools/test/with-supermemory/unit.test.ts — added V3 compatibility tests (prototype getters, ES private fields, Proxy correctness)

Testing

Unit Tests (14/14 passing)

cd packages/tools && npx vitest run test/with-supermemory/unit.test.ts
Test Status
Environment validation: throw if no API key PASS
Environment validation: create wrapped model with valid key PASS
AI SDK 6 (V3): preserve prototype getter properties PASS
AI SDK 6 (V3): override doGenerate and doStream PASS
AI SDK 6 (V3): ES private fields (#config) PASS
V2 backwards compatibility: preserve all properties PASS
Proxy override correctness: no Object.prototype shadowing PASS
Memory caching: cache on first call PASS
Memory caching: use cache on continuation step PASS
Memory caching: refetch on new user turn PASS
Edge cases: API errors gracefully PASS
Edge cases: empty prompt array PASS
Edge cases: empty content in query mode PASS
Edge cases: no mutation of original params PASS

Cross-Version Real API Tests (v4, v5, v6 — all passing)

Tested with real OpenAI API calls (gpt-4o-mini) against three isolated SDK installations:

AI SDK v4 (specificationVersion: v1) — ALL PASSED

  • Property preservation: specificationVersion, provider, modelId all preserved
  • generateText returned valid response
  • streamText returned valid response

AI SDK v5 (specificationVersion: v2) — ALL PASSED

  • Property preservation: specificationVersion, provider, modelId, supportedUrls all preserved
  • generateText returned valid response
  • streamText returned valid response

AI SDK v6 (specificationVersion: v3) — ALL PASSED

  • Property preservation: specificationVersion, provider, modelId, supportedUrls all preserved
  • generateText returned valid response
  • streamText returned valid response

Build

cd packages/tools && bun run build

Build succeeds with no errors.


Session Details

Vorflux AI added 3 commits April 15, 2026 19:12
…AI SDK 6 compatibility

Replace object spread with Object.create + Object.defineProperties to preserve
prototype getters (e.g. `provider`) that AI SDK v4/v5/v6 models define on their
class prototype rather than as own properties.

The spread operator `{ ...model }` only copies own enumerable properties, which
causes prototype getters like `provider` to become `undefined` on the wrapped
model. This triggers AI_UnsupportedModelVersionError at runtime with AI SDK 6.

The fix:
1. Creates the wrapper via Object.create(Object.getPrototypeOf(model)) to
   inherit all prototype properties (getters, methods)
2. Copies all own property descriptors via Object.getOwnPropertyDescriptors
3. Overrides doGenerate/doStream on the instance to shadow the inherited ones

Tested with real OpenAI API calls against AI SDK v4, v5, and v6.

Closes #852
Addresses review feedback about ES private fields breaking with
Object.create + Object.defineProperties approach:

Source changes (packages/tools/src/vercel/index.ts):
- Replace Object.create/Object.defineProperties with a Proxy that
  delegates property access to the original model instance. This
  preserves prototype getters, ES private fields (#field), and
  instance brand checks since 'this' inside getters remains the
  real model instance.
- Extract doGenerate/doStream overrides into named functions
  (wrappedDoGenerate/wrappedDoStream) for clarity.
- Remove one-use 'descriptors' intermediate variable (inlined).

Test changes (packages/tools/test/with-supermemory/unit.test.ts):
- Remove duplicate test 'should preserve all V3 metadata after
  wrapping' (strict subset of prototype getter test).
- Add MockLanguageModelV3WithPrivateFields class using true ES
  private fields (#config) to catch TypeError that would occur
  with Object.create-based wrapping.
- Add test 'should handle V3 models with ES private fields' that
  verifies the Proxy approach works with private field access.
- Clean up 'valid API key' test to be intentionally minimal
  (property preservation covered by dedicated V2/V3 suites).
…rototype props

The 'in' operator checks the entire prototype chain, so
'constructor' in overrides would match Object.prototype.constructor
and return undefined from the overrides map. Use hasOwnProperty.call()
to only match own keys (doGenerate, doStream).
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 15, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
supermemory-app ee9bfe7 Commit Preview URL

Branch Preview URL
Apr 15 2026, 07:35 PM

@MaheshtheDev
Copy link
Copy Markdown
Member

#854

@vorflux
Copy link
Copy Markdown
Contributor Author

vorflux bot commented Apr 15, 2026

Thanks for the heads up @MaheshtheDev! Looks like #854 addresses the same root cause (spread dropping prototype properties) with a similar Proxy approach. Happy to defer to whichever PR the maintainers prefer to merge -- the core fix is the same in both.

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.

withSupermemory incompatible with AI SDK 6

1 participant