AI SDK giving crypto is not defined error
I am using vercel ai sdk (aws bedrock with sonnet), tried using both node and non node environment. But still I am getting this error
Uncaught ReferenceError: crypto is not defined at hmac (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:219:4) at signature [as signature] (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:181:22) at authHeader [as authHeader] (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:173:15) at sign [as sign] (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:160:4) at <anonymous> (../node_modules/@ai-sdk/amazon-bedrock/src/bedrock-sigv4-fetch.ts:57:26) at postToApi (../node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:65:14) at postJsonToApi (../node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:28:4) at doGenerate [as doGenerate] (../node_modules/@ai-sdk/amazon-bedrock/src/bedrock-chat-language-model.ts:243:6) at async fn (../node_modules/ai/core/generate-text/generate-text.ts:350:16) at async <anonymous> (../node_modules/ai/core/telemetry/record-span.ts:19:10)
Uncaught ReferenceError: crypto is not defined at hmac (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:219:4) at signature [as signature] (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:181:22) at authHeader [as authHeader] (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:173:15) at sign [as sign] (../node_modules/aws4fetch/dist/aws4fetch.esm.mjs:160:4) at <anonymous> (../node_modules/@ai-sdk/amazon-bedrock/src/bedrock-sigv4-fetch.ts:57:26) at postToApi (../node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:65:14) at postJsonToApi (../node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:28:4) at doGenerate [as doGenerate] (../node_modules/@ai-sdk/amazon-bedrock/src/bedrock-chat-language-model.ts:243:6) at async fn (../node_modules/ai/core/generate-text/generate-text.ts:350:16) at async <anonymous> (../node_modules/ai/core/telemetry/record-span.ts:19:10)
12 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
here is my action code
export const convertTranscriptToLinkedInPost = internalAction({
args: {
videoTranscript: v.string(),
templateContent: v.optional(v.string()),
},
handler: async (ctx, args) => {
const { videoTranscript, templateContent } = args;
// Call ai sdk to convert transcript to linkedin post
try {
const bedrock = createAmazonBedrock({
region: 'us-east-1',
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
});
console.log(
'AWS Bedrock | Model',
bedrock('anthropic.claude-3-5-sonnet-20240620-v1:0'),
);
const { text } = await generateText({
model: bedrock('anthropic.claude-3-5-sonnet-20240620-v1:0'),
system: systemPrompt,
prompt: getTranscriptToLinkedInPostPrompt(
templateContent,
videoTranscript,
),
});
return text;
} catch (error) {
console.error('Error in LLM call', error);
throw new Error('Error in LLM call ' + error);
}
},
});
Interesting. The "ai" library has worked for others. Is this in a "use node" action?
yes @Lee
is it due to createAmazonBedrock from ai sdk ??
without "use node" I am getting this error
[TypeError: this.url.searchParams is not iterable]
I haven't seen either of those errors. Will look into later
could you share the imports you're doing so i can repro?
my best guess based on the error messages:
1. our "use node" environment uses Node 18, which doesn't support the
crypto
global -- because it was introduced in a later version of Node
2. our default environment implements most APIs but sometimes we have missed some. That [TypeError: this.url.searchParams is not iterable]
sounds like something we should just fixThis is my imports also I am calling
convertTranscriptToLinkedInPost
in workflow component
import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';
import { generateText } from 'ai';
Could you suggest some solutions to solve this. I am stuck with this as I am migrating my all LLM calls to vercel AI sdkI expect we'll be able to fix (2) on monday. Are you aware of any other reason why you would need "use node"?
As a workaround for (1), i expect polyfills of the "crypto" global exist for older versions of node.
No I was trying with node and without node to figure out why things are not working
ok i fixed the error
[TypeError: this.url.searchParams is not iterable]
. would be interested to know if the non-node runtime works nowThanks for the quick turnaround, let me check
@Lee , its working thank you !!
Yay!