Skip to content

wrapLanguageModel doesn't validate object schema #9594

@so-shiba

Description

@so-shiba

Description

The official documentation for caching https://ai-sdk.dev/docs/advanced/caching uses wrapLanguageModel to intercept doGenerate call. However, doGenerate doesn't validate the object with json schema. Therefore, if doGenerate returns a AI response with invalid json, it's still being cached and the generateObject will fail consistently

AI SDK Version

export const cacheMiddleware: LanguageModelV2Middleware = {
wrapGenerate: async ({ doGenerate, params }) => {
const cacheKey = JSON.stringify(params);

const cached = (await redis.get(cacheKey)) as Awaited<
  ReturnType<LanguageModelV2['doGenerate']>
> | null;

if (cached !== null) {
  return {
    ...cached,
    response: {
      ...cached.response,
      timestamp: cached?.response?.timestamp
        ? new Date(cached?.response?.timestamp)
        : undefined,
    },
  };
}

const result = await doGenerate();

redis.set(cacheKey, result);

return result;

},
}

This happens on all versions of ai sdk

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions